ffmpeg-devel
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
February 2022
- 99 participants
- 339 discussions
[PATCH 1/2] lafi/vf_edgedetect: Move some common functions into seperate file
by Thilo Borgmann 25 Apr '22
by Thilo Borgmann 25 Apr '22
25 Apr '22
Hi,
v2, common functions moved into lavfi/edge_common.{c,h}. Also moved gaussian_blur() into the same.
-Thilo
3
13
24 Apr '22
Monterey needs mBytesPerFrame and mBytesPerPacket to be set, and I'm
surprised this didn't break any previous system versions.
Fixes bug #9564: Cannot decode xHE-AAC with audiotoolbox (aac_at) on
Mac OS Monterey. Fixes likely bug that none of the AudioToolbox
decoders work on Monterey.
Signed-off-by: Christopher Snowhill <kode54(a)gmail.com>
---
libavcodec/audiotoolboxdec.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libavcodec/audiotoolboxdec.c b/libavcodec/audiotoolboxdec.c
index 9939fef218..4abcb63a03 100644
--- a/libavcodec/audiotoolboxdec.c
+++ b/libavcodec/audiotoolboxdec.c
@@ -370,6 +370,11 @@ static av_cold int ffat_create_decoder(AVCodecContext *avctx,
avctx->sample_rate = out_format.mSampleRate = in_format.mSampleRate;
avctx->channels = out_format.mChannelsPerFrame = in_format.mChannelsPerFrame;
+ out_format.mBytesPerFrame =
+ out_format.mChannelsPerFrame * (out_format.mBitsPerChannel / 8);
+ out_format.mBytesPerPacket =
+ out_format.mBytesPerFrame * out_format.mFramesPerPacket;
+
if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_QT)
in_format.mFramesPerPacket = 64;
--
2.32.0 (Apple Git-132)
2
1
18 Apr '22
---
v4: break when error occured in fread, fix infinite loop introduced by v3
v3: check EOF by "eof = !data_size && feof(f);"
doc/examples/decode_video.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/doc/examples/decode_video.c b/doc/examples/decode_video.c
index 18ee90a6c0..7238e38103 100644
--- a/doc/examples/decode_video.c
+++ b/doc/examples/decode_video.c
@@ -92,6 +92,7 @@ int main(int argc, char **argv)
uint8_t *data;
size_t data_size;
int ret;
+ int eof;
AVPacket *pkt;
if (argc <= 2) {
@@ -150,15 +151,16 @@ int main(int argc, char **argv)
exit(1);
}
- while (!feof(f)) {
+ do {
/* read raw data from the input file */
data_size = fread(inbuf, 1, INBUF_SIZE, f);
- if (!data_size)
+ if (ferror(f))
break;
+ eof = !data_size;
/* use the parser to split the data into frames */
data = inbuf;
- while (data_size > 0) {
+ while (data_size > 0 || eof) {
ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0) {
@@ -170,8 +172,10 @@ int main(int argc, char **argv)
if (pkt->size)
decode(c, frame, pkt, outfilename);
+ else if (eof)
+ break;
}
- }
+ } while (!eof);
/* flush the decoder */
decode(c, frame, NULL, outfilename);
--
2.31.1
2
2
17 Apr '22
From: Gyan Doshi <ffmpeg(a)gyani.pro>
The fate test file can be found here: https://drive.google.com/file/d/1jGW3f94rglLfr5WGmMQe3SEnp1YkbMRy/view?usp=…
The video file needs to be copied to fate-suite/mkv/
---
libavcodec/dynamic_hdr10_plus.c | 273 +++++++++++++++++---
libavcodec/dynamic_hdr10_plus.h | 35 ++-
libavformat/matroska.h | 5 +
libavformat/matroskadec.c | 30 ++-
libavformat/matroskaenc.c | 47 ++--
tests/fate/matroska.mak | 6 +
tests/ref/fate/matroska-hdr10-plus-metadata | 150 +++++++++++
7 files changed, 484 insertions(+), 62 deletions(-)
create mode 100644 tests/ref/fate/matroska-hdr10-plus-metadata
diff --git a/libavcodec/dynamic_hdr10_plus.c b/libavcodec/dynamic_hdr10_plus.c
index a602e606ed..df7828a476 100644
--- a/libavcodec/dynamic_hdr10_plus.c
+++ b/libavcodec/dynamic_hdr10_plus.c
@@ -18,6 +18,12 @@
#include "dynamic_hdr10_plus.h"
#include "get_bits.h"
+#include "put_bits.h"
+
+static const uint8_t usa_country_code = 0xB5;
+static const uint16_t smpte_provider_code = 0x003C;
+static const uint16_t smpte2094_40_provider_oriented_code = 0x0001;
+static const uint16_t smpte2094_40_application_identifier = 0x04;
static const int64_t luminance_den = 1;
static const int32_t peak_luminance_den = 15;
@@ -27,8 +33,8 @@ static const int32_t knee_point_den = 4095;
static const int32_t bezier_anchor_den = 1023;
static const int32_t saturation_weight_den = 8;
-int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t *data,
- int size)
+int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus* s, const uint8_t* data,
+ int size)
{
GetBitContext gbc, *gb = &gbc;
int ret;
@@ -40,7 +46,9 @@ int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t
if (ret < 0)
return ret;
- s->application_version = get_bits(gb, 8);
+ if (get_bits_left(gb) < 8)
+ return AVERROR_INVALIDDATA;
+ s->application_version = get_bits(gb, 8);
if (get_bits_left(gb) < 2)
return AVERROR_INVALIDDATA;
@@ -56,15 +64,11 @@ int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t
for (int w = 1; w < s->num_windows; w++) {
// The corners are set to absolute coordinates here. They should be
// converted to the relative coordinates (in [0, 1]) in the decoder.
- AVHDRPlusColorTransformParams *params = &s->params[w];
- params->window_upper_left_corner_x =
- (AVRational){get_bits(gb, 16), 1};
- params->window_upper_left_corner_y =
- (AVRational){get_bits(gb, 16), 1};
- params->window_lower_right_corner_x =
- (AVRational){get_bits(gb, 16), 1};
- params->window_lower_right_corner_y =
- (AVRational){get_bits(gb, 16), 1};
+ AVHDRPlusColorTransformParams* params = &s->params[w];
+ params->window_upper_left_corner_x = (AVRational) { get_bits(gb, 16), 1 };
+ params->window_upper_left_corner_y = (AVRational) { get_bits(gb, 16), 1 };
+ params->window_lower_right_corner_x = (AVRational) { get_bits(gb, 16), 1 };
+ params->window_lower_right_corner_y = (AVRational) { get_bits(gb, 16), 1 };
params->center_of_ellipse_x = get_bits(gb, 16);
params->center_of_ellipse_y = get_bits(gb, 16);
@@ -78,8 +82,7 @@ int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t
if (get_bits_left(gb) < 28)
return AVERROR(EINVAL);
- s->targeted_system_display_maximum_luminance =
- (AVRational){get_bits_long(gb, 27), luminance_den};
+ s->targeted_system_display_maximum_luminance = (AVRational) { get_bits_long(gb, 27), luminance_den };
s->targeted_system_display_actual_peak_luminance_flag = get_bits1(gb);
if (s->targeted_system_display_actual_peak_luminance_flag) {
@@ -99,38 +102,33 @@ int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
- s->targeted_system_display_actual_peak_luminance[i][j] =
- (AVRational){get_bits(gb, 4), peak_luminance_den};
+ s->targeted_system_display_actual_peak_luminance[i][j] = (AVRational) { get_bits(gb, 4), peak_luminance_den };
}
}
}
for (int w = 0; w < s->num_windows; w++) {
- AVHDRPlusColorTransformParams *params = &s->params[w];
+ AVHDRPlusColorTransformParams* params = &s->params[w];
if (get_bits_left(gb) < (3 * 17 + 17 + 4))
return AVERROR(EINVAL);
for (int i = 0; i < 3; i++) {
- params->maxscl[i] =
- (AVRational){get_bits(gb, 17), rgb_den};
+ params->maxscl[i] = (AVRational) { get_bits(gb, 17), rgb_den };
}
- params->average_maxrgb =
- (AVRational){get_bits(gb, 17), rgb_den};
+ params->average_maxrgb = (AVRational) { get_bits(gb, 17), rgb_den };
params->num_distribution_maxrgb_percentiles = get_bits(gb, 4);
- if (get_bits_left(gb) <
- (params->num_distribution_maxrgb_percentiles * 24))
+ if (get_bits_left(gb) < (params->num_distribution_maxrgb_percentiles * 24))
return AVERROR(EINVAL);
for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
params->distribution_maxrgb[i].percentage = get_bits(gb, 7);
- params->distribution_maxrgb[i].percentile =
- (AVRational){get_bits(gb, 17), rgb_den};
+ params->distribution_maxrgb[i].percentile = (AVRational) { get_bits(gb, 17), rgb_den };
}
if (get_bits_left(gb) < 10)
return AVERROR(EINVAL);
- params->fraction_bright_pixels = (AVRational){get_bits(gb, 10), fraction_pixel_den};
+ params->fraction_bright_pixels = (AVRational) { get_bits(gb, 10), fraction_pixel_den };
}
if (get_bits_left(gb) < 1)
return AVERROR(EINVAL);
@@ -152,14 +150,13 @@ int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
- s->mastering_display_actual_peak_luminance[i][j] =
- (AVRational){get_bits(gb, 4), peak_luminance_den};
+ s->mastering_display_actual_peak_luminance[i][j] = (AVRational) { get_bits(gb, 4), peak_luminance_den };
}
}
}
for (int w = 0; w < s->num_windows; w++) {
- AVHDRPlusColorTransformParams *params = &s->params[w];
+ AVHDRPlusColorTransformParams* params = &s->params[w];
if (get_bits_left(gb) < 1)
return AVERROR(EINVAL);
@@ -168,18 +165,15 @@ int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t
if (get_bits_left(gb) < 28)
return AVERROR(EINVAL);
- params->knee_point_x =
- (AVRational){get_bits(gb, 12), knee_point_den};
- params->knee_point_y =
- (AVRational){get_bits(gb, 12), knee_point_den};
+ params->knee_point_x = (AVRational) { get_bits(gb, 12), knee_point_den };
+ params->knee_point_y = (AVRational) { get_bits(gb, 12), knee_point_den };
params->num_bezier_curve_anchors = get_bits(gb, 4);
if (get_bits_left(gb) < (params->num_bezier_curve_anchors * 10))
return AVERROR(EINVAL);
for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
- params->bezier_curve_anchors[i] =
- (AVRational){get_bits(gb, 10), bezier_anchor_den};
+ params->bezier_curve_anchors[i] = (AVRational) { get_bits(gb, 10), bezier_anchor_den };
}
}
@@ -189,10 +183,215 @@ int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t
if (params->color_saturation_mapping_flag) {
if (get_bits_left(gb) < 6)
return AVERROR(EINVAL);
- params->color_saturation_weight =
- (AVRational){get_bits(gb, 6), saturation_weight_den};
+ params->color_saturation_weight = (AVRational) { get_bits(gb, 6), saturation_weight_den };
}
}
return 0;
}
+
+int ff_parse_full_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus* s, const uint8_t* data,
+ int size)
+{
+ uint8_t country_code;
+ uint16_t provider_code;
+ uint16_t provider_oriented_code;
+ uint8_t application_identifier;
+ GetBitContext gbc, *gb = &gbc;
+ int ret, offset;
+
+ if (!s)
+ return AVERROR(ENOMEM);
+
+ if (size < 7)
+ return AVERROR_INVALIDDATA;
+
+ ret = init_get_bits8(gb, data, size);
+ if (ret < 0)
+ return ret;
+
+ country_code = get_bits(gb, 8);
+ provider_code = get_bits(gb, 16);
+
+ if (country_code != usa_country_code ||
+ provider_code != smpte_provider_code)
+ return AVERROR_INVALIDDATA;
+
+ // A/341 Amendment – 2094-40
+ provider_oriented_code = get_bits(gb, 16);
+ application_identifier = get_bits(gb, 8);
+ if (provider_oriented_code != smpte2094_40_provider_oriented_code ||
+ application_identifier != smpte2094_40_application_identifier)
+ return AVERROR_INVALIDDATA;
+
+ offset = get_bits_count(gb) / 8;
+
+ return ff_parse_itu_t_t35_to_dynamic_hdr10_plus(s, gb->buffer + offset, size - offset);
+}
+
+int ff_itu_t_t35_buffer_size(const AVDynamicHDRPlus* s)
+{
+ int bit_count = 0;
+ int w, size;
+
+ if (!s)
+ return 0;
+
+ // 7 bytes for country code, provider code, and user identifier.
+ bit_count += 56;
+
+ if (s->num_windows < 1 || s->num_windows > 3)
+ return 0;
+ // Count bits for window params.
+ bit_count += 2 + ((19 * 8 + 1) * (s->num_windows - 1));
+
+ bit_count += 28;
+ if (s->targeted_system_display_actual_peak_luminance_flag) {
+ int rows, cols;
+ rows = s->num_rows_targeted_system_display_actual_peak_luminance;
+ cols = s->num_cols_targeted_system_display_actual_peak_luminance;
+ if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25)))
+ return 0;
+
+ bit_count += (10 + rows * cols * 4);
+ }
+ for (w = 0; w < s->num_windows; w++) {
+ bit_count += (3 * 17 + 17 + 4 + 10) + (s->params[w].num_distribution_maxrgb_percentiles * 24);
+ }
+ bit_count++;
+
+ if (s->mastering_display_actual_peak_luminance_flag) {
+ int rows, cols;
+ rows = s->num_rows_mastering_display_actual_peak_luminance;
+ cols = s->num_cols_mastering_display_actual_peak_luminance;
+ if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25)))
+ return 0;
+
+ bit_count += (10 + rows * cols * 4);
+ }
+
+ for (w = 0; w < s->num_windows; w++) {
+ bit_count++;
+ if (s->params[w].tone_mapping_flag)
+ bit_count += (28 + s->params[w].num_bezier_curve_anchors * 10);
+
+ bit_count++;
+ if (s->params[w].color_saturation_mapping_flag)
+ bit_count += 6;
+ }
+ size = bit_count / 8;
+ if (bit_count % 8 != 0)
+ size++;
+ return size;
+}
+
+int ff_write_dynamic_hdr10_plus_to_full_itu_t_t35(const AVDynamicHDRPlus* s, uint8_t** data, size_t* size)
+{
+ int w, i, j;
+ PutBitContext pbc, *pb = &pbc;
+
+ if (!s || !size)
+ return AVERROR(EINVAL);
+
+ *size = ff_itu_t_t35_buffer_size(s);
+ if (*size <= 0)
+ return AVERROR(EINVAL);
+ *data = av_mallocz(*size);
+ init_put_bits(pb, *data, *size);
+ if (put_bits_left(pb) < *size) {
+ av_freep(data);
+ return AVERROR(EINVAL);
+ }
+ put_bits(pb, 8, usa_country_code);
+
+ put_bits(pb, 16, smpte_provider_code);
+ put_bits(pb, 16, smpte2094_40_provider_oriented_code);
+ put_bits(pb, 8, smpte2094_40_application_identifier);
+ put_bits(pb, 8, s->application_version);
+
+ put_bits(pb, 2, s->num_windows);
+
+ for (w = 1; w < s->num_windows; w++) {
+ put_bits(pb, 16, s->params[w].window_upper_left_corner_x.num / s->params[w].window_upper_left_corner_x.den);
+ put_bits(pb, 16, s->params[w].window_upper_left_corner_y.num / s->params[w].window_upper_left_corner_y.den);
+ put_bits(pb, 16, s->params[w].window_lower_right_corner_x.num / s->params[w].window_lower_right_corner_x.den);
+ put_bits(pb, 16, s->params[w].window_lower_right_corner_y.num / s->params[w].window_lower_right_corner_y.den);
+ put_bits(pb, 16, s->params[w].center_of_ellipse_x);
+ put_bits(pb, 16, s->params[w].center_of_ellipse_y);
+ put_bits(pb, 8, s->params[w].rotation_angle);
+ put_bits(pb, 16, s->params[w].semimajor_axis_internal_ellipse);
+ put_bits(pb, 16, s->params[w].semimajor_axis_external_ellipse);
+ put_bits(pb, 16, s->params[w].semiminor_axis_external_ellipse);
+ put_bits(pb, 1, s->params[w].overlap_process_option);
+ }
+ put_bits(pb, 27,
+ s->targeted_system_display_maximum_luminance.num * luminance_den / s->targeted_system_display_maximum_luminance.den);
+ put_bits(pb, 1, s->targeted_system_display_actual_peak_luminance_flag);
+ if (s->targeted_system_display_actual_peak_luminance_flag) {
+ int rows, cols;
+ rows = s->num_rows_targeted_system_display_actual_peak_luminance;
+ cols = s->num_cols_targeted_system_display_actual_peak_luminance;
+ put_bits(pb, 5, rows);
+ put_bits(pb, 5, cols);
+ for (i = 0; i < rows; i++) {
+ for (j = 0; j < cols; j++) {
+ put_bits(
+ pb, 4,
+ s->targeted_system_display_actual_peak_luminance[i][j].num * peak_luminance_den / s->targeted_system_display_actual_peak_luminance[i][j].den);
+ }
+ }
+ }
+ for (w = 0; w < s->num_windows; w++) {
+ for (i = 0; i < 3; i++) {
+ put_bits(pb, 17,
+ s->params[w].maxscl[i].num * rgb_den / s->params[w].maxscl[i].den);
+ }
+ put_bits(pb, 17,
+ s->params[w].average_maxrgb.num * rgb_den / s->params[w].average_maxrgb.den);
+ put_bits(pb, 4, s->params[w].num_distribution_maxrgb_percentiles);
+
+ for (i = 0; i < s->params[w].num_distribution_maxrgb_percentiles; i++) {
+ put_bits(pb, 7, s->params[w].distribution_maxrgb[i].percentage);
+ put_bits(pb, 17,
+ s->params[w].distribution_maxrgb[i].percentile.num * rgb_den / s->params[w].distribution_maxrgb[i].percentile.den);
+ }
+ put_bits(pb, 10,
+ s->params[w].fraction_bright_pixels.num * fraction_pixel_den / s->params[w].fraction_bright_pixels.den);
+ }
+ put_bits(pb, 1, s->mastering_display_actual_peak_luminance_flag);
+ if (s->mastering_display_actual_peak_luminance_flag) {
+ int rows, cols;
+ rows = s->num_rows_mastering_display_actual_peak_luminance;
+ cols = s->num_cols_mastering_display_actual_peak_luminance;
+ put_bits(pb, 5, rows);
+ put_bits(pb, 5, cols);
+ for (i = 0; i < rows; i++) {
+ for (j = 0; j < cols; j++) {
+ put_bits(
+ pb, 4,
+ s->mastering_display_actual_peak_luminance[i][j].num * peak_luminance_den / s->mastering_display_actual_peak_luminance[i][j].den);
+ }
+ }
+ }
+
+ for (w = 0; w < s->num_windows; w++) {
+ put_bits(pb, 1, s->params[w].tone_mapping_flag);
+ if (s->params[w].tone_mapping_flag) {
+ put_bits(pb, 12,
+ s->params[w].knee_point_x.num * knee_point_den / s->params[w].knee_point_x.den);
+ put_bits(pb, 12,
+ s->params[w].knee_point_y.num * knee_point_den / s->params[w].knee_point_y.den);
+ put_bits(pb, 4, s->params[w].num_bezier_curve_anchors);
+ for (i = 0; i < s->params[w].num_bezier_curve_anchors; i++) {
+ put_bits(pb, 10,
+ s->params[w].bezier_curve_anchors[i].num * bezier_anchor_den / s->params[w].bezier_curve_anchors[i].den);
+ }
+ }
+ put_bits(pb, 1, s->params[w].color_saturation_mapping_flag);
+ if (s->params[w].color_saturation_mapping_flag)
+ put_bits(pb, 6,
+ s->params[w].color_saturation_weight.num * saturation_weight_den / s->params[w].color_saturation_weight.den);
+ }
+ flush_put_bits(pb);
+ return 0;
+}
diff --git a/libavcodec/dynamic_hdr10_plus.h b/libavcodec/dynamic_hdr10_plus.h
index cd7acf0432..dafe548d2d 100644
--- a/libavcodec/dynamic_hdr10_plus.h
+++ b/libavcodec/dynamic_hdr10_plus.h
@@ -29,7 +29,38 @@
*
* @return 0 if succeed. Otherwise, returns the appropriate AVERROR.
*/
-int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t *data,
- int size);
+int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus* s, const uint8_t* data,
+ int size);
+
+/**
+ * Parse the user data registered ITU-T T.35 with header to AVDynamicHDRPlus. At first check
+ * the header if the provider code is SMPTE-2094-40. Then will parse the data to AVDynamicHDRPlus.
+ * @param s A pointer containing the decoded AVDynamicHDRPlus structure.
+ * @param data The byte array containing the raw ITU-T T.35 data with header.
+ * @param size Size of the data array in bytes.
+ *
+ * @return 0 if succeed. Otherwise, returns the appropriate AVERROR.
+ */
+int ff_parse_full_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus* s, const uint8_t* data,
+ int size);
+
+/**
+ * Get the size of buffer required to save the encoded bit stream of
+ * AVDynamicHDRPlus in the form of the user data registered ITU-T T.35.
+ * @param s The AVDynamicHDRPlus structure.
+ *
+ * @return The size of bit stream required for encoding. 0 if the data is invalid.
+ */
+int ff_itu_t_t35_buffer_size(const AVDynamicHDRPlus* s);
+
+/**
+ * Encode and write AVDynamicHDRPlus to the user data registered ITU-T T.3 with header (containing the provider code).
+ * @param s A pointer containing the AVDynamicHDRPlus structure.
+ * @param data The byte array containing the raw ITU-T T.35 data with header.
+ * @param size The size of the raw ITU-T T.35 data.
+ *
+ * @return 0 if succeed. Otherwise, returns the appropriate AVERROR.
+ */
+int ff_write_dynamic_hdr10_plus_to_full_itu_t_t35(const AVDynamicHDRPlus* s, uint8_t** data, size_t* size);
#endif /* AVCODEC_DYNAMIC_HDR10_PLUS_H */
diff --git a/libavformat/matroska.h b/libavformat/matroska.h
index 2d04a6838b..37c60cccf7 100644
--- a/libavformat/matroska.h
+++ b/libavformat/matroska.h
@@ -351,6 +351,11 @@ typedef enum {
MATROSKA_VIDEO_PROJECTION_TYPE_MESH = 3,
} MatroskaVideoProjectionType;
+typedef enum {
+ MATROSKA_BLOCK_ADD_ID_DEFAULT = 0,
+ MATROSKA_BLOCK_ADD_ID_DYNAMIC_HDR10_PLUS = 4,
+} MatroskaBlockAddID;
+
/*
* Matroska Codec IDs, strings
*/
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index fdfcc86aeb..e8860cc214 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -46,6 +46,7 @@
#include "libavutil/spherical.h"
#include "libavcodec/bytestream.h"
+#include "libavcodec/dynamic_hdr10_plus.h"
#include "libavcodec/flac.h"
#include "libavcodec/mpeg4audio.h"
#include "libavcodec/packet_internal.h"
@@ -3528,15 +3529,28 @@ static int matroska_parse_frame(MatroskaDemuxContext *matroska,
pkt->stream_index = st->index;
if (additional_size > 0) {
- uint8_t *side_data = av_packet_new_side_data(pkt,
- AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
- additional_size + 8);
- if (!side_data) {
- av_packet_unref(pkt);
- return AVERROR(ENOMEM);
+ if (additional_id == MATROSKA_BLOCK_ADD_ID_DYNAMIC_HDR10_PLUS) {
+ AVDynamicHDRPlus hdr10_plus;
+ if (!ff_parse_full_itu_t_t35_to_dynamic_hdr10_plus(&hdr10_plus, additional, additional_size)) {
+ uint8_t *side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_DYNAMIC_HDR10_PLUS, sizeof(hdr10_plus));
+ if (!side_data) {
+ av_packet_unref(pkt);
+ av_free(pkt);
+ return AVERROR(ENOMEM);
+ }
+ memcpy(side_data, (uint8_t*)(&hdr10_plus), sizeof(hdr10_plus));
+ }
+ } else {
+ uint8_t *side_data = av_packet_new_side_data(pkt,
+ AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
+ additional_size + 8);
+ if (!side_data) {
+ av_packet_unref(pkt);
+ return AVERROR(ENOMEM);
+ }
+ AV_WB64(side_data, additional_id);
+ memcpy(side_data + 8, additional, additional_size);
}
- AV_WB64(side_data, additional_id);
- memcpy(side_data + 8, additional, additional_size);
}
if (discard_padding) {
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 899a3388cd..832d837ed7 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -51,6 +51,7 @@
#include "libavutil/samplefmt.h"
#include "libavutil/stereo3d.h"
+#include "libavcodec/dynamic_hdr10_plus.h"
#include "libavcodec/xiph.h"
#include "libavcodec/mpeg4audio.h"
@@ -2029,14 +2030,15 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb,
MatroskaMuxContext *mkv = s->priv_data;
AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
mkv_track *track = &mkv->tracks[pkt->stream_index];
- uint8_t *data = NULL, *side_data = NULL;
- size_t side_data_size;
+ uint8_t *data = NULL, *side_data = NULL, *hdr10_plus_itu_t_t35 = NULL;
+ size_t side_data_size, hdr10_plus_itu_t_t35_size = 0;
int err = 0, offset = 0, size = pkt->size;
int64_t ts = track->write_dts ? pkt->dts : pkt->pts;
uint64_t additional_id;
int64_t discard_padding = 0;
unsigned track_number = track->track_num;
ebml_master block_group, block_additions, block_more;
+ int use_blockgroup = 0;
ts += track->ts_offset;
@@ -2084,13 +2086,18 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb,
(AVRational){1, par->sample_rate},
(AVRational){1, 1000000000});
}
-
side_data = av_packet_get_side_data(pkt,
+ AV_PKT_DATA_DYNAMIC_HDR10_PLUS,
+ &side_data_size);
+ if (side_data && side_data_size > 0)
+ ff_write_dynamic_hdr10_plus_to_full_itu_t_t35((AVDynamicHDRPlus*)side_data, &hdr10_plus_itu_t_t35, &hdr10_plus_itu_t_t35_size);
+
+ side_data = av_packet_get_side_data(pkt,
AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
&side_data_size);
if (side_data) {
- // Only the Codec-specific BlockMore (id == 1) is currently supported.
- if (side_data_size < 8 || (additional_id = AV_RB64(side_data)) != 1) {
+ // Only the Codec-specific BlockMore (id == 1) and HDR10+ BlockMore (id == 4) are currently supported.
+ if (side_data_size < 8 || (additional_id = AV_RB64(side_data)) != 1 || !hdr10_plus_itu_t_t35_size) {
side_data_size = 0;
} else {
side_data += 8;
@@ -2098,7 +2105,8 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb,
}
}
- if (side_data_size || discard_padding) {
+ use_blockgroup = hdr10_plus_itu_t_t35_size || side_data_size || discard_padding;
+ if (use_blockgroup) {
block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
blockid = MATROSKA_ID_BLOCK;
}
@@ -2119,18 +2127,27 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb,
if (discard_padding)
put_ebml_sint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding);
- if (side_data_size) {
+ if (side_data_size || hdr10_plus_itu_t_t35_size) {
block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
- block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
- /* Until dbc50f8a our demuxer used a wrong default value
- * of BlockAddID, so we write it unconditionally. */
- put_ebml_uint (pb, MATROSKA_ID_BLOCKADDID, additional_id);
- put_ebml_binary(pb, MATROSKA_ID_BLOCKADDITIONAL,
- side_data, side_data_size);
- end_ebml_master(pb, block_more);
+ if (side_data_size) {
+ block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
+ /* Until dbc50f8a our demuxer used a wrong default value
+ * of BlockAddID, so we write it unconditionally. */
+ put_ebml_uint (pb, MATROSKA_ID_BLOCKADDID, additional_id);
+ put_ebml_binary(pb, MATROSKA_ID_BLOCKADDITIONAL,
+ side_data, side_data_size);
+ end_ebml_master(pb, block_more);
+ }
+ if(hdr10_plus_itu_t_t35_size) {
+ block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
+ put_ebml_uint(pb, MATROSKA_ID_BLOCKADDID, MATROSKA_BLOCK_ADD_ID_DYNAMIC_HDR10_PLUS);
+ put_ebml_binary(pb, MATROSKA_ID_BLOCKADDITIONAL,
+ hdr10_plus_itu_t_t35, hdr10_plus_itu_t_t35_size);
+ end_ebml_master(pb, block_more);
+ }
end_ebml_master(pb, block_additions);
}
- if (side_data_size || discard_padding)
+ if (use_blockgroup)
end_ebml_master(pb, block_group);
return 0;
diff --git a/tests/fate/matroska.mak b/tests/fate/matroska.mak
index b57765280a..f27b731fa1 100644
--- a/tests/fate/matroska.mak
+++ b/tests/fate/matroska.mak
@@ -75,6 +75,12 @@ FATE_MATROSKA_FFMPEG_FFPROBE-$(call ALLYES, MATROSKA_DEMUXER MATROSKA_MUXER \
+= fate-matroska-spherical-mono-remux
fate-matroska-spherical-mono-remux: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/spherical.mkv matroska "-map 0 -map 0 -c copy -disposition:0 -default+forced -disposition:1 -default -default_mode passthrough -color_primaries:1 bt709 -color_trc:1 smpte170m -colorspace:1 bt2020c -color_range:1 pc" "-map 0 -c copy -t 0" "" "-show_entries stream_side_data_list:stream_disposition=default,forced:stream=color_range,color_space,color_primaries,color_transfer"
+# The input file of the following test contains HDR10+ metadata and so this
+# test tests correct encoding and decoding HDR10+ for VP9/MKV.
+FATE_MATROSKA_FFMPEG_FFPROBE-$(call ALLYES, MATROSKA_DEMUXER MATROSKA_MUXER) \
+ += fate-matroska-hdr10-plus-metadata
+fate-matroska-hdr10-plus-metadata: CMD = transcode matroska $(TARGET_SAMPLES)/mkv/hdr10_plus_vp9_sample.mkv matroska "-map 0 -map 0 -c copy -pixel_format yuv420p10le" "" "" "-show_frames"
+
# The input file of the following test contains Content Light Level as well as
# Mastering Display Metadata and so this test tests correct muxing and demuxing
# of these. It furthermore also tests that this data is correctly propagated
diff --git a/tests/ref/fate/matroska-hdr10-plus-metadata b/tests/ref/fate/matroska-hdr10-plus-metadata
new file mode 100644
index 0000000000..cbcfc0f41f
--- /dev/null
+++ b/tests/ref/fate/matroska-hdr10-plus-metadata
@@ -0,0 +1,150 @@
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 1280x720
+#sar 0: 1/1
+0, 0, 0, 1, 2764800, 0xf2617bf2
+[FRAME]
+media_type=video
+stream_index=0
+key_frame=1
+pkt_pts=0
+pkt_pts_time=0.000000
+pkt_dts=0
+pkt_dts_time=0.000000
+best_effort_timestamp=0
+best_effort_timestamp_time=0.000000
+pkt_duration=40
+pkt_duration_time=0.040000
+pkt_pos=582
+pkt_size=13350
+width=1280
+height=720
+pix_fmt=yuv420p10le
+sample_aspect_ratio=1:1
+pict_type=I
+coded_picture_number=0
+display_picture_number=0
+interlaced_frame=0
+top_field_first=0
+repeat_pict=0
+color_range=tv
+color_space=unknown
+color_primaries=unknown
+color_transfer=unknown
+chroma_location=unspecified
+[SIDE_DATA]
+side_data_type=HDR Dynamic Metadata SMPTE2094-40 (HDR10+)
+application version=1
+num_windows=1
+targeted_system_display_maximum_luminance=400/1
+maxscl=3340/100000
+maxscl=2870/100000
+maxscl=2720/100000
+average_maxrgb=510/100000
+num_distribution_maxrgb_percentiles=9
+distribution_maxrgb_percentage=1
+distribution_maxrgb_percentile=30/100000
+distribution_maxrgb_percentage=5
+distribution_maxrgb_percentile=2940/100000
+distribution_maxrgb_percentage=10
+distribution_maxrgb_percentile=255/100000
+distribution_maxrgb_percentage=25
+distribution_maxrgb_percentile=70/100000
+distribution_maxrgb_percentage=50
+distribution_maxrgb_percentile=1340/100000
+distribution_maxrgb_percentage=75
+distribution_maxrgb_percentile=1600/100000
+distribution_maxrgb_percentage=90
+distribution_maxrgb_percentile=1850/100000
+distribution_maxrgb_percentage=95
+distribution_maxrgb_percentile=1950/100000
+distribution_maxrgb_percentage=99
+distribution_maxrgb_percentile=2940/100000
+fraction_bright_pixels=1/1000
+knee_point_x=0/4095
+knee_point_y=0/4095
+num_bezier_curve_anchors=9
+bezier_curve_anchors=102/1023
+bezier_curve_anchors=205/1023
+bezier_curve_anchors=307/1023
+bezier_curve_anchors=410/1023
+bezier_curve_anchors=512/1023
+bezier_curve_anchors=614/1023
+bezier_curve_anchors=717/1023
+bezier_curve_anchors=819/1023
+bezier_curve_anchors=922/1023
+[/SIDE_DATA]
+[/FRAME]
+[FRAME]
+media_type=video
+stream_index=1
+key_frame=1
+pkt_pts=0
+pkt_pts_time=0.000000
+pkt_dts=0
+pkt_dts_time=0.000000
+best_effort_timestamp=0
+best_effort_timestamp_time=0.000000
+pkt_duration=40
+pkt_duration_time=0.040000
+pkt_pos=14051
+pkt_size=13350
+width=1280
+height=720
+pix_fmt=yuv420p10le
+sample_aspect_ratio=1:1
+pict_type=I
+coded_picture_number=0
+display_picture_number=0
+interlaced_frame=0
+top_field_first=0
+repeat_pict=0
+color_range=tv
+color_space=unknown
+color_primaries=unknown
+color_transfer=unknown
+chroma_location=unspecified
+[SIDE_DATA]
+side_data_type=HDR Dynamic Metadata SMPTE2094-40 (HDR10+)
+application version=1
+num_windows=1
+targeted_system_display_maximum_luminance=400/1
+maxscl=3340/100000
+maxscl=2870/100000
+maxscl=2720/100000
+average_maxrgb=510/100000
+num_distribution_maxrgb_percentiles=9
+distribution_maxrgb_percentage=1
+distribution_maxrgb_percentile=30/100000
+distribution_maxrgb_percentage=5
+distribution_maxrgb_percentile=2940/100000
+distribution_maxrgb_percentage=10
+distribution_maxrgb_percentile=255/100000
+distribution_maxrgb_percentage=25
+distribution_maxrgb_percentile=70/100000
+distribution_maxrgb_percentage=50
+distribution_maxrgb_percentile=1340/100000
+distribution_maxrgb_percentage=75
+distribution_maxrgb_percentile=1600/100000
+distribution_maxrgb_percentage=90
+distribution_maxrgb_percentile=1850/100000
+distribution_maxrgb_percentage=95
+distribution_maxrgb_percentile=1950/100000
+distribution_maxrgb_percentage=99
+distribution_maxrgb_percentile=2940/100000
+fraction_bright_pixels=1/1000
+knee_point_x=0/4095
+knee_point_y=0/4095
+num_bezier_curve_anchors=9
+bezier_curve_anchors=102/1023
+bezier_curve_anchors=205/1023
+bezier_curve_anchors=307/1023
+bezier_curve_anchors=410/1023
+bezier_curve_anchors=512/1023
+bezier_curve_anchors=614/1023
+bezier_curve_anchors=717/1023
+bezier_curve_anchors=819/1023
+bezier_curve_anchors=922/1023
+[/SIDE_DATA]
+[/FRAME]
--
2.33.0.rc1.237.g0d66db33f3-goog
3
6
This patch adds specializations for hscale for filterSize == 4 and 8 and
converts the existing implementation for the X8 version. For the old code, now
used for the X8 version, it improves the efficiency of the final summations by
reducing 11 instructions to 7.
ff_hscale8to15_8_neon is mostly unchanged from the original except for a few
changes.
- The loads for the filter data were consolidated into a single 64 byte ld1
instruction.
- The final summations were improved.
- The inner loop on filterSize was completely removed
ff_hscale8to15_4_neon is a complete rewrite. Since the main bottleneck here is
loading the data from src, this data is loaded a whole block ahead and stored
back to the stack to be loaded again with ld4. This arranges the data for most
efficient use of the vector instructions and removes the need for completion
adds at the end. The number of iterations of the C per iteration of the assembly
is increased from 4 to 8, but because of the prefetching, it can only be used
when dstW is >= 16.
This improves speed by 26% on Graviton 2 (Neoverse N1)
ffmpeg -nostats -f lavfi -i testsrc2=4k:d=2 -vf bench=start,scale=1024x1024,bench=stop -f null -
before: t:0.001796 avg:0.001839 max:0.002756 min:0.001733
after: t:0.001690 avg:0.001352 max:0.002171 min:0.001292
In direct micro benchmarks I wrote the benefit is more dramatic when filterSize == 4.
| (seconds) | c6g | |
| ----------- | ------- | ----- |
| filterSize | 4 | 8 |
| original | 7.554 | 7.621 |
| optimized | 3.736 | 7.054 |
| improvement | 102.19% | 8.04% |
Signed-off-by: Jonathan Swinney <jswinney(a)amazon.com>
---
libswscale/aarch64/hscale.S | 263 +++++++++++++++++++++++++++++++++--
libswscale/aarch64/swscale.c | 41 ++++--
libswscale/utils.c | 2 +-
3 files changed, 284 insertions(+), 22 deletions(-)
diff --git a/libswscale/aarch64/hscale.S b/libswscale/aarch64/hscale.S
index af55ffe2b7..a934653a46 100644
--- a/libswscale/aarch64/hscale.S
+++ b/libswscale/aarch64/hscale.S
@@ -1,5 +1,7 @@
/*
* Copyright (c) 2016 Clément Bœsch <clement stupeflix.com>
+ * Copyright (c) 2019-2021 Sebastian Pop <spop(a)amazon.com>
+ * Copyright (c) 2022 Jonathan Swinney <jswinney(a)amazon.com>
*
* This file is part of FFmpeg.
*
@@ -20,7 +22,25 @@
#include "libavutil/aarch64/asm.S"
-function ff_hscale_8_to_15_neon, export=1
+/*
+;-----------------------------------------------------------------------------
+; horizontal line scaling
+;
+; void hscale<source_width>to<intermediate_nbits>_<filterSize>_<opt>
+; (SwsContext *c, int{16,32}_t *dst,
+; int dstW, const uint{8,16}_t *src,
+; const int16_t *filter,
+; const int32_t *filterPos, int filterSize);
+;
+; Scale one horizontal line. Input is either 8-bit width or 16-bit width
+; ($source_width can be either 8, 9, 10 or 16, difference is whether we have to
+; downscale before multiplying). Filter is 14 bits. Output is either 15 bits
+; (in int16_t) or 19 bits (in int32_t), as given in $intermediate_nbits. Each
+; output pixel is generated from $filterSize input pixels, the position of
+; the first pixel is given in filterPos[nOutputPixel].
+;----------------------------------------------------------------------------- */
+
+function ff_hscale8to15_X8_neon, export=1
sbfiz x7, x6, #1, #32 // filterSize*2 (*2 because int16)
1: ldr w8, [x5], #4 // filterPos[idx]
ldr w0, [x5], #4 // filterPos[idx + 1]
@@ -61,20 +81,239 @@ function ff_hscale_8_to_15_neon, export=1
smlal v3.4S, v18.4H, v19.4H // v3 accumulates srcp[filterPos[3] + {0..3}] * filter[{0..3}]
smlal2 v3.4S, v18.8H, v19.8H // v3 accumulates srcp[filterPos[3] + {4..7}] * filter[{4..7}]
b.gt 2b // inner loop if filterSize not consumed completely
- addp v0.4S, v0.4S, v0.4S // part0 horizontal pair adding
- addp v1.4S, v1.4S, v1.4S // part1 horizontal pair adding
- addp v2.4S, v2.4S, v2.4S // part2 horizontal pair adding
- addp v3.4S, v3.4S, v3.4S // part3 horizontal pair adding
- addp v0.4S, v0.4S, v0.4S // part0 horizontal pair adding
- addp v1.4S, v1.4S, v1.4S // part1 horizontal pair adding
- addp v2.4S, v2.4S, v2.4S // part2 horizontal pair adding
- addp v3.4S, v3.4S, v3.4S // part3 horizontal pair adding
- zip1 v0.4S, v0.4S, v1.4S // part01 = zip values from part0 and part1
- zip1 v2.4S, v2.4S, v3.4S // part23 = zip values from part2 and part3
- mov v0.d[1], v2.d[0] // part0123 = zip values from part01 and part23
+ uzp1 v4.4S, v0.4S, v1.4S // unzip low parts 0 and 1
+ uzp2 v5.4S, v0.4S, v1.4S // unzip high parts 0 and 1
+ uzp1 v6.4S, v2.4S, v3.4S // unzip low parts 2 and 3
+ uzp2 v7.4S, v2.4S, v3.4S // unzip high parts 2 and 3
+ add v16.4S, v4.4S, v5.4S // add half of each of part 0 and 1
+ add v17.4S, v6.4S, v7.4S // add half of each of part 2 and 3
+ addp v0.4S, v16.4S, v17.4S // pairwise add to complete half adds in earlier steps
subs w2, w2, #4 // dstW -= 4
sqshrn v0.4H, v0.4S, #7 // shift and clip the 2x16-bit final values
st1 {v0.4H}, [x1], #8 // write to destination part0123
b.gt 1b // loop until end of line
ret
endfunc
+
+
+function ff_hscale8to15_8_neon, export=1
+// x0 SwsContext *c (not used)
+// x1 int16_t *dst
+// x2 int dstW
+// x3 const uint8_t *src
+// x4 const int16_t *filter
+// x5 const int32_t *filterPos
+// x6 int filterSize
+// x8-x11 filterPos values
+
+// v0-v3 multiply add accumulators
+// v4-v7 filter data, temp for final horizontal sum
+// v16-v19 src data
+1:
+ ld1 {v4.8H, v5.8H, v6.8H, v7.8H}, [x4], #64 // load filter[idx=0..3, j=0..7]
+ ldp w8, w9, [x5] // filterPos[idx + 0], [idx + 1]
+ ldp w10, w11, [x5, 8] // filterPos[idx + 2], [idx + 3]
+ movi v0.2D, #0 // val sum part 1 (for dst[0])
+ movi v1.2D, #0 // val sum part 2 (for dst[1])
+ add x5, x5, #16 // increment filterPos
+
+ add x8, x3, w8, UXTW // srcp + filterPos[0]
+ add x9, x3, w9, UXTW // srcp + filterPos[1]
+ add x10, x3, w10, UXTW // srcp + filterPos[2]
+ add x11, x3, w11, UXTW // srcp + filterPos[3]
+
+ ld1 {v16.8B}, [x8], #8 // srcp[filterPos[0] + {0..7}]
+ ld1 {v17.8B}, [x9], #8 // srcp[filterPos[1] + {0..7}]
+
+ movi v2.2D, #0 // val sum part 3 (for dst[2])
+ movi v3.2D, #0 // val sum part 4 (for dst[3])
+
+ uxtl v16.8H, v16.8B // unpack part 1 to 16-bit
+ uxtl v17.8H, v17.8B // unpack part 2 to 16-bit
+
+ smlal v0.4S, v16.4H, v4.4H // v0 accumulates srcp[filterPos[0] + {0..3}] * filter[{0..3}]
+ smlal v1.4S, v17.4H, v5.4H // v1 accumulates srcp[filterPos[1] + {0..3}] * filter[{0..3}]
+
+ ld1 {v18.8B}, [x10], #8 // srcp[filterPos[2] + {0..7}]
+ ld1 {v19.8B}, [x11], #8 // srcp[filterPos[3] + {0..7}]
+
+ smlal2 v0.4S, v16.8H, v4.8H // v0 accumulates srcp[filterPos[0] + {4..7}] * filter[{4..7}]
+ smlal2 v1.4S, v17.8H, v5.8H // v1 accumulates srcp[filterPos[1] + {4..7}] * filter[{4..7}]
+
+ uxtl v18.8H, v18.8B // unpack part 3 to 16-bit
+ uxtl v19.8H, v19.8B // unpack part 4 to 16-bit
+
+ smlal v2.4S, v18.4H, v6.4H // v2 accumulates srcp[filterPos[2] + {0..3}] * filter[{0..3}]
+ smlal v3.4S, v19.4H, v7.4H // v3 accumulates srcp[filterPos[3] + {0..3}] * filter[{0..3}]
+
+ smlal2 v2.4S, v18.8H, v6.8H // v2 accumulates srcp[filterPos[2] + {4..7}] * filter[{4..7}]
+ smlal2 v3.4S, v19.8H, v7.8H // v3 accumulates srcp[filterPos[3] + {4..7}] * filter[{4..7}]
+
+ uzp1 v4.4S, v0.4S, v1.4S // unzip low parts 0 and 1
+ uzp2 v5.4S, v0.4S, v1.4S // unzip high parts 0 and 1
+ uzp1 v6.4S, v2.4S, v3.4S // unzip low parts 2 and 3
+ uzp2 v7.4S, v2.4S, v3.4S // unzip high parts 2 and 3
+
+ add v0.4S, v4.4S, v5.4S // add half of each of part 0 and 1
+ add v1.4S, v6.4S, v7.4S // add half of each of part 2 and 3
+
+ addp v4.4S, v0.4S, v1.4S // pairwise add to complete half adds in earlier steps
+
+ subs w2, w2, #4 // dstW -= 4
+ sqshrn v0.4H, v4.4S, #7 // shift and clip the 2x16-bit final values
+ st1 {v0.4H}, [x1], #8 // write to destination part0123
+ b.gt 1b // loop until end of line
+ ret
+endfunc
+
+function ff_hscale8to15_4_neon, export=1
+// x0 SwsContext *c (not used)
+// x1 int16_t *dst
+// x2 int dstW
+// x3 const uint8_t *src
+// x4 const int16_t *filter
+// x5 const int32_t *filterPos
+// x6 int filterSize
+// x8-x15 registers for gathering src data
+
+// v0 madd accumulator 4S
+// v1-v4 filter values (16 bit) 8H
+// v5 madd accumulator 4S
+// v16-v19 src values (8 bit) 8B
+
+// This implementation has 4 sections:
+// 1. Prefetch src data
+// 2. Interleaved prefetching src data and madd
+// 3. Complete madd
+// 4. Complete remaining iterations when dstW % 8 != 0
+
+ add sp, sp, #-32 // allocate 32 bytes on the stack
+ cmp w2, #16 // if dstW <16, skip to the last block used for wrapping up
+ b.lt 2f
+
+ // load 8 values from filterPos to be used as offsets into src
+ ldp w8, w9, [x5] // filterPos[idx + 0], [idx + 1]
+ ldp w10, w11, [x5, 8] // filterPos[idx + 2], [idx + 3]
+ ldp w12, w13, [x5, 16] // filterPos[idx + 4], [idx + 5]
+ ldp w14, w15, [x5, 24] // filterPos[idx + 6], [idx + 7]
+ add x5, x5, #32 // advance filterPos
+
+ // gather random access data from src into contiguous memory
+ ldr w8, [x3, w8, UXTW] // src[filterPos[idx + 0]][0..3]
+ ldr w9, [x3, w9, UXTW] // src[filterPos[idx + 1]][0..3]
+ ldr w10, [x3, w10, UXTW] // src[filterPos[idx + 2]][0..3]
+ ldr w11, [x3, w11, UXTW] // src[filterPos[idx + 3]][0..3]
+ ldr w12, [x3, w12, UXTW] // src[filterPos[idx + 4]][0..3]
+ ldr w13, [x3, w13, UXTW] // src[filterPos[idx + 5]][0..3]
+ ldr w14, [x3, w14, UXTW] // src[filterPos[idx + 6]][0..3]
+ ldr w15, [x3, w15, UXTW] // src[filterPos[idx + 7]][0..3]
+ stp w8, w9, [sp] // *scratch_mem = { src[filterPos[idx + 0]][0..3], src[filterPos[idx + 1]][0..3] }
+ stp w10, w11, [sp, 8] // *scratch_mem = { src[filterPos[idx + 2]][0..3], src[filterPos[idx + 3]][0..3] }
+ stp w12, w13, [sp, 16] // *scratch_mem = { src[filterPos[idx + 4]][0..3], src[filterPos[idx + 5]][0..3] }
+ stp w14, w15, [sp, 24] // *scratch_mem = { src[filterPos[idx + 6]][0..3], src[filterPos[idx + 7]][0..3] }
+
+1:
+ ld4 {v16.8B, v17.8B, v18.8B, v19.8B}, [sp] // transpose 8 bytes each from src into 4 registers
+
+ // load 8 values from filterPos to be used as offsets into src
+ ldp w8, w9, [x5] // filterPos[idx + 0][0..3], [idx + 1][0..3], next iteration
+ ldp w10, w11, [x5, 8] // filterPos[idx + 2][0..3], [idx + 3][0..3], next iteration
+ ldp w12, w13, [x5, 16] // filterPos[idx + 4][0..3], [idx + 5][0..3], next iteration
+ ldp w14, w15, [x5, 24] // filterPos[idx + 6][0..3], [idx + 7][0..3], next iteration
+
+ movi v0.2D, #0 // Clear madd accumulator for idx 0..3
+ movi v5.2D, #0 // Clear madd accumulator for idx 4..7
+
+ ld4 {v1.8H, v2.8H, v3.8H, v4.8H}, [x4], #64 // load filter idx + 0..7
+
+ add x5, x5, #32 // advance filterPos
+
+ // interleaved SIMD and prefetching intended to keep ld/st and vector pipelines busy
+ uxtl v16.8H, v16.8B // unsigned extend long, covert src data to 16-bit
+ uxtl v17.8H, v17.8B // unsigned extend long, covert src data to 16-bit
+ ldr w8, [x3, w8, UXTW] // src[filterPos[idx + 0]], next iteration
+ ldr w9, [x3, w9, UXTW] // src[filterPos[idx + 1]], next iteration
+ uxtl v18.8H, v18.8B // unsigned extend long, covert src data to 16-bit
+ uxtl v19.8H, v19.8B // unsigned extend long, covert src data to 16-bit
+ ldr w10, [x3, w10, UXTW] // src[filterPos[idx + 2]], next iteration
+ ldr w11, [x3, w11, UXTW] // src[filterPos[idx + 3]], next iteration
+
+ smlal v0.4S, v1.4H, v16.4H // multiply accumulate inner loop j = 0, idx = 0..3
+ smlal v0.4S, v2.4H, v17.4H // multiply accumulate inner loop j = 1, idx = 0..3
+ ldr w12, [x3, w12, UXTW] // src[filterPos[idx + 4]], next iteration
+ ldr w13, [x3, w13, UXTW] // src[filterPos[idx + 5]], next iteration
+ smlal v0.4S, v3.4H, v18.4H // multiply accumulate inner loop j = 2, idx = 0..3
+ smlal v0.4S, v4.4H, v19.4H // multiply accumulate inner loop j = 3, idx = 0..3
+ ldr w14, [x3, w14, UXTW] // src[filterPos[idx + 6]], next iteration
+ ldr w15, [x3, w15, UXTW] // src[filterPos[idx + 7]], next iteration
+
+ smlal2 v5.4S, v1.8H, v16.8H // multiply accumulate inner loop j = 0, idx = 4..7
+ smlal2 v5.4S, v2.8H, v17.8H // multiply accumulate inner loop j = 1, idx = 4..7
+ stp w8, w9, [sp] // *scratch_mem = { src[filterPos[idx + 0]][0..3], src[filterPos[idx + 1]][0..3] }
+ stp w10, w11, [sp, 8] // *scratch_mem = { src[filterPos[idx + 2]][0..3], src[filterPos[idx + 3]][0..3] }
+ smlal2 v5.4S, v3.8H, v18.8H // multiply accumulate inner loop j = 2, idx = 4..7
+ smlal2 v5.4S, v4.8H, v19.8H // multiply accumulate inner loop j = 3, idx = 4..7
+ stp w12, w13, [sp, 16] // *scratch_mem = { src[filterPos[idx + 4]][0..3], src[filterPos[idx + 5]][0..3] }
+ stp w14, w15, [sp, 24] // *scratch_mem = { src[filterPos[idx + 6]][0..3], src[filterPos[idx + 7]][0..3] }
+
+ sub w2, w2, #8 // dstW -= 8
+ sqshrn v0.4H, v0.4S, #7 // shift and clip the 2x16-bit final values
+ sqshrn v1.4H, v5.4S, #7 // shift and clip the 2x16-bit final values
+ st1 {v0.4H, v1.4H}, [x1], #16 // write to dst[idx + 0..7]
+ cmp w2, #16 // continue on main loop if there are at least 16 iterations left
+ b.ge 1b
+
+ // last full iteration
+ ld4 {v16.8B, v17.8B, v18.8B, v19.8B}, [sp]
+ ld4 {v1.8H, v2.8H, v3.8H, v4.8H}, [x4], #64 // load filter idx + 0..7
+
+ movi v0.2D, #0 // Clear madd accumulator for idx 0..3
+ movi v5.2D, #0 // Clear madd accumulator for idx 4..7
+
+ uxtl v16.8H, v16.8B // unsigned extend long, covert src data to 16-bit
+ uxtl v17.8H, v17.8B // unsigned extend long, covert src data to 16-bit
+ uxtl v18.8H, v18.8B // unsigned extend long, covert src data to 16-bit
+ uxtl v19.8H, v19.8B // unsigned extend long, covert src data to 16-bit
+
+ smlal v0.4S, v1.4H, v16.4H // multiply accumulate inner loop j = 0, idx = 0..3
+ smlal v0.4S, v2.4H, v17.4H // multiply accumulate inner loop j = 1, idx = 0..3
+ smlal v0.4S, v3.4H, v18.4H // multiply accumulate inner loop j = 2, idx = 0..3
+ smlal v0.4S, v4.4H, v19.4H // multiply accumulate inner loop j = 3, idx = 0..3
+
+ smlal2 v5.4S, v1.8H, v16.8H // multiply accumulate inner loop j = 0, idx = 4..7
+ smlal2 v5.4S, v2.8H, v17.8H // multiply accumulate inner loop j = 1, idx = 4..7
+ smlal2 v5.4S, v3.8H, v18.8H // multiply accumulate inner loop j = 2, idx = 4..7
+ smlal2 v5.4S, v4.8H, v19.8H // multiply accumulate inner loop j = 3, idx = 4..7
+
+ subs w2, w2, #8 // dstW -= 8
+ sqshrn v0.4H, v0.4S, #7 // shift and clip the 2x16-bit final values
+ sqshrn v1.4H, v5.4S, #7 // shift and clip the 2x16-bit final values
+ st1 {v0.4H, v1.4H}, [x1], #16 // write to dst[idx + 0..7]
+
+ cbnz w2, 2f // if >0 iterations remain, jump to the wrap up section
+
+ add sp, sp, #32 // clean up stack
+ ret
+
+ // finish up when dstW % 8 != 0 or dstW < 16
+2:
+ // load src
+ ldr w8, [x5], #4 // filterPos[i]
+ ldr w9, [x3, w8, UXTW] // src[filterPos[i] + 0..3]
+ ins v5.S[0], w9 // move to simd register
+ // load filter
+ ld1 {v6.4H}, [x4], #8 // filter[filterSize * i + 0..3]
+
+ uxtl v5.8H, v5.8B // unsigned exten long, convert src data to 16-bit
+ smull v0.4S, v5.4H, v6.4H // 4 iterations of src[...] * filter[...]
+ addp v0.4S, v0.4S, v0.4S // accumulate the smull results
+ addp v0.4S, v0.4S, v0.4S // accumulate the smull results
+ sqshrn v0.4H, v0.4S, #7 // shift and clip the 2x16-bit final values
+ mov w10, v0.S[0] // move back to general register (only one value from simd reg is used)
+ strh w10, [x1], #2 // dst[i] = ...
+ sub w2, w2, #1 // dstW--
+ cbnz w2, 2b
+
+ add sp, sp, #32 // clean up stack
+ ret
+endfunc
diff --git a/libswscale/aarch64/swscale.c b/libswscale/aarch64/swscale.c
index 09d0a7130e..2ea4ccb3a6 100644
--- a/libswscale/aarch64/swscale.c
+++ b/libswscale/aarch64/swscale.c
@@ -22,25 +22,48 @@
#include "libswscale/swscale_internal.h"
#include "libavutil/aarch64/cpu.h"
-void ff_hscale_8_to_15_neon(SwsContext *c, int16_t *dst, int dstW,
- const uint8_t *src, const int16_t *filter,
- const int32_t *filterPos, int filterSize);
+#define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
+void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
+ SwsContext *c, int16_t *data, \
+ int dstW, const uint8_t *src, \
+ const int16_t *filter, \
+ const int32_t *filterPos, int filterSize)
+#define SCALE_FUNCS(filter_n, opt) \
+ SCALE_FUNC(filter_n, 8, 15, opt);
+#define ALL_SCALE_FUNCS(opt) \
+ SCALE_FUNCS(4, opt); \
+ SCALE_FUNCS(8, opt); \
+ SCALE_FUNCS(X8, opt)
+
+ALL_SCALE_FUNCS(neon);
void ff_yuv2planeX_8_neon(const int16_t *filter, int filterSize,
const int16_t **src, uint8_t *dest, int dstW,
const uint8_t *dither, int offset);
+#define ASSIGN_SCALE_FUNC2(hscalefn, filtersize, opt) do { \
+ if (c->srcBpc == 8 && c->dstBpc <= 14) { \
+ hscalefn = \
+ ff_hscale8to15_ ## filtersize ## _ ## opt; \
+ } \
+} while (0)
+
+#define ASSIGN_SCALE_FUNC(hscalefn, filtersize, opt) \
+ switch (filtersize) { \
+ case 4: ASSIGN_SCALE_FUNC2(hscalefn, 4, opt); break; \
+ case 8: ASSIGN_SCALE_FUNC2(hscalefn, 8, opt); break; \
+ default: if (filtersize % 8 == 0) \
+ ASSIGN_SCALE_FUNC2(hscalefn, X8, opt); \
+ break; \
+ }
+
av_cold void ff_sws_init_swscale_aarch64(SwsContext *c)
{
int cpu_flags = av_get_cpu_flags();
if (have_neon(cpu_flags)) {
- if (c->srcBpc == 8 && c->dstBpc <= 14 &&
- (c->hLumFilterSize % 8) == 0 &&
- (c->hChrFilterSize % 8) == 0)
- {
- c->hyScale = c->hcScale = ff_hscale_8_to_15_neon;
- }
+ ASSIGN_SCALE_FUNC(c->hyScale, c->hLumFilterSize, neon);
+ ASSIGN_SCALE_FUNC(c->hcScale, c->hChrFilterSize, neon);
if (c->dstBpc == 8) {
c->yuv2planeX = ff_yuv2planeX_8_neon;
}
diff --git a/libswscale/utils.c b/libswscale/utils.c
index c5ea8853d5..2f2b8e73a9 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -1825,7 +1825,7 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
{
const int filterAlign = X86_MMX(cpu_flags) ? 4 :
PPC_ALTIVEC(cpu_flags) ? 8 :
- have_neon(cpu_flags) ? 8 : 1;
+ have_neon(cpu_flags) ? 4 : 1;
if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
&c->hLumFilterSize, c->lumXInc,
--
2.32.0
3
3
[PATCH] avcodec: Pass the HDR10+ metadata to the packet side data in VP9 encoder
by Mohammad Izadi 13 Apr '22
by Mohammad Izadi 13 Apr '22
13 Apr '22
HDR10+ metadata is stored in the bit stream for HEVC. The story is different for VP9 and cannot store the metadata in the bit stream. HDR10+ should be passed to packet side data an stored in the container (mkv) for VP9.
This CL is taking HDR10+ from AVFrame side data in libvpxenc and is passing it to the AVPacket side data.
---
doc/APIchanges | 2 +
libavcodec/avpacket.c | 1 +
libavcodec/decode.c | 1 +
libavcodec/libvpxenc.c | 98 ++++++++++++++++++++++++++++++++++++++++++
libavcodec/packet.h | 8 ++++
libavcodec/version.h | 4 +-
6 files changed, 112 insertions(+), 2 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index c46f4d5304..60995579e5 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,8 @@ libavutil: 2021-04-27
API changes, most recent first:
+2021-05-25 - 8c88a66d3c - lavc 59.2.100 - packet.h
+ Add AV_PKT_DATA_DYNAMIC_HDR10_PLUS
2021-04-27 - cb3ac722f4 - lavc 59.0.100 - avcodec.h
Constified AVCodecParserContext.parser.
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index 7383d12d3e..800bee3489 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -289,6 +289,7 @@ const char *av_packet_side_data_name(enum AVPacketSideDataType type)
case AV_PKT_DATA_ICC_PROFILE: return "ICC Profile";
case AV_PKT_DATA_DOVI_CONF: return "DOVI configuration record";
case AV_PKT_DATA_S12M_TIMECODE: return "SMPTE ST 12-1:2014 timecode";
+ case AV_PKT_DATA_DYNAMIC_HDR10_PLUS: return "HDR10+ Dynamic Metadata (SMPTE 2094-40)";
}
return NULL;
}
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 75bc7ad98e..40f688e40c 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1488,6 +1488,7 @@ int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
{ AV_PKT_DATA_A53_CC, AV_FRAME_DATA_A53_CC },
{ AV_PKT_DATA_ICC_PROFILE, AV_FRAME_DATA_ICC_PROFILE },
{ AV_PKT_DATA_S12M_TIMECODE, AV_FRAME_DATA_S12M_TIMECODE },
+ { AV_PKT_DATA_DYNAMIC_HDR10_PLUS, AV_FRAME_DATA_DYNAMIC_HDR_PLUS },
};
if (IS_EMPTY(pkt) && av_fifo_size(avctx->internal->pkt_props) >= sizeof(*pkt))
diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
index 66bad444d0..cf7e25a4bc 100644
--- a/libavcodec/libvpxenc.c
+++ b/libavcodec/libvpxenc.c
@@ -64,6 +64,11 @@ struct FrameListData {
struct FrameListData *next;
};
+typedef struct FrameHDR10Plus {
+ int64_t pts;
+ AVBufferRef *hdr10_plus;
+} FrameHDR10Plus;
+
typedef struct VPxEncoderContext {
AVClass *class;
struct vpx_codec_ctx encoder;
@@ -121,6 +126,8 @@ typedef struct VPxEncoderContext {
int tune_content;
int corpus_complexity;
int tpl_model;
+ int discard_hdr10_plus;
+ AVFifoBuffer *hdr10_plus_fifo;
/**
* If the driver does not support ROI then warn the first time we
* encounter a frame with ROI side data.
@@ -316,6 +323,55 @@ static av_cold void free_frame_list(struct FrameListData *list)
}
}
+static av_cold int add_hdr10_plus(AVFifoBuffer *fifo, struct FrameHDR10Plus *data)
+{
+ int err = av_fifo_grow(fifo, sizeof(*data));
+ if (err < 0)
+ return err;
+ av_fifo_generic_write(fifo, data, sizeof(*data), NULL);
+ return 0;
+}
+
+static av_cold void free_hdr10_plus(struct FrameHDR10Plus *p)
+{
+ if (!p)
+ return;
+ av_buffer_unref(&p->hdr10_plus);
+ av_free(p);
+}
+
+static av_cold void free_hdr10_plus_fifo(AVFifoBuffer **fifo)
+{
+ FrameHDR10Plus *frame_hdr10_plus = NULL;
+ while (av_fifo_size(*fifo) >= sizeof(FrameHDR10Plus)) {
+ av_fifo_generic_read(*fifo, frame_hdr10_plus, sizeof(FrameHDR10Plus), NULL);
+ free_hdr10_plus(frame_hdr10_plus);
+ }
+ av_fifo_freep(fifo);
+}
+
+static int copy_hdr10_plus_to_pkt(AVFifoBuffer *fifo, AVPacket *pkt)
+{
+ FrameHDR10Plus *frame_hdr10_plus;
+ uint8_t *data;
+ if (av_fifo_size(fifo) < 1)
+ return 0;
+
+ av_fifo_generic_read(fifo, frame_hdr10_plus, sizeof(*frame_hdr10_plus), NULL);
+ if (!frame_hdr10_plus || !pkt || !frame_hdr10_plus->hdr10_plus || frame_hdr10_plus->pts != pkt->pts)
+ return 0;
+
+ data = av_packet_new_side_data(pkt, AV_PKT_DATA_DYNAMIC_HDR10_PLUS, frame_hdr10_plus->hdr10_plus->size);
+ if (!data) {
+ free_hdr10_plus(frame_hdr10_plus);
+ return AVERROR(ENOMEM);
+ }
+ memcpy(data, frame_hdr10_plus->hdr10_plus->data, frame_hdr10_plus->hdr10_plus->size);
+ free_hdr10_plus(frame_hdr10_plus);
+
+ return 0;
+}
+
static av_cold int codecctl_int(AVCodecContext *avctx,
enum vp8e_enc_control_id id, int val)
{
@@ -384,6 +440,7 @@ static av_cold int vpx_free(AVCodecContext *avctx)
av_freep(&ctx->twopass_stats.buf);
av_freep(&avctx->stats_out);
free_frame_list(ctx->coded_frame_list);
+ free_hdr10_plus_fifo(&ctx->hdr10_plus_fifo);
return 0;
}
@@ -835,6 +892,7 @@ static av_cold int vpx_init(AVCodecContext *avctx,
#endif
AVDictionaryEntry* en = NULL;
+ ctx->discard_hdr10_plus = 1;
av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
@@ -851,6 +909,14 @@ static av_cold int vpx_init(AVCodecContext *avctx,
if (avctx->codec_id == AV_CODEC_ID_VP9) {
if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
return AVERROR(EINVAL);
+ // Keep HDR10+ if it has bit depth higher than 8 and
+ // it has PQ trc (SMPTE2084).
+ if (enccfg.g_bit_depth > 8 && avctx->color_trc == AVCOL_TRC_SMPTE2084) {
+ ctx->discard_hdr10_plus = 0;
+ ctx->hdr10_plus_fifo = av_fifo_alloc(sizeof(FrameHDR10Plus));
+ if (!ctx->hdr10_plus_fifo)
+ return AVERROR(ENOMEM);
+ }
}
#endif
@@ -1211,6 +1277,15 @@ static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
AV_WB64(side_data, 1);
memcpy(side_data + 8, cx_frame->buf_alpha, cx_frame->sz_alpha);
}
+ if (cx_frame->frame_number != -1) {
+ VPxContext *ctx = avctx->priv_data;
+ if (!ctx->discard_hdr10_plus) {
+ int err = copy_hdr10_plus_to_pkt(ctx->hdr10_plus_fifo, pkt);
+ if (err < 0)
+ return err;
+ }
+ }
+
return pkt->size;
}
@@ -1618,6 +1693,29 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
vp9_encode_set_roi(avctx, frame->width, frame->height, sd);
}
}
+
+ if (!ctx->discard_hdr10_plus) {
+ AVFrameSideData *hdr10_plus_metadata;
+ // Add HDR10+ metadata to queue.
+ hdr10_plus_metadata = av_frame_get_side_data(frame, AV_FRAME_DATA_DYNAMIC_HDR_PLUS);
+ if (hdr10_plus_metadata) {
+ int err;
+ struct FrameHDR10Plus *data = av_malloc(sizeof(*data));
+ if (!data)
+ return AVERROR(ENOMEM);
+ data->pts = frame->pts;
+ data->hdr10_plus = av_buffer_ref(hdr10_plus_metadata->buf);
+ if (!data->hdr10_plus) {
+ av_freep(&data);
+ return AVERROR(ENOMEM);
+ }
+ err = add_hdr10_plus(ctx->hdr10_plus_fifo, data);
+ if (err < 0) {
+ av_freep(&data);
+ return err;
+ }
+ }
+ }
}
// this is for encoding with preset temporal layering patterns defined in
diff --git a/libavcodec/packet.h b/libavcodec/packet.h
index fad8341c12..a9d3a9b596 100644
--- a/libavcodec/packet.h
+++ b/libavcodec/packet.h
@@ -290,6 +290,14 @@ enum AVPacketSideDataType {
*/
AV_PKT_DATA_S12M_TIMECODE,
+ /**
+ * HDR10+ dynamic metadata associated with a video frame. The metadata is in
+ * the form of the AVDynamicHDRPlus struct and contains
+ * information for color volume transform - application 4 of
+ * SMPTE 2094-40:2016 standard.
+ */
+ AV_PKT_DATA_DYNAMIC_HDR10_PLUS,
+
/**
* The number of side data types.
* This is not part of the public API/ABI in the sense that it may
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 5b1e9e77f3..1288cecebe 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -28,8 +28,8 @@
#include "libavutil/version.h"
#define LIBAVCODEC_VERSION_MAJOR 59
-#define LIBAVCODEC_VERSION_MINOR 1
-#define LIBAVCODEC_VERSION_MICRO 101
+#define LIBAVCODEC_VERSION_MINOR 2
+#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
--
2.32.0.rc1.229.g3e70b5a671-goog
4
28
[PATCH 1/3] lavc/encode: factor audio/video-specific parts out of ff_encode_preinit()
by Anton Khirnov 13 Apr '22
by Anton Khirnov 13 Apr '22
13 Apr '22
---
libavcodec/encode.c | 127 ++++++++++++++++++++++++++------------------
1 file changed, 75 insertions(+), 52 deletions(-)
diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 618be0573d..6543dda5bd 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -406,15 +406,67 @@ int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *
return 0;
}
-int ff_encode_preinit(AVCodecContext *avctx)
+static int encode_preinit_video(AVCodecContext *avctx)
{
+ const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
int i;
- if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
- av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
- return AVERROR(EINVAL);
+ if (avctx->codec->pix_fmts) {
+ for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
+ if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
+ break;
+ if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
+ char buf[128];
+ snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
+ av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
+ (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
+ return AVERROR(EINVAL);
+ }
+ if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
+ avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
+ avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
+ avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
+ avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
+ avctx->color_range = AVCOL_RANGE_JPEG;
+ }
+
+ if ( avctx->bits_per_raw_sample < 0
+ || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
+ av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
+ avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
+ avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
+ }
+ if (avctx->width <= 0 || avctx->height <= 0) {
+ av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
+ return AVERROR(EINVAL);
+ }
+
+ if (avctx->hw_frames_ctx) {
+ AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
+ if (frames_ctx->format != avctx->pix_fmt) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
+ return AVERROR(EINVAL);
+ }
+ if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
+ avctx->sw_pix_fmt != frames_ctx->sw_format) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Mismatching AVCodecContext.sw_pix_fmt (%s) "
+ "and AVHWFramesContext.sw_format (%s)\n",
+ av_get_pix_fmt_name(avctx->sw_pix_fmt),
+ av_get_pix_fmt_name(frames_ctx->sw_format));
+ return AVERROR(EINVAL);
+ }
+ avctx->sw_pix_fmt = frames_ctx->sw_format;
}
+ return 0;
+}
+
+static int encode_preinit_audio(AVCodecContext *avctx)
+{
+ int i;
+
if (avctx->codec->sample_fmts) {
for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
@@ -434,24 +486,6 @@ int ff_encode_preinit(AVCodecContext *avctx)
return AVERROR(EINVAL);
}
}
- if (avctx->codec->pix_fmts) {
- for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
- if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
- break;
- if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
- char buf[128];
- snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
- av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
- (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
- return AVERROR(EINVAL);
- }
- if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
- avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
- avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
- avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
- avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
- avctx->color_range = AVCOL_RANGE_JPEG;
- }
if (avctx->codec->supported_samplerates) {
for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
@@ -500,19 +534,26 @@ int ff_encode_preinit(AVCodecContext *avctx)
avctx->channels);
return AVERROR(EINVAL);
}
- if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
- const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
- if ( avctx->bits_per_raw_sample < 0
- || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
- av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
- avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
- avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
- }
- if (avctx->width <= 0 || avctx->height <= 0) {
- av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
- return AVERROR(EINVAL);
- }
+
+ return 0;
+}
+
+int ff_encode_preinit(AVCodecContext *avctx)
+{
+ int ret = 0;
+
+ if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
+ av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
+ return AVERROR(EINVAL);
}
+
+ switch (avctx->codec_type) {
+ case AVMEDIA_TYPE_VIDEO: ret = encode_preinit_video(avctx); break;
+ case AVMEDIA_TYPE_AUDIO: ret = encode_preinit_audio(avctx); break;
+ }
+ if (ret < 0)
+ return ret;
+
if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
&& avctx->bit_rate>0 && avctx->bit_rate<1000) {
av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
@@ -531,24 +572,6 @@ int ff_encode_preinit(AVCodecContext *avctx)
return AVERROR(EINVAL);
}
- if (avctx->hw_frames_ctx) {
- AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
- if (frames_ctx->format != avctx->pix_fmt) {
- av_log(avctx, AV_LOG_ERROR,
- "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
- return AVERROR(EINVAL);
- }
- if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
- avctx->sw_pix_fmt != frames_ctx->sw_format) {
- av_log(avctx, AV_LOG_ERROR,
- "Mismatching AVCodecContext.sw_pix_fmt (%s) "
- "and AVHWFramesContext.sw_format (%s)\n",
- av_get_pix_fmt_name(avctx->sw_pix_fmt),
- av_get_pix_fmt_name(frames_ctx->sw_format));
- return AVERROR(EINVAL);
- }
- avctx->sw_pix_fmt = frames_ctx->sw_format;
- }
if (avctx->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY)
avctx->internal->intra_only_flag = AV_PKT_FLAG_KEY;
--
2.33.0
3
11
FFmpeg 4.4.1 still supported libva v1, FFmpeg 5.0 does not do that anymore.
If dropping support for libva v1 wasn't an intentional decision, it's easy
to re-enable it.
Ingo
2
6
[PATCH v5 1/7] avcodec/v4l2_context: don't reinit output queue when dynamic resolution change
by Ming Qian 04 Apr '22
by Ming Qian 04 Apr '22
04 Apr '22
in the v4l2 stateful video document, we can see the following
description:
During the resolution change sequence, the OUTPUT queue must remain
streaming. Calling VIDIOC_STREAMOFF() on the OUTPUT queue would
abort the sequence and initiate a seek.
In principle, the OUTPUT queue operates separately from the CAPTURE
queue and this remains true for the duration of the entire
resolution change sequence as well.
so don't reinit the output queue when handling the resolution change
event
Signed-off-by: Ming Qian <ming.qian(a)nxp.com>
---
libavcodec/v4l2_context.c | 27 ++-------------------------
1 file changed, 2 insertions(+), 25 deletions(-)
diff --git a/libavcodec/v4l2_context.c b/libavcodec/v4l2_context.c
index ff1ea8e57b08..dda5157698c3 100644
--- a/libavcodec/v4l2_context.c
+++ b/libavcodec/v4l2_context.c
@@ -162,9 +162,8 @@ static int v4l2_handle_event(V4L2Context *ctx)
{
V4L2m2mContext *s = ctx_to_m2mctx(ctx);
struct v4l2_format cap_fmt = s->capture.format;
- struct v4l2_format out_fmt = s->output.format;
struct v4l2_event evt = { 0 };
- int full_reinit, reinit, ret;
+ int reinit, ret;
ret = ioctl(s->fd, VIDIOC_DQEVENT, &evt);
if (ret < 0) {
@@ -180,25 +179,12 @@ static int v4l2_handle_event(V4L2Context *ctx)
if (evt.type != V4L2_EVENT_SOURCE_CHANGE)
return 0;
- ret = ioctl(s->fd, VIDIOC_G_FMT, &out_fmt);
- if (ret) {
- av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT\n", s->output.name);
- return 0;
- }
-
ret = ioctl(s->fd, VIDIOC_G_FMT, &cap_fmt);
if (ret) {
av_log(logger(ctx), AV_LOG_ERROR, "%s VIDIOC_G_FMT\n", s->capture.name);
return 0;
}
- full_reinit = v4l2_resolution_changed(&s->output, &out_fmt);
- if (full_reinit) {
- s->output.height = v4l2_get_height(&out_fmt);
- s->output.width = v4l2_get_width(&out_fmt);
- s->output.sample_aspect_ratio = v4l2_get_sar(&s->output);
- }
-
reinit = v4l2_resolution_changed(&s->capture, &cap_fmt);
if (reinit) {
s->capture.height = v4l2_get_height(&cap_fmt);
@@ -206,18 +192,9 @@ static int v4l2_handle_event(V4L2Context *ctx)
s->capture.sample_aspect_ratio = v4l2_get_sar(&s->capture);
}
- if (full_reinit || reinit)
+ if (reinit)
s->reinit = 1;
- if (full_reinit) {
- ret = ff_v4l2_m2m_codec_full_reinit(s);
- if (ret) {
- av_log(logger(ctx), AV_LOG_ERROR, "v4l2_m2m_codec_full_reinit\n");
- return AVERROR(EINVAL);
- }
- goto reinit_run;
- }
-
if (reinit) {
if (s->avctx)
ret = ff_set_dimensions(s->avctx, s->capture.width, s->capture.height);
--
2.33.0
3
15
Calculate Spatial Info (SI) and Temporal Info (TI) scores for a video, as defined
in ITU-T P.910: Subjective video quality assessment methods for multimedia
applications.
---
Changelog | 1 +
doc/filters.texi | 25 ++++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/version.h | 2 +-
libavfilter/vf_siti.c | 359 +++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 388 insertions(+), 1 deletion(-)
create mode 100644 libavfilter/vf_siti.c
diff --git a/Changelog b/Changelog
index dcb80e0ed9..9ca79a12aa 100644
--- a/Changelog
+++ b/Changelog
@@ -55,6 +55,7 @@ version <next>:
- asuperpass and asuperstop filter
- shufflepixels filter
- tmidequalizer filter
+- siti filter
version 4.3:
diff --git a/doc/filters.texi b/doc/filters.texi
index 813e35c2f9..eb1f23128c 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -18158,6 +18158,31 @@ ffmpeg -i input1.mkv -i input2.mkv -filter_complex "[0:v][1:v] signature=nb_inpu
@end itemize
+@anchor{siti}
+@section siti
+
+Calculate Spatial Info (SI) and Temporal Info (TI) scores for a video, as defined
+in ITU-T P.910: Subjective video quality assessment methods for multimedia
+applications. Available PDF at @url{https://www.itu.int/rec/T-REC-P.910-199909-S/en }.
+Per frame metrics can be written into a file in csv format.
+
+It accepts the following option:
+
+@table @option
+@item stats_file
+Set the path to the file where per frame SI and TI metrics will be written. If no file
+is specified, only summary statistics will be printed to the console.
+@end table
+
+@subsection Examples
+@itemize
+@item
+To calculate SI/TI metrics and store per frame data to stats.csv:
+@example
+ffmpeg -i input.mp4 -vf siti=stats_file='siti.csv' -f null -
+@end example
+@end itemize
+
@anchor{smartblur}
@section smartblur
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index ad1046d526..5514536463 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -413,6 +413,7 @@ OBJS-$(CONFIG_SMARTBLUR_FILTER) += vf_smartblur.o
OBJS-$(CONFIG_SOBEL_FILTER) += vf_convolution.o
OBJS-$(CONFIG_SOBEL_OPENCL_FILTER) += vf_convolution_opencl.o opencl.o \
opencl/convolution.o
+OBJS-$(CONFIG_SITI_FILTER) += vf_siti.o
OBJS-$(CONFIG_SPLIT_FILTER) += split.o
OBJS-$(CONFIG_SPP_FILTER) += vf_spp.o qp_table.o
OBJS-$(CONFIG_SR_FILTER) += vf_sr.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index ce317dfa1c..0aef9fdcef 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -393,6 +393,7 @@ extern AVFilter ff_vf_signature;
extern AVFilter ff_vf_smartblur;
extern AVFilter ff_vf_sobel;
extern AVFilter ff_vf_sobel_opencl;
+extern AVFilter ff_vf_siti;
extern AVFilter ff_vf_split;
extern AVFilter ff_vf_spp;
extern AVFilter ff_vf_sr;
diff --git a/libavfilter/version.h b/libavfilter/version.h
index fbe2ed62b2..2136235e54 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@
#include "libavutil/version.h"
#define LIBAVFILTER_VERSION_MAJOR 7
-#define LIBAVFILTER_VERSION_MINOR 95
+#define LIBAVFILTER_VERSION_MINOR 96
#define LIBAVFILTER_VERSION_MICRO 100
diff --git a/libavfilter/vf_siti.c b/libavfilter/vf_siti.c
new file mode 100644
index 0000000000..e8b6142ae7
--- /dev/null
+++ b/libavfilter/vf_siti.c
@@ -0,0 +1,359 @@
+/*
+ * Copyright (c) 2002 A'rpi
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/**
+ * @file
+ * Calculate Spatial Info (SI) and Temporal Info (TI) scores
+ */
+
+#include <math.h>
+
+#include "libavutil/imgutils.h"
+#include "libavutil/internal.h"
+#include "libavutil/opt.h"
+
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+static const int X_FILTER[9] = {
+ 1, 0, -1,
+ 2, 0, -2,
+ 1, 0, -1
+};
+
+static const int Y_FILTER[9] = {
+ 1, 2, 1,
+ 0, 0, 0,
+ -1, -2, -1
+};
+
+typedef struct SiTiContext {
+ const AVClass *class;
+ int pixel_depth;
+ int width, height;
+ int nb_frames;
+ unsigned char *prev_frame;
+ double max_si;
+ double max_ti;
+ double min_si;
+ double min_ti;
+ double sum_si;
+ double sum_ti;
+ FILE *stats_file;
+ char *stats_file_str;
+ int full_range;
+} SiTiContext;
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum AVPixelFormat pix_fmts[] = {
+ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
+ AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
+ AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10,
+ AV_PIX_FMT_NONE
+ };
+
+ AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+ if (!fmts_list)
+ return AVERROR(ENOMEM);
+ return ff_set_common_formats(ctx, fmts_list);
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+ // User options but no input data
+ SiTiContext *s = ctx->priv;
+ s->max_si = 0;
+ if (s->stats_file_str) {
+ s->stats_file = fopen(s->stats_file_str, "w");
+ if (!s->stats_file) {
+ int err = AVERROR(errno);
+ char buf[128];
+ av_strerror(err, buf, sizeof(buf));
+ av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
+ s->stats_file_str, buf);
+ return err;
+ }
+ fprintf(s->stats_file, "Frame,SI,TI\n");
+ }
+
+ return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ SiTiContext *s = ctx->priv;
+
+ double avg_si = s->sum_si / s->nb_frames;
+ double avg_ti = s->sum_ti / s->nb_frames;
+ av_log(ctx, AV_LOG_INFO,
+ "Summary:\nTotal frames: %d\n\n"
+ "Spatial Information:\nAverage: %f\nMax: %f\nMin: %f\n\n"
+ "Temporal Information:\nAverage: %f\nMax: %f\nMin: %f\n",
+ s->nb_frames, avg_si, s->max_si, s->min_si, avg_ti, s->max_ti, s->min_ti
+ );
+
+ if (s->stats_file && s->stats_file != stdout)
+ fclose(s->stats_file);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+ // Video input data avilable
+ AVFilterContext *ctx = inlink->dst;
+ SiTiContext *s = ctx->priv;
+ int max_pixsteps[4];
+
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+ av_image_fill_max_pixsteps(max_pixsteps, NULL, desc);
+
+ s->pixel_depth = max_pixsteps[0];
+ s->width = inlink->w;
+ s->height = inlink->h;
+ size_t pixel_sz = s->pixel_depth==1? (size_t) sizeof(uint8_t) : (size_t) sizeof(uint16_t);
+ size_t data_sz = (size_t) s->width * pixel_sz * s->height;
+ s->prev_frame = av_malloc(data_sz);
+
+ return 0;
+}
+
+// Get frame data handling 8 and 10 bit formats
+static uint16_t get_frame_data(const unsigned char* src, int pixel_depth, int index)
+{
+ const uint16_t *src16 = (const uint16_t *)src;
+ if (pixel_depth == 2)
+ {
+ return src16[index];
+ }
+ return (uint16_t) src[index];
+}
+
+// Set frame data handling 8 and 10 bit formats
+static void set_frame_data(unsigned char* dst, int pixel_depth, int index, uint16_t data)
+{
+ uint16_t *dst16 = (uint16_t *)dst;
+ if (pixel_depth == 2)
+ {
+ dst16[index] = data;
+ }
+ else
+ {
+ dst[index] = (uint8_t) data;
+ }
+}
+
+// Determine whether the video is in full or limited range. If not defined, assume limited.
+static int is_full_range(AVFrame* frame)
+{
+ if (frame->color_range == AVCOL_RANGE_UNSPECIFIED || frame->color_range == AVCOL_RANGE_NB)
+ {
+ // If color range not specified, fallback to pixel format
+ return frame->format == AV_PIX_FMT_YUVJ420P || frame->format == AV_PIX_FMT_YUVJ422P;
+ }
+ return frame->color_range == AVCOL_RANGE_JPEG;
+}
+
+// Check frame's color range and convert to full range if needed
+static uint16_t convert_full_range(uint16_t y, SiTiContext *s)
+{
+ if (s->full_range == 1)
+ {
+ return y;
+ }
+
+ // For 8 bits, limited range goes from 16 to 235, for 10 bits the range is multiplied by 4
+ double factor = s->pixel_depth == 1? 1 : 4;
+ double shift = 16 * factor;
+ double limit_upper = 235 * factor - shift;
+ double full_upper = 256 * factor - 1;
+ double limit_y = fmin(fmax(y - shift, 0), limit_upper);
+ return (uint16_t) (full_upper * limit_y / limit_upper);
+}
+
+// Applies sobel convolution
+static void convolve_sobel(const unsigned char* src, double* dst, int linesize, SiTiContext *s)
+{
+ int filter_width = 3;
+ int filter_size = filter_width * filter_width;
+ for (int j=1; j<s->height-1; j++)
+ {
+ for (int i=1; i<s->width-1; i++)
+ {
+ double x_conv_sum = 0, y_conv_sum = 0;
+ for (int k=0; k<filter_size; k++)
+ {
+ int ki = k % filter_width - 1;
+ int kj = floor(k / filter_width) - 1;
+ int index = (j + kj) * (linesize / s->pixel_depth) + (i + ki);
+ uint16_t data = convert_full_range(get_frame_data(src, s->pixel_depth, index), s);
+ x_conv_sum += data * X_FILTER[k];
+ y_conv_sum += data * Y_FILTER[k];
+ }
+ double gradient = sqrt(x_conv_sum * x_conv_sum + y_conv_sum * y_conv_sum);
+ // Dst matrix is smaller than src since we ignore edges that can't be convolved
+ dst[(j - 1) * (s->width - 2) + (i - 1)] = gradient;
+ }
+ }
+}
+
+// Calculate pixel difference between current and previous frame, and update previous
+static void calculate_motion(const unsigned char* curr, double* motion_matrix,
+ int linesize, SiTiContext *s)
+{
+ for (int j=0; j<s->height; j++)
+ {
+ for (int i=0; i<s->width; i++)
+ {
+ double motion = 0;
+ int curr_index = j * (linesize / s->pixel_depth) + i;
+ int prev_index = j * s->width + i;
+ uint16_t curr_data = convert_full_range(get_frame_data(curr, s->pixel_depth, curr_index), s);
+
+ if (s->nb_frames > 1)
+ {
+ // Previous frame is already converted to full range
+ motion = curr_data - get_frame_data(s->prev_frame, s->pixel_depth, prev_index);
+ }
+ set_frame_data(s->prev_frame, s->pixel_depth, prev_index, curr_data);
+ motion_matrix[j * s->width + i] = motion;
+ }
+ }
+}
+
+static double std_deviation(double* img_metrics, int width, int height)
+{
+ double size = height * width;
+
+ double mean_sum = 0;
+ for (int j=0; j<height; j++)
+ {
+ for (int i=0; i<width; i++)
+ {
+ mean_sum += img_metrics[j * width + i];
+ }
+ }
+ double mean = mean_sum / size;
+
+ double sqr_diff_sum = 0;
+ for (int j=0; j<height; j++)
+ {
+ for (int i=0; i<width; i++)
+ {
+ double mean_diff = img_metrics[j * width + i] - mean;
+ sqr_diff_sum += (mean_diff * mean_diff);
+ }
+ }
+ double variance = sqr_diff_sum / size;
+ return sqrt(variance);
+}
+
+static void set_meta(AVDictionary **metadata, const char *key, float d)
+{
+ char value[128];
+ snprintf(value, sizeof(value), "%0.2f", d);
+ av_dict_set(metadata, key, value, 0);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+ AVFilterContext *ctx = inlink->dst;
+ SiTiContext *s = ctx->priv;
+
+ // Gradient matrix will not include the input frame's edges
+ size_t gradient_data_sz = (size_t) (s->width - 2) * sizeof(double) * (s->height - 2);
+ double *gradient_matrix = av_malloc(gradient_data_sz);
+ size_t motion_data_sz = (size_t) s->width * sizeof(double) * s->height;
+ double *motion_matrix = av_malloc(motion_data_sz);
+ if (!gradient_matrix || !motion_matrix) {
+ av_frame_free(&frame);
+ return AVERROR(ENOMEM);
+ }
+
+ s->full_range = is_full_range(frame);
+ s->nb_frames++;
+
+ // Calculate si and ti
+ convolve_sobel(frame->data[0], gradient_matrix, frame->linesize[0], s);
+ calculate_motion(frame->data[0], motion_matrix, frame->linesize[0], s);
+ double si = std_deviation(gradient_matrix, s->width - 2, s->height - 2);
+ double ti = std_deviation(motion_matrix, s->width, s->height);
+
+ // Calculate statistics
+ s->max_si = fmax(si, s->max_si);
+ s->max_ti = fmax(ti, s->max_ti);
+ s->sum_si += si;
+ s->sum_ti += ti;
+ s->min_si = s->nb_frames == 1? si : fmin(si, s->min_si);
+ s->min_ti = s->nb_frames == 1? ti : fmin(ti, s->min_ti);
+
+ // Set si ti information in frame metadata
+ set_meta(&frame->metadata, "lavfi.siti.si", si);
+ set_meta(&frame->metadata, "lavfi.siti.ti", ti);
+
+ // Print per frame csv data to file
+ if (s->stats_file)
+ {
+ fprintf(s->stats_file, "%d,%f,%f\n", s->nb_frames, si, ti);
+ }
+
+ av_free(gradient_matrix);
+ return ff_filter_frame(inlink->dst->outputs[0], frame);
+}
+
+#define OFFSET(x) offsetof(SiTiContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption siti_options[] = {
+ {"stats_file", "Set file where to store per-frame si-ti scores", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(siti);
+
+static const AVFilterPad avfilter_vf_siti_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = config_input,
+ .filter_frame = filter_frame,
+ },
+ { NULL }
+};
+
+static const AVFilterPad avfilter_vf_siti_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO
+ },
+ { NULL }
+};
+
+AVFilter ff_vf_siti = {
+ .name = "siti",
+ .description = NULL_IF_CONFIG_SMALL("Calculate spatial info (SI)."),
+ .priv_size = sizeof(SiTiContext),
+ .priv_class = &siti_class,
+ .init = init,
+ .uninit = uninit,
+ .query_formats = query_formats,
+ .inputs = avfilter_vf_siti_inputs,
+ .outputs = avfilter_vf_siti_outputs,
+};
--
2.13.5
8
39