ffmpeg-devel
Threads by month
- ----- 2026 -----
- 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
October 2022
- 88 participants
- 289 discussions
[PATCH v13 8/9] avformat/mov_demuxer: Extended MOV demuxer to handle EVC video content
by Dawid Kozinski 07 Oct '22
by Dawid Kozinski 07 Oct '22
07 Oct '22
- Added evc extension to the list of extensions for ff_mov_demuxer
Signed-off-by: Dawid Kozinski <d.kozinski(a)samsung.com>
---
libavformat/demux.c | 1 +
libavformat/mov.c | 3 ++-
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/libavformat/demux.c b/libavformat/demux.c
index 2dfd82a63c..f3ebe4d09b 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -120,6 +120,7 @@ static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st,
{ "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
{ "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
{ "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
+ { "evc", AV_CODEC_ID_EVC, AVMEDIA_TYPE_VIDEO },
{ 0 }
};
int score;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 1f436e21d6..435c9d905e 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2502,6 +2502,7 @@ static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb,
case AV_CODEC_ID_VP9:
sti->need_parsing = AVSTREAM_PARSE_FULL;
break;
+ case AV_CODEC_ID_EVC:
case AV_CODEC_ID_AV1:
/* field_order detection of H264 requires parsing */
case AV_CODEC_ID_H264:
@@ -9130,7 +9131,7 @@ const AVInputFormat ff_mov_demuxer = {
.long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
.priv_class = &mov_class,
.priv_data_size = sizeof(MOVContext),
- .extensions = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4b,ism,ismv,isma,f4v,avif",
+ .extensions = "mov,mp4,m4a,3gp,3g2,mj2,psp,m4b,ism,ismv,isma,f4v,avif,evc",
.flags_internal = FF_FMT_INIT_CLEANUP,
.read_probe = mov_probe,
.read_header = mov_read_header,
--
2.17.1
1
0
[PATCH v13 7/9] avformat/mov_muxer: Extended MOV muxer to handle EVC video content
by Dawid Kozinski 07 Oct '22
by Dawid Kozinski 07 Oct '22
07 Oct '22
- Changes in mov_write_video_tag function to handle EVC elementary stream
- Provided structure EVCDecoderConfigurationRecord that specifies the decoder configuration information for ISO/IEC 23094-1 video content
Signed-off-by: Dawid Kozinski <d.kozinski(a)samsung.com>
---
libavformat/Makefile | 2 +-
libavformat/evc.c | 455 ++++++++++++++++++++++++++++++++++++++++
libavformat/evc.h | 44 ++++
libavformat/isom_tags.c | 2 +
libavformat/movenc.c | 35 +++-
5 files changed, 536 insertions(+), 2 deletions(-)
create mode 100644 libavformat/evc.c
create mode 100644 libavformat/evc.h
diff --git a/libavformat/Makefile b/libavformat/Makefile
index af175d2097..6ae5056c61 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -363,7 +363,7 @@ OBJS-$(CONFIG_MOV_DEMUXER) += mov.o mov_chan.o mov_esds.o \
OBJS-$(CONFIG_MOV_MUXER) += movenc.o av1.o avc.o hevc.o vpcc.o \
movenchint.o mov_chan.o rtp.o \
movenccenc.o movenc_ttml.o rawutils.o \
- dovi_isom.o
+ dovi_isom.o evc.o
OBJS-$(CONFIG_MP2_MUXER) += rawenc.o
OBJS-$(CONFIG_MP3_DEMUXER) += mp3dec.o replaygain.o
OBJS-$(CONFIG_MP3_MUXER) += mp3enc.o rawenc.o id3v2enc.o
diff --git a/libavformat/evc.c b/libavformat/evc.c
new file mode 100644
index 0000000000..72c2759924
--- /dev/null
+++ b/libavformat/evc.c
@@ -0,0 +1,455 @@
+/*
+ * EVC helper functions for muxers
+ * Copyright (c) 2022 Dawid Kozinski <d.kozinski(a)samsung.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "libavcodec/get_bits.h"
+#include "libavcodec/golomb.h"
+#include "libavcodec/evc.h"
+#include "avformat.h"
+#include "avio.h"
+#include "evc.h"
+#include "avio_internal.h"
+
+// The length field that indicates the length in bytes of the following NAL unit is configured to be of 4 bytes
+#define EVC_NAL_UNIT_LENGTH_BYTE (4) /* byte */
+#define EVC_NAL_HEADER_SIZE (2) /* byte */
+
+// @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.1
+enum {
+ SPS_INDEX,
+ PPS_INDEX,
+ APS_INDEX,
+ SEI_INDEX,
+ NB_ARRAYS
+};
+
+// rpl structure
+typedef struct RefPicListStruct {
+ int poc;
+ int tid;
+ int ref_pic_num;
+ int ref_pic_active_num;
+ int ref_pics[EVC_MAX_NUM_REF_PICS];
+ char pic_type;
+
+} RefPicListStruct;
+
+// The sturcture reflects SPS RBSP(raw byte sequence payload) layout
+// @see ISO_IEC_23094-1 section 7.3.2.1
+//
+// The following descriptors specify the parsing process of each element
+// u(n) - unsigned integer using n bits
+// ue(v) - unsigned integer 0-th order Exp_Golomb-coded syntax element with the left bit first
+typedef struct EVCSPS {
+ int sps_seq_parameter_set_id; // ue(v)
+ int profile_idc; // u(8)
+ int level_idc; // u(8)
+ int toolset_idc_h; // u(32)
+ int toolset_idc_l; // u(32)
+ int chroma_format_idc; // ue(v)
+ int pic_width_in_luma_samples; // ue(v)
+ int pic_height_in_luma_samples; // ue(v)
+ int bit_depth_luma_minus8; // ue(v)
+ int bit_depth_chroma_minus8; // ue(v)
+
+ // @note
+ // Currently the structure does not reflect the entire SPS RBSP layout.
+ // It contains only the fields that are necessary to read from the NAL unit all the values
+ // necessary for the correct initialization of EVCDecoderConfigurationRecord
+
+ // @note
+ // If necessary, add the missing fields to the structure to reflect
+ // the contents of the entire NAL unit of the SPS type
+
+} EVCSPS;
+
+// @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
+typedef struct EVCNALUnitArray {
+ uint8_t array_completeness; // when equal to 1 indicates that all NAL units of the given type are in the following array
+ uint8_t NAL_unit_type; // indicates the type of the NAL units in the following array
+ uint16_t numNalus; // indicates the number of NAL units of the indicated type
+ uint16_t *nalUnitLength; // indicates the length in bytes of the NAL unit
+ uint8_t **nalUnit; // contains an SPS, PPS, APS or a SEI NAL unit, as specified in ISO/IEC 23094-1
+} EVCNALUnitArray;
+
+/**
+ * @brief Specifies the decoder configuration information for ISO/IEC 23094-1 video content.
+ * @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.2
+ * Carriage of network abstraction layer (NAL) unit structured video in the ISO base media file format
+ */
+typedef struct EVCDecoderConfigurationRecord {
+ uint8_t configurationVersion; // 8 bits
+ uint8_t profile_idc; // 8 bits
+ uint8_t level_idc; // 8 bits
+ uint32_t toolset_idc_h; // 32 bits
+ uint32_t toolset_idc_l; // 32 bits
+ uint8_t chroma_format_idc; // 2 bits
+ uint8_t bit_depth_luma_minus8; // 3 bits
+ uint8_t bit_depth_chroma_minus8; // 3 bits
+ uint16_t pic_width_in_luma_samples; // 16 bits
+ uint16_t pic_height_in_luma_samples; // 16 bits
+ uint8_t reserved; // 6 bits '000000'b
+ uint8_t lengthSizeMinusOne; // 2 bits
+ uint8_t num_of_arrays; // 8 bits
+ EVCNALUnitArray arrays[NB_ARRAYS];
+} EVCDecoderConfigurationRecord;
+
+typedef struct NALU {
+ int offset;
+ uint32_t size;
+} NALU;
+
+typedef struct NALUList {
+ NALU *nalus;
+ unsigned nalus_array_size;
+ unsigned nb_nalus; ///< valid entries in nalus
+} NALUList;
+
+static int get_nalu_type(const uint8_t *bits, int bits_size)
+{
+ int unit_type_plus1 = 0;
+
+ if (bits_size >= EVC_NAL_HEADER_SIZE) {
+ unsigned char *p = (unsigned char *)bits;
+ // forbidden_zero_bit
+ if ((p[0] & 0x80) != 0)
+ return -1;
+
+ // nal_unit_type
+ unit_type_plus1 = (p[0] >> 1) & 0x3F;
+ }
+
+ return unit_type_plus1 - 1;
+}
+
+static uint32_t read_nal_unit_length(const uint8_t *bits, int bits_size)
+{
+ uint32_t nalu_len = 0;
+
+ if (bits_size >= EVC_NAL_UNIT_LENGTH_BYTE) {
+
+ int t = 0;
+ for (int i = 0; i < EVC_NAL_UNIT_LENGTH_BYTE; i++)
+ t = (t << 8) | bits[i];
+
+ nalu_len = t;
+ if (nalu_len == 0)
+ return 0;
+ }
+
+ return nalu_len;
+}
+
+// @see ISO_IEC_23094-1 (7.3.2.1 SPS RBSP syntax)
+static int evcc_parse_sps(const uint8_t *bs, int bs_size, EVCDecoderConfigurationRecord *evcc)
+{
+ GetBitContext gb;
+ int sps_seq_parameter_set_id;
+ EVCSPS sps;
+
+ if (init_get_bits8(&gb, bs, bs_size) < 0)
+ return 0;
+
+ sps.sps_seq_parameter_set_id = get_ue_golomb(&gb);
+
+ if (sps_seq_parameter_set_id >= EVC_MAX_SPS_COUNT)
+ return 0;
+
+ // the Baseline profile is indicated by profile_idc eqal to 0
+ // the Main profile is indicated by profile_idc eqal to 1
+ sps.profile_idc = get_bits(&gb, 8);
+
+ sps.level_idc = get_bits(&gb, 8);
+
+ sps.toolset_idc_h = get_bits(&gb, 32);
+ sps.toolset_idc_l = get_bits(&gb, 32);
+
+ // 0 - monochrome
+ // 1 - 4:2:0
+ // 2 - 4:2:2
+ // 3 - 4:4:4
+ sps.chroma_format_idc = get_ue_golomb(&gb);
+
+ sps.pic_width_in_luma_samples = get_ue_golomb(&gb);
+ sps.pic_height_in_luma_samples = get_ue_golomb(&gb);
+
+ sps.bit_depth_luma_minus8 = get_ue_golomb(&gb);
+ sps.bit_depth_chroma_minus8 = get_ue_golomb(&gb);
+
+ evcc->profile_idc = sps.profile_idc;
+ evcc->level_idc = sps.level_idc;
+ evcc->toolset_idc_h = sps.toolset_idc_h;
+ evcc->toolset_idc_l = sps.toolset_idc_l;
+ evcc->chroma_format_idc = sps.chroma_format_idc;
+ evcc->bit_depth_luma_minus8 = sps.bit_depth_luma_minus8;
+ evcc->bit_depth_chroma_minus8 = sps.bit_depth_chroma_minus8;
+ evcc->pic_width_in_luma_samples = sps.pic_width_in_luma_samples;
+ evcc->pic_height_in_luma_samples = sps.pic_height_in_luma_samples;
+
+ return 0;
+}
+
+// @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
+static int evcc_array_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size,
+ uint8_t nal_type, int ps_array_completeness,
+ EVCNALUnitArray *array)
+{
+ int ret;
+ uint16_t numNalus = array->numNalus;
+
+ ret = av_reallocp_array(&array->nalUnit, numNalus + 1, sizeof(uint8_t *));
+ if (ret < 0)
+ return ret;
+
+ ret = av_reallocp_array(&array->nalUnitLength, numNalus + 1, sizeof(uint16_t));
+ if (ret < 0)
+ return ret;
+
+ array->nalUnit [numNalus] = (uint8_t*)nal_buf;
+ array->nalUnitLength[numNalus] = nal_size;
+ array->NAL_unit_type = nal_type;
+ array->numNalus++;
+
+ /*
+ * When the sample entry name is 'evc1', the default and mandatory value of
+ * array_completeness is 1 for arrays of all types of parameter sets, and 0
+ * for all other arrays.
+ */
+ if (nal_type == EVC_SPS_NUT || nal_type == EVC_PPS_NUT || nal_type == EVC_APS_NUT)
+ array->array_completeness = ps_array_completeness;
+
+ return 0;
+}
+
+static void evcc_init(EVCDecoderConfigurationRecord *evcc)
+{
+ memset(evcc, 0, sizeof(EVCDecoderConfigurationRecord));
+ evcc->configurationVersion = 1;
+ evcc->lengthSizeMinusOne = 3; // 4 bytes
+}
+
+static void evcc_close(EVCDecoderConfigurationRecord *evcc)
+{
+ for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
+ EVCNALUnitArray *const array = &evcc->arrays[i];
+ array->numNalus = 0;
+ av_freep(&array->nalUnit);
+ av_freep(&array->nalUnitLength);
+ }
+}
+
+static int evcc_write(AVIOContext *pb, EVCDecoderConfigurationRecord *evcc)
+{
+ uint16_t sps_count;
+
+ av_log(NULL, AV_LOG_TRACE, "configurationVersion: %"PRIu8"\n",
+ evcc->configurationVersion);
+ av_log(NULL, AV_LOG_TRACE, "profile_idc: %"PRIu8"\n",
+ evcc->profile_idc);
+ av_log(NULL, AV_LOG_TRACE, "level_idc: %"PRIu8"\n",
+ evcc->level_idc);
+ av_log(NULL, AV_LOG_TRACE, "toolset_idc_h: %"PRIu32"\n",
+ evcc->toolset_idc_h);
+ av_log(NULL, AV_LOG_TRACE, "toolset_idc_l: %"PRIu32"\n",
+ evcc->toolset_idc_l);
+ av_log(NULL, AV_LOG_TRACE, "chroma_format_idc: %"PRIu8"\n",
+ evcc->chroma_format_idc);
+ av_log(NULL, AV_LOG_TRACE, "bit_depth_luma_minus8: %"PRIu8"\n",
+ evcc->bit_depth_luma_minus8);
+ av_log(NULL, AV_LOG_TRACE, "bit_depth_chroma_minus8: %"PRIu8"\n",
+ evcc->bit_depth_chroma_minus8);
+ av_log(NULL, AV_LOG_TRACE, "pic_width_in_luma_samples: %"PRIu16"\n",
+ evcc->pic_width_in_luma_samples);
+ av_log(NULL, AV_LOG_TRACE, "pic_height_in_luma_samples: %"PRIu16"\n",
+ evcc->pic_height_in_luma_samples);
+ av_log(NULL, AV_LOG_TRACE, "lengthSizeMinusOne: %"PRIu8"\n",
+ evcc->lengthSizeMinusOne);
+ av_log(NULL, AV_LOG_TRACE, "num_of_arrays: %"PRIu8"\n",
+ evcc->num_of_arrays);
+ for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
+ const EVCNALUnitArray *const array = &evcc->arrays[i];
+
+ if(array->numNalus == 0)
+ continue;
+
+ av_log(NULL, AV_LOG_TRACE, "array_completeness[%"PRIu8"]: %"PRIu8"\n",
+ i, array->array_completeness);
+ av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%"PRIu8"]: %"PRIu8"\n",
+ i, array->NAL_unit_type);
+ av_log(NULL, AV_LOG_TRACE, "numNalus[%"PRIu8"]: %"PRIu16"\n",
+ i, array->numNalus);
+ for ( unsigned j = 0; j < array->numNalus; j++)
+ av_log(NULL, AV_LOG_TRACE,
+ "nalUnitLength[%"PRIu8"][%"PRIu16"]: %"PRIu16"\n",
+ i, j, array->nalUnitLength[j]);
+ }
+
+ /*
+ * We need at least one SPS.
+ */
+ sps_count = evcc->arrays[SPS_INDEX].numNalus;
+ if (!sps_count || sps_count > EVC_MAX_SPS_COUNT)
+ return AVERROR_INVALIDDATA;
+
+ if (!sps_count || sps_count > EVC_MAX_SPS_COUNT)
+ return AVERROR_INVALIDDATA;
+
+ /* unsigned int(8) configurationVersion = 1; */
+ avio_w8(pb, evcc->configurationVersion);
+
+ /* unsigned int(8) profile_idc */
+ avio_w8(pb, evcc->profile_idc);
+
+ /* unsigned int(8) profile_idc */
+ avio_w8(pb, evcc->level_idc);
+
+ /* unsigned int(32) toolset_idc_h */
+ avio_wb32(pb, evcc->toolset_idc_h);
+
+ /* unsigned int(32) toolset_idc_l */
+ avio_wb32(pb, evcc->toolset_idc_l);
+
+ /*
+ * unsigned int(2) chroma_format_idc;
+ * unsigned int(3) bit_depth_luma_minus8;
+ * unsigned int(3) bit_depth_chroma_minus8;
+ */
+ avio_w8(pb, evcc->chroma_format_idc << 6 |
+ evcc->bit_depth_luma_minus8 << 3 |
+ evcc->bit_depth_chroma_minus8);
+
+ /* unsigned int(16) pic_width_in_luma_samples; */
+ avio_wb16(pb, evcc->pic_width_in_luma_samples);
+
+ /* unsigned int(16) pic_width_in_luma_samples; */
+ avio_wb16(pb, evcc->pic_height_in_luma_samples);
+
+ /*
+ * bit(6) reserved = '111111'b;
+ * unsigned int(2) chromaFormat;
+ */
+ avio_w8(pb, evcc->lengthSizeMinusOne | 0xfc);
+
+ /* unsigned int(8) numOfArrays; */
+ avio_w8(pb, evcc->num_of_arrays);
+
+ for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
+ const EVCNALUnitArray *const array = &evcc->arrays[i];
+
+ if (!array->numNalus)
+ continue;
+
+ /*
+ * bit(1) array_completeness;
+ * unsigned int(1) reserved = 0;
+ * unsigned int(6) NAL_unit_type;
+ */
+ avio_w8(pb, array->array_completeness << 7 |
+ array->NAL_unit_type & 0x3f);
+
+ /* unsigned int(16) numNalus; */
+ avio_wb16(pb, array->numNalus);
+
+ for (unsigned j = 0; j < array->numNalus; j++) {
+ /* unsigned int(16) nalUnitLength; */
+ avio_wb16(pb, array->nalUnitLength[j]);
+
+ /* bit(8*nalUnitLength) nalUnit; */
+ avio_write(pb, array->nalUnit[j],
+ array->nalUnitLength[j]);
+ }
+ }
+
+ return 0;
+}
+
+int ff_isom_write_evcc(AVIOContext *pb, const uint8_t *data,
+ int size, int ps_array_completeness)
+{
+ EVCDecoderConfigurationRecord evcc;
+ int nalu_type;
+ size_t nalu_size;
+ int bytes_to_read = size;
+ unsigned array_index;
+
+ int ret = 0;
+
+ if (size < 8) {
+ /* We can't write a valid evcC from the provided data */
+ return AVERROR_INVALIDDATA;
+ } else if (*data == 1) {
+ /* Data is already evcC-formatted */
+ avio_write(pb, data, size);
+ return 0;
+ }
+
+ evcc_init(&evcc);
+
+ while (bytes_to_read > EVC_NAL_UNIT_LENGTH_BYTE) {
+ nalu_size = read_nal_unit_length(data, EVC_NAL_UNIT_LENGTH_BYTE);
+ if (nalu_size == 0) break;
+
+ data += EVC_NAL_UNIT_LENGTH_BYTE;
+ bytes_to_read -= EVC_NAL_UNIT_LENGTH_BYTE;
+
+ if (bytes_to_read < nalu_size) break;
+
+ nalu_type = get_nalu_type(data, bytes_to_read);
+
+ switch (nalu_type) {
+ case EVC_SPS_NUT:
+ array_index = SPS_INDEX;
+ break;
+ case EVC_PPS_NUT:
+ array_index = PPS_INDEX;
+ break;
+ case EVC_APS_NUT:
+ array_index = APS_INDEX;
+ break;
+ case EVC_SEI_NUT:
+ array_index = SEI_INDEX;
+ break;
+ default:
+ break;
+ }
+
+ ret = evcc_array_add_nal_unit(data, nalu_size, nalu_type, ps_array_completeness, &(evcc.arrays[array_index]));
+ if (ret < 0)
+ goto end;
+ if (evcc.arrays[array_index].numNalus == 1)
+ evcc.num_of_arrays++;
+
+ if(nalu_type == EVC_SPS_NUT) {
+ ret = evcc_parse_sps(data, nalu_size, &evcc);
+ if (ret < 0)
+ goto end;
+ }
+
+ data += nalu_size;
+ bytes_to_read -= nalu_size;
+ }
+
+ ret = evcc_write(pb, &evcc);
+
+end:
+ evcc_close(&evcc);
+ return ret;
+}
diff --git a/libavformat/evc.h b/libavformat/evc.h
new file mode 100644
index 0000000000..db56275fd8
--- /dev/null
+++ b/libavformat/evc.h
@@ -0,0 +1,44 @@
+/*
+ * EVC helper functions for muxers
+ * Copyright (c) 2022 Dawid Kozinski <d.kozinski(a)samsung.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ */
+
+#ifndef AVFORMAT_EVC_H
+#define AVFORMAT_EVC_H
+
+#include <stdint.h>
+#include "libavutil/rational.h"
+#include "avio.h"
+
+
+
+/**
+ * Writes EVC sample metadata to the provided AVIOContext.
+ *
+ * @param pb pointer to the AVIOContext where the evc sample metadata shall be written
+ * @param buf input data buffer
+ * @param size size in bytes of the input data buffer
+ * @param ps_array_completeness @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
+ *
+ * @return 0 in case of success, a negative error code in case of failure
+ */
+int ff_isom_write_evcc(AVIOContext *pb, const uint8_t *data,
+ int size, int ps_array_completeness);
+
+#endif // AVFORMAT_EVC_H
diff --git a/libavformat/isom_tags.c b/libavformat/isom_tags.c
index e2b80405cc..8ca740cab1 100644
--- a/libavformat/isom_tags.c
+++ b/libavformat/isom_tags.c
@@ -147,6 +147,8 @@ const AVCodecTag ff_codec_movvideo_tags[] = {
{ AV_CODEC_ID_H264, MKTAG('d', 'v', 'a', '1') }, /* AVC-based Dolby Vision derived from avc1 */
{ AV_CODEC_ID_H264, MKTAG('d', 'v', 'a', 'v') }, /* AVC-based Dolby Vision derived from avc3 */
+ { AV_CODEC_ID_EVC, MKTAG('e', 'v', 'c', '1') }, /* EVC/MPEG-5 */
+
{ AV_CODEC_ID_VP8, MKTAG('v', 'p', '0', '8') }, /* VP8 */
{ AV_CODEC_ID_VP9, MKTAG('v', 'p', '0', '9') }, /* VP9 */
{ AV_CODEC_ID_AV1, MKTAG('a', 'v', '0', '1') }, /* AV1 */
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 754f95912a..6f782bd536 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -35,6 +35,7 @@
#include "isom.h"
#include "av1.h"
#include "avc.h"
+#include "evc.h"
#include "libavcodec/ac3_parser_internal.h"
#include "libavcodec/dnxhddata.h"
#include "libavcodec/flac.h"
@@ -1392,6 +1393,21 @@ static int mov_write_hvcc_tag(AVIOContext *pb, MOVTrack *track)
return update_size(pb, pos);
}
+static int mov_write_evcc_tag(AVIOContext *pb, MOVTrack *track)
+{
+ int64_t pos = avio_tell(pb);
+
+ avio_wb32(pb, 0);
+ ffio_wfourcc(pb, "evcC");
+
+ if (track->tag == MKTAG('e','v','c','1'))
+ ff_isom_write_evcc(pb, track->vos_data, track->vos_len, 1);
+ else
+ ff_isom_write_evcc(pb, track->vos_data, track->vos_len, 0);
+
+ return update_size(pb, pos);
+}
+
/* also used by all avid codecs (dv, imx, meridien) and their variants */
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
{
@@ -1641,6 +1657,16 @@ static int mov_get_h264_codec_tag(AVFormatContext *s, MOVTrack *track)
return tag;
}
+static int mov_get_evc_codec_tag(AVFormatContext *s, MOVTrack *track)
+{
+ int tag = track->par->codec_tag;
+
+ if (!tag)
+ tag = MKTAG('e', 'v', 'c', 'i');
+
+ return tag;
+}
+
static const struct {
enum AVPixelFormat pix_fmt;
uint32_t tag;
@@ -1722,6 +1748,8 @@ static unsigned int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
else if (track->par->codec_id == AV_CODEC_ID_H264)
tag = mov_get_h264_codec_tag(s, track);
+ else if (track->par->codec_id == AV_CODEC_ID_EVC)
+ tag = mov_get_evc_codec_tag(s, track);
else if (track->par->codec_id == AV_CODEC_ID_DNXHD)
tag = mov_get_dnxhd_codec_tag(s, track);
else if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
@@ -2290,6 +2318,9 @@ static int mov_write_video_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
mov_write_avcc_tag(pb, track);
if (track->mode == MODE_IPOD)
mov_write_uuid_tag_ipod(pb);
+ }
+ else if (track->par->codec_id ==AV_CODEC_ID_EVC) {
+ mov_write_evcc_tag(pb, track);
} else if (track->par->codec_id == AV_CODEC_ID_VP9) {
mov_write_vpcc_tag(mov->fc, pb, track);
} else if (track->par->codec_id == AV_CODEC_ID_AV1) {
@@ -6033,7 +6064,8 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
if ((par->codec_id == AV_CODEC_ID_DNXHD ||
par->codec_id == AV_CODEC_ID_H264 ||
par->codec_id == AV_CODEC_ID_HEVC ||
- par->codec_id == AV_CODEC_ID_TRUEHD) && !trk->vos_len &&
+ par->codec_id == AV_CODEC_ID_TRUEHD ||
+ par->codec_id == AV_CODEC_ID_EVC) && !trk->vos_len &&
!TAG_IS_AVCI(trk->tag)) {
/* copy frame to create needed atoms */
trk->vos_len = size;
@@ -7692,6 +7724,7 @@ static const AVCodecTag codec_mp4_tags[] = {
{ AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '3') },
{ AV_CODEC_ID_HEVC, MKTAG('h', 'e', 'v', '1') },
{ AV_CODEC_ID_HEVC, MKTAG('h', 'v', 'c', '1') },
+ { AV_CODEC_ID_EVC, MKTAG('e', 'v', 'c', '1') },
{ AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', '4', 'v') },
{ AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', '4', 'v') },
{ AV_CODEC_ID_MJPEG, MKTAG('m', 'p', '4', 'v') },
--
2.17.1
1
0
07 Oct '22
- Added EVC decoder wrapper
- Changes in project configuration file and libavcodec Makefile
- Added documentation for xevd wrapper
Signed-off-by: Dawid Kozinski <d.kozinski(a)samsung.com>
---
configure | 4 +
doc/decoders.texi | 24 +++
doc/general_contents.texi | 10 +-
libavcodec/Makefile | 1 +
libavcodec/allcodecs.c | 1 +
libavcodec/libxevd.c | 423 ++++++++++++++++++++++++++++++++++++++
6 files changed, 462 insertions(+), 1 deletion(-)
create mode 100644 libavcodec/libxevd.c
diff --git a/configure b/configure
index 7f430995a6..0adbd4291e 100755
--- a/configure
+++ b/configure
@@ -292,6 +292,7 @@ External library support:
--enable-libx264 enable H.264 encoding via x264 [no]
--enable-libx265 enable HEVC encoding via x265 [no]
--enable-libxeve enable EVC encoding via libxeve [no]
+ --enable-libxevd enable EVC decoding via libxevd [no]
--enable-libxavs enable AVS encoding via xavs [no]
--enable-libxavs2 enable AVS2 encoding via xavs2 [no]
--enable-libxcb enable X11 grabbing using XCB [autodetect]
@@ -1877,6 +1878,7 @@ EXTERNAL_LIBRARY_LIST="
libvorbis
libvpx
libwebp
+ libxevd
libxeve
libxml2
libzimg
@@ -3409,6 +3411,7 @@ libx264rgb_encoder_select="libx264_encoder"
libx265_encoder_deps="libx265"
libxavs_encoder_deps="libxavs"
libxavs2_encoder_deps="libxavs2"
+libxevd_decoder_deps="libxevd"
libxeve_encoder_deps="libxeve"
libxvid_encoder_deps="libxvid"
libzvbi_teletext_decoder_deps="libzvbi"
@@ -6742,6 +6745,7 @@ enabled libx265 && require_pkg_config libx265 x265 x265.h x265_api_get
require_cpp_condition libx265 x265.h "X265_BUILD >= 70"
enabled libxavs && require libxavs "stdint.h xavs.h" xavs_encoder_encode "-lxavs $pthreads_extralibs $libm_extralibs"
enabled libxavs2 && require_pkg_config libxavs2 "xavs2 >= 1.3.0" "stdint.h xavs2.h" xavs2_api_get
+enabled libxevd && require_pkg_config libxevd "xevd >= 0.4.0" "xevd.h" xevd_decode
enabled libxeve && require_pkg_config libxeve "xeve >= 0.4.0" "xeve.h" xeve_encode
enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore
enabled libzimg && require_pkg_config libzimg "zimg >= 2.7.0" zimg.h zimg_get_api_version
diff --git a/doc/decoders.texi b/doc/decoders.texi
index e2fcbf5dc9..0bd9096114 100644
--- a/doc/decoders.texi
+++ b/doc/decoders.texi
@@ -126,6 +126,30 @@ Set amount of frame threads to use during decoding. The default value is 0 (auto
@end table
+@section libxevd
+
+eXtra-fast Essential Video Decoder (XEVD) MPEG-5 EVC decoder wrapper.
+
+This decoder requires the presence of the libxevd headers and library
+during configuration. You need to explicitly configure the build with
+@option{--enable-libxevd}.
+
+The xevd project website is at @url{https://github.com/mpeg5/xevd}.
+
+@subsection Options
+
+The following options are supported by the libxevd wrapper.
+The xevd-equivalent options or values are listed in parentheses for easy migration.
+
+To get a more accurate and extensive documentation of the libxevd options,
+invoke the command @code{xevd_app --help} or consult the libxevd documentation.
+
+@table @option
+@item threads (@emph{threads})
+Force to use a specific number of threads
+
+@end table
+
@section QSV Decoders
The family of Intel QuickSync Video decoders (VC1, MPEG-2, H.264, HEVC,
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index 417f0ab64f..0d9daf43a7 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -351,6 +351,14 @@ Go to @url{https://github.com/mpeg5/xeve} and follow the instructions for
installing the XEVE library. Then pass @code{--enable-libxeve} to configure to
enable it.
+@section eXtra-fast Essential Video Decoder (XEVD)
+
+FFmpeg can make use of the XEVD library for EVC video decoding.
+
+Go to @url{https://github.com/mpeg5/xevd} and follow the instructions for
+installing the XEVD library. Then pass @code{--enable-libxevd} to configure to
+enable it.
+
@section ZVBI
ZVBI is a VBI decoding library which can be used by FFmpeg to decode DVB
@@ -944,7 +952,7 @@ following image formats are supported:
@item Escape 124 @tab @tab X
@item Escape 130 @tab @tab X
@item EVC / MPEG-5 Part 1 @tab X @tab X
- @tab encoding supported through external library libxeve
+ @tab encoding and decoding supported through external libraries libxeve and libxevd
@item FFmpeg video codec #1 @tab X @tab X
@tab lossless codec (fourcc: FFV1)
@item Flash Screen Video v1 @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 8944e2a0d6..f2799ae19b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1107,6 +1107,7 @@ OBJS-$(CONFIG_LIBX264_ENCODER) += libx264.o
OBJS-$(CONFIG_LIBX265_ENCODER) += libx265.o
OBJS-$(CONFIG_LIBXAVS_ENCODER) += libxavs.o
OBJS-$(CONFIG_LIBXAVS2_ENCODER) += libxavs2.o
+OBJS-$(CONFIG_LIBXEVD_DECODER) += libxevd.o
OBJS-$(CONFIG_LIBXEVE_ENCODER) += libxeve.o
OBJS-$(CONFIG_LIBXVID_ENCODER) += libxvid.o
OBJS-$(CONFIG_LIBZVBI_TELETEXT_DECODER) += libzvbi-teletextdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 5ce398f9ee..571ca31056 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -808,6 +808,7 @@ extern LIBX264_CONST FFCodec ff_libx264_encoder;
extern const FFCodec ff_libx264rgb_encoder;
extern FFCodec ff_libx265_encoder;
extern const FFCodec ff_libxeve_encoder;
+extern const FFCodec ff_libxevd_decoder;
extern const FFCodec ff_libxavs_encoder;
extern const FFCodec ff_libxavs2_encoder;
extern const FFCodec ff_libxvid_encoder;
diff --git a/libavcodec/libxevd.c b/libavcodec/libxevd.c
new file mode 100644
index 0000000000..133d004f4b
--- /dev/null
+++ b/libavcodec/libxevd.c
@@ -0,0 +1,423 @@
+/*
+ * libxevd decoder
+ * EVC (MPEG-5 Essential Video Coding) decoding using XEVD MPEG-5 EVC decoder library
+ *
+ * Copyright (C) 2021 Dawid Kozinski <d.kozinski(a)samsung.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ */
+
+#include <float.h>
+#include <stdlib.h>
+
+#include <xevd.h>
+
+#include "libavutil/internal.h"
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/pixfmt.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/cpu.h"
+
+#include "avcodec.h"
+#include "internal.h"
+#include "packet_internal.h"
+#include "codec_internal.h"
+#include "profiles.h"
+#include "decode.h"
+
+#define XEVD_PARAM_BAD_NAME -1
+#define XEVD_PARAM_BAD_VALUE -2
+
+#define EVC_NAL_HEADER_SIZE 2 /* byte */
+
+/**
+ * The structure stores all the states associated with the instance of Xeve MPEG-5 EVC decoder
+ */
+typedef struct XevdContext {
+ const AVClass *class;
+
+ XEVD id; // XEVD instance identifier @see xevd.h
+ XEVD_CDSC cdsc; // decoding parameters @see xevd.h
+} XevdContext;
+
+/**
+ * The function populates the XEVD_CDSC structure.
+ * XEVD_CDSC contains all decoder parameters that should be initialized before its use.
+ *
+ * @param[in] avctx codec context
+ * @param[out] cdsc contains all decoder parameters that should be initialized before its use
+ *
+ */
+static void get_conf(AVCodecContext *avctx, XEVD_CDSC *cdsc)
+{
+ int cpu_count = av_cpu_count();
+
+ /* clear XEVS_CDSC structure */
+ memset(cdsc, 0, sizeof(XEVD_CDSC));
+
+ /* init XEVD_CDSC */
+ if (avctx->thread_count <= 0)
+ cdsc->threads = (cpu_count < XEVD_MAX_TASK_CNT) ? cpu_count : XEVD_MAX_TASK_CNT;
+ else if (avctx->thread_count > XEVD_MAX_TASK_CNT)
+ cdsc->threads = XEVD_MAX_TASK_CNT;
+ else
+ cdsc->threads = avctx->thread_count;
+}
+
+/**
+ * Read NAL unit length
+ * @param bs input data (bitstream)
+ * @return the length of NAL unit on success, 0 value on failure
+ */
+static uint32_t read_nal_unit_length(const uint8_t *bs, int bs_size, AVCodecContext *avctx)
+{
+ uint32_t len = 0;
+ XEVD_INFO info;
+ int ret;
+
+ if (bs_size == XEVD_NAL_UNIT_LENGTH_BYTE) {
+ ret = xevd_info((void *)bs, XEVD_NAL_UNIT_LENGTH_BYTE, 1, &info);
+ if (XEVD_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot get bitstream information\n");
+ return 0;
+ }
+ len = info.nalu_len;
+ if (len == 0) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid bitstream size! [%d]\n", bs_size);
+ return 0;
+ }
+ }
+
+ return len;
+}
+
+/**
+ * @param[in] xectx the structure that stores all the state associated with the instance of Xeve MPEG-5 EVC decoder
+ * @param[out] avctx codec context
+ * @return 0 on success, negative value on failure
+ */
+static int export_stream_params(const XevdContext *xectx, AVCodecContext *avctx)
+{
+ int ret;
+ int size;
+ int color_space;
+
+ avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
+
+ size = 4;
+ ret = xevd_config(xectx->id, XEVD_CFG_GET_CODED_WIDTH, &avctx->coded_width, &size);
+ if (XEVD_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to get coded_width\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ ret = xevd_config(xectx->id, XEVD_CFG_GET_CODED_HEIGHT, &avctx->coded_height, &size);
+ if (XEVD_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to get coded_height\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ ret = xevd_config(xectx->id, XEVD_CFG_GET_WIDTH, &avctx->width, &size);
+ if (XEVD_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to get width\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ ret = xevd_config(xectx->id, XEVD_CFG_GET_HEIGHT, &avctx->height, &size);
+ if (XEVD_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to get height\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ ret = xevd_config(xectx->id, XEVD_CFG_GET_COLOR_SPACE, &color_space, &size);
+ if (XEVD_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to get color_space\n");
+ return AVERROR_EXTERNAL;
+ }
+ switch(color_space) {
+ case XEVD_CS_YCBCR400_10LE:
+ avctx->pix_fmt = AV_PIX_FMT_GRAY10LE;
+ break;
+ case XEVD_CS_YCBCR420_10LE:
+ avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE;
+ break;
+ case XEVD_CS_YCBCR422_10LE:
+ avctx->pix_fmt = AV_PIX_FMT_YUV422P10LE;
+ break;
+ case XEVD_CS_YCBCR444_10LE:
+ avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "Unknown color space\n");
+ avctx->pix_fmt = AV_PIX_FMT_NONE;
+ return AVERROR_INVALIDDATA;
+ }
+
+ // the function returns sps->num_reorder_pics
+ ret = xevd_config(xectx->id, XEVD_CFG_GET_MAX_CODING_DELAY, &avctx->max_b_frames, &size);
+ if (XEVD_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to get max_coding_delay\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ avctx->has_b_frames = (avctx->max_b_frames) ? 1 : 0;
+
+ avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
+ avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
+ avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
+
+ return 0;
+}
+
+/**
+ * @brief Copy image in imgb to frame.
+ *
+ * @param avctx codec context
+ * @param[in] imgb
+ * @param[out] frame
+ * @return 0 on success, negative value on failure
+ */
+static int libxevd_image_copy(struct AVCodecContext *avctx, XEVD_IMGB *imgb, struct AVFrame *frame)
+{
+ int ret;
+ if (imgb->cs != XEVD_CS_YCBCR420_10LE) {
+ av_log(avctx, AV_LOG_ERROR, "Not supported pixel format: %s\n", av_get_pix_fmt_name(avctx->pix_fmt));
+ return AVERROR_INVALIDDATA;
+ }
+
+ if (imgb->w[0] != avctx->width || imgb->h[0] != avctx->height) { // stream resolution changed
+ if (ff_set_dimensions(avctx, imgb->w[0], imgb->h[0]) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot set new dimension\n");
+ return AVERROR_INVALIDDATA;
+ }
+ }
+
+ if (ret = ff_get_buffer(avctx, frame, 0) < 0) {
+ return ret;
+ }
+
+ av_image_copy(frame->data, frame->linesize, (const uint8_t **)imgb->a,
+ imgb->s, avctx->pix_fmt,
+ imgb->w[0], imgb->h[0]);
+
+ return 0;
+}
+
+/**
+ * Initialize decoder
+ * Create a decoder instance and allocate all the needed resources
+ *
+ * @param avctx codec context
+ * @return 0 on success, negative error code on failure
+ */
+static av_cold int libxevd_init(AVCodecContext *avctx)
+{
+ XevdContext *xectx = avctx->priv_data;
+ XEVD_CDSC *cdsc = &(xectx->cdsc);
+
+ /* read configurations and set values for created descriptor (XEVD_CDSC) */
+ get_conf(avctx, cdsc);
+
+ /* create decoder */
+ xectx->id = xevd_create(&(xectx->cdsc), NULL);
+ if (xectx->id == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot create XEVD encoder\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ return 0;
+}
+
+/**
+ * Decode picture
+ *
+ * @param avctx codec context
+ * @param[out] frame decoded frame
+ * @param[out] got_frame_ptr decoder sets to 0 or 1 to indicate that a
+ * non-empty frame or subtitle was returned in
+ * outdata.
+ * @param[in] avpkt AVPacket containing encoded data to be decoded
+ *
+ * @return amount of bytes read from the packet on success, negative error
+ * code on failure
+ */
+static int libxevd_decode(struct AVCodecContext *avctx, struct AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
+{
+ XevdContext *xectx = NULL;
+ XEVD_IMGB *imgb = NULL;
+ XEVD_STAT stat;
+ XEVD_BITB bitb;
+ int xevd_ret, nalu_size, bs_read_pos;
+ int ret = 0;
+
+ xectx = avctx->priv_data;
+
+ if (avpkt->size > 0) {
+ bs_read_pos = 0;
+ imgb = NULL;
+ while(avpkt->size > (bs_read_pos + XEVD_NAL_UNIT_LENGTH_BYTE)) {
+ memset(&stat, 0, sizeof(XEVD_STAT));
+ memset(&bitb, 0, sizeof(XEVD_BITB));
+
+ nalu_size = read_nal_unit_length(avpkt->data + bs_read_pos, XEVD_NAL_UNIT_LENGTH_BYTE, avctx);
+ if (nalu_size == 0) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid bitstream\n");
+ ret = AVERROR_INVALIDDATA;
+ goto ERR;
+ }
+ bs_read_pos += XEVD_NAL_UNIT_LENGTH_BYTE;
+
+ bitb.addr = avpkt->data + bs_read_pos;
+ bitb.ssize = nalu_size;
+
+ /* main decoding block */
+ xevd_ret = xevd_decode(xectx->id, &bitb, &stat);
+ if (XEVD_FAILED(xevd_ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to decode bitstream\n");
+ ret = AVERROR_EXTERNAL;
+ goto ERR;
+ }
+
+ bs_read_pos += nalu_size;
+
+ if (stat.nalu_type == XEVD_NUT_SPS) { // EVC stream parameters changed
+ if ((ret = export_stream_params(xectx, avctx)) != 0)
+ goto ERR;
+ }
+
+ if (stat.read != nalu_size)
+ av_log(avctx, AV_LOG_INFO, "Different reading of bitstream (in:%d, read:%d)\n,", nalu_size, stat.read);
+ if (stat.fnum >= 0) {
+ // already has a decoded image
+ if (imgb) {
+ // xevd_pull uses pool of objects of type XEVD_IMGB.
+ // The pool size is equal MAX_PB_SIZE (26), so release object when it is no more needed
+ imgb->release(imgb);
+ imgb = NULL;
+ }
+ xevd_ret = xevd_pull(xectx->id, &imgb);
+ if (XEVD_FAILED(xevd_ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to pull the decoded image (xevd error code: %d, frame#=%d)\n", xevd_ret, stat.fnum);
+ ret = AVERROR_EXTERNAL;
+ goto ERR;
+ } else if (xevd_ret == XEVD_OK_FRM_DELAYED) {
+ if (imgb) {
+ // xevd_pull uses pool of objects of type XEVD_IMGB.
+ // The pool size is equal MAX_PB_SIZE (26), so release object when it is no more needed
+ imgb->release(imgb);
+ imgb = NULL;
+ }
+ }
+ if (imgb) {
+ int ret = libxevd_image_copy(avctx, imgb, frame);
+ if(ret < 0) {
+ goto ERR;
+ }
+
+ frame->pts = avpkt->pts;
+ *got_frame_ptr = 1;
+
+ // xevd_pull uses pool of objects of type XEVD_IMGB.
+ // The pool size is equal MAX_PB_SIZE (26), so release object when it is no more needed
+ imgb->release(imgb);
+ imgb = NULL;
+ } else
+ *got_frame_ptr = 0;
+ }
+ }
+ } else { // bumping
+ xevd_ret = xevd_pull(xectx->id, &(imgb));
+ if (xevd_ret == XEVD_ERR_UNEXPECTED) { // bumping process completed
+ *got_frame_ptr = 0;
+ return 0;
+ } else if (XEVD_FAILED(xevd_ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to pull the decoded image (xevd error code: %d)\n", xevd_ret);
+ ret = AVERROR_EXTERNAL;
+ goto ERR;
+ }
+ if (imgb) {
+ int ret = libxevd_image_copy(avctx, imgb, frame);
+ if(ret < 0) {
+ goto ERR;
+ }
+
+ frame->pts = avpkt->pts;
+ *got_frame_ptr = 1;
+
+ // xevd_pull uses pool of objects of type XEVD_IMGB.
+ // The pool size is equal MAX_PB_SIZE (26), so release object when it is no more needed
+ imgb->release(imgb);
+ imgb = NULL;
+ } else
+ *got_frame_ptr = 0;
+ }
+
+ return avpkt->size;
+
+ERR:
+ if (imgb) {
+ imgb->release(imgb);
+ imgb = NULL;
+ }
+ *got_frame_ptr = 0;
+
+ return ret;
+}
+
+/**
+ * Destroy decoder
+ *
+ * @param avctx codec context
+ * @return 0 on success
+ */
+static av_cold int libxevd_close(AVCodecContext *avctx)
+{
+ XevdContext *xectx = avctx->priv_data;
+ if (xectx->id) {
+ xevd_delete(xectx->id);
+ xectx->id = NULL;
+ }
+
+ return 0;
+}
+
+#define OFFSET(x) offsetof(XevdContext, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+
+static const AVClass libxevd_class = {
+ .class_name = "libxevd",
+ .item_name = av_default_item_name,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
+const FFCodec ff_libxevd_decoder = {
+ .p.name = "evc",
+ .p.long_name = NULL_IF_CONFIG_SMALL("EVC / MPEG-5 Essential Video Coding (EVC)"),
+ .p.type = AVMEDIA_TYPE_VIDEO,
+ .p.id = AV_CODEC_ID_EVC,
+ .init = libxevd_init,
+ FF_CODEC_DECODE_CB(libxevd_decode),
+ .close = libxevd_close,
+ .priv_data_size = sizeof(XevdContext),
+ .p.priv_class = &libxevd_class,
+ .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_DR1,
+ .p.profiles = NULL_IF_CONFIG_SMALL(ff_evc_profiles),
+ .p.wrapper_name = "libxevd",
+ .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_NOT_INIT_THREADSAFE,
+};
--
2.17.1
1
0
07 Oct '22
- Added EVC encoder wrapper
- Changes in project configuration file and libavcodec Makefile
- Added documentation for xeve wrapper
Signed-off-by: Dawid Kozinski <d.kozinski(a)samsung.com>
---
configure | 4 +
doc/encoders.texi | 69 +++++
doc/general_contents.texi | 11 +
libavcodec/Makefile | 1 +
libavcodec/allcodecs.c | 1 +
libavcodec/libxeve.c | 615 ++++++++++++++++++++++++++++++++++++++
6 files changed, 701 insertions(+)
create mode 100644 libavcodec/libxeve.c
diff --git a/configure b/configure
index 957b7fe13e..7f430995a6 100755
--- a/configure
+++ b/configure
@@ -291,6 +291,7 @@ External library support:
--enable-libwebp enable WebP encoding via libwebp [no]
--enable-libx264 enable H.264 encoding via x264 [no]
--enable-libx265 enable HEVC encoding via x265 [no]
+ --enable-libxeve enable EVC encoding via libxeve [no]
--enable-libxavs enable AVS encoding via xavs [no]
--enable-libxavs2 enable AVS2 encoding via xavs2 [no]
--enable-libxcb enable X11 grabbing using XCB [autodetect]
@@ -1876,6 +1877,7 @@ EXTERNAL_LIBRARY_LIST="
libvorbis
libvpx
libwebp
+ libxeve
libxml2
libzimg
libzmq
@@ -3407,6 +3409,7 @@ libx264rgb_encoder_select="libx264_encoder"
libx265_encoder_deps="libx265"
libxavs_encoder_deps="libxavs"
libxavs2_encoder_deps="libxavs2"
+libxeve_encoder_deps="libxeve"
libxvid_encoder_deps="libxvid"
libzvbi_teletext_decoder_deps="libzvbi"
vapoursynth_demuxer_deps="vapoursynth"
@@ -6739,6 +6742,7 @@ enabled libx265 && require_pkg_config libx265 x265 x265.h x265_api_get
require_cpp_condition libx265 x265.h "X265_BUILD >= 70"
enabled libxavs && require libxavs "stdint.h xavs.h" xavs_encoder_encode "-lxavs $pthreads_extralibs $libm_extralibs"
enabled libxavs2 && require_pkg_config libxavs2 "xavs2 >= 1.3.0" "stdint.h xavs2.h" xavs2_api_get
+enabled libxeve && require_pkg_config libxeve "xeve >= 0.4.0" "xeve.h" xeve_encode
enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore
enabled libzimg && require_pkg_config libzimg "zimg >= 2.7.0" zimg.h zimg_get_api_version
enabled libzmq && require_pkg_config libzmq "libzmq >= 4.2.1" zmq.h zmq_ctx_new
diff --git a/doc/encoders.texi b/doc/encoders.texi
index 1a5216f8eb..4c8c2871ec 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -2894,6 +2894,75 @@ ffmpeg -i input -c:v libxavs2 -xavs2-params RdoqLevel=0 output.avs2
@end example
@end table
+@section libxeve
+
+eXtra-fast Essential Video Encoder (XEVE) MPEG-5 EVC encoder wrapper.
+The xeve-equivalent options or values are listed in parentheses for easy migration.
+
+This encoder requires the presence of the libxeve headers and library
+during configuration. You need to explicitly configure the build with
+@option{--enable-libxeve}.
+
+@float NOTE
+Many libxeve encoder options are mapped to FFmpeg global codec options,
+while unique encoder options are provided through private options.
+Additionally the xeve-params private options allows one to pass a list
+of key=value tuples as accepted by the libxeve @code{parse_xeve_params} function.
+@end float
+
+The xeve project website is at @url{https://github.com/mpeg5/xeve}.
+
+@subsection Options
+
+The following options are supported by the libxeve wrapper.
+The xeve-equivalent options or values are listed in parentheses for easy migration.
+
+@float NOTE
+To reduce the duplication of documentation, only the private options
+and some others requiring special attention are documented here. For
+the documentation of the undocumented generic options, see
+@ref{codec-options,,the Codec Options chapter}.
+@end float
+
+@float NOTE
+To get a more accurate and extensive documentation of the libxeve options,
+invoke the command @code{xeve_app --help} or consult the libxeve documentation.
+@end float
+
+@table @option
+@item b (@emph{bitrate})
+Set target video bitrate in bits/s.
+Note that FFmpeg's b option is expressed in bits/s, while xeve's bitrate is in kilobits/s.
+
+@item bf (@emph{bframes})
+Set the maximum number of B frames (1,3,7,15).
+
+@item g (@emph{keyint})
+Set the GOP size (I-picture period).
+
+@item preset (@emph{preset})
+Set the xeve preset.
+Set the encoder preset value to determine encoding speed [fast, medium, slow, placebo]
+
+@item tune (@emph{tune})
+Set the encoder tune parameter [psnr, zerolatency]
+
+@item profile (@emph{profile})
+Set the encoder profile [0: baselie; 1: main]
+
+@item crf (@emph{crf})
+Set the quality for constant quality mode.
+Constant rate factor <10..49> [default: 32]
+
+@item qp (@emph{qp})
+Set constant quantization rate control method parameter.
+Quantization parameter qp <0..51> [default: 32]
+
+@item threads (@emph{threads})
+Force to use a specific number of threads
+
+@end table
+
@section libxvid
Xvid MPEG-4 Part 2 encoder wrapper.
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index b005cb6c3e..417f0ab64f 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -343,6 +343,14 @@ libxavs2 is under the GNU Public License Version 2 or later
details), you must upgrade FFmpeg's license to GPL in order to use it.
@end float
+@section eXtra-fast Essential Video Encoder (XEVE)
+
+FFmpeg can make use of the XEVE library for EVC video encoding.
+
+Go to @url{https://github.com/mpeg5/xeve} and follow the instructions for
+installing the XEVE library. Then pass @code{--enable-libxeve} to configure to
+enable it.
+
@section ZVBI
ZVBI is a VBI decoding library which can be used by FFmpeg to decode DVB
@@ -595,6 +603,7 @@ library:
@item raw DTS @tab X @tab X
@item raw DTS-HD @tab @tab X
@item raw E-AC-3 @tab X @tab X
+@item raw EVC @tab X @tab X
@item raw FLAC @tab X @tab X
@item raw GSM @tab @tab X
@item raw H.261 @tab X @tab X
@@ -934,6 +943,8 @@ following image formats are supported:
@item Electronic Arts TQI video @tab @tab X
@item Escape 124 @tab @tab X
@item Escape 130 @tab @tab X
+@item EVC / MPEG-5 Part 1 @tab X @tab X
+ @tab encoding supported through external library libxeve
@item FFmpeg video codec #1 @tab X @tab X
@tab lossless codec (fourcc: FFV1)
@item Flash Screen Video v1 @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 0fdf141d54..8944e2a0d6 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1107,6 +1107,7 @@ OBJS-$(CONFIG_LIBX264_ENCODER) += libx264.o
OBJS-$(CONFIG_LIBX265_ENCODER) += libx265.o
OBJS-$(CONFIG_LIBXAVS_ENCODER) += libxavs.o
OBJS-$(CONFIG_LIBXAVS2_ENCODER) += libxavs2.o
+OBJS-$(CONFIG_LIBXEVE_ENCODER) += libxeve.o
OBJS-$(CONFIG_LIBXVID_ENCODER) += libxvid.o
OBJS-$(CONFIG_LIBZVBI_TELETEXT_DECODER) += libzvbi-teletextdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index cfeb01ac1c..5ce398f9ee 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -807,6 +807,7 @@ extern LIBX264_CONST FFCodec ff_libx264_encoder;
#endif
extern const FFCodec ff_libx264rgb_encoder;
extern FFCodec ff_libx265_encoder;
+extern const FFCodec ff_libxeve_encoder;
extern const FFCodec ff_libxavs_encoder;
extern const FFCodec ff_libxavs2_encoder;
extern const FFCodec ff_libxvid_encoder;
diff --git a/libavcodec/libxeve.c b/libavcodec/libxeve.c
new file mode 100644
index 0000000000..38d70a1d2f
--- /dev/null
+++ b/libavcodec/libxeve.c
@@ -0,0 +1,615 @@
+/*
+ * libxeve encoder
+ * EVC (MPEG-5 Essential Video Coding) encoding using XEVE MPEG-5 EVC encoder library
+ *
+ * Copyright (C) 2021 Dawid Kozinski <d.kozinski(a)samsung.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ */
+
+#include <float.h>
+#include <stdlib.h>
+
+#include <xeve.h>
+
+#include "libavutil/internal.h"
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/pixfmt.h"
+#include "libavutil/time.h"
+#include "libavutil/cpu.h"
+#include "libavutil/avstring.h"
+
+#include "avcodec.h"
+#include "internal.h"
+#include "packet_internal.h"
+#include "codec_internal.h"
+#include "profiles.h"
+#include "encode.h"
+
+#define MAX_BS_BUF (16*1024*1024)
+
+/**
+ * Error codes
+ */
+#define XEVE_PARAM_BAD_NAME -100
+#define XEVE_PARAM_BAD_VALUE -200
+
+/**
+ * Encoder states
+ *
+ * STATE_ENCODING - the encoder receives and processes input frames
+ * STATE_BUMPING - there are no more input frames, however the encoder still processes previously received data
+ */
+typedef enum State {
+ STATE_ENCODING,
+ STATE_BUMPING,
+} State;
+
+/**
+ * The structure stores all the states associated with the instance of Xeve MPEG-5 EVC encoder
+ */
+typedef struct XeveContext {
+ const AVClass *class;
+
+ XEVE id; // XEVE instance identifier
+ XEVE_CDSC cdsc; // coding parameters i.e profile, width & height of input frame, num of therads, frame rate ...
+ XEVE_BITB bitb; // bitstream buffer (output)
+ XEVE_STAT stat; // encoding status (output)
+ XEVE_IMGB imgb; // image buffer (input)
+
+ State state; // encoder state (skipping, encoding, bumping)
+
+ int profile_id; // encoder profile (main, baseline)
+ int preset_id; // preset of xeve ( fast, medium, slow, placebo)
+ int tune_id; // tune of xeve (psnr, zerolatency)
+
+ // variables for rate control modes
+ int rc_mode; // Rate control mode [ 0(CQP) / 1(ABR) / 2(CRF) ]
+ int qp; // quantization parameter (QP) [0,51]
+ int crf; // constant rate factor (CRF) [10,49]
+
+ int hash; // embed picture signature (HASH) for conformance checking in decoding
+ int sei_info; // embed Supplemental enhancement information while encoding
+
+ int color_format; // input data color format: currently only XEVE_CF_YCBCR420 is supported
+
+ AVDictionary *xeve_params;
+} XeveContext;
+
+/**
+ * Convert FFmpeg pixel format (AVPixelFormat) to XEVE pre-defined color format
+ *
+ * @param[in] av_pix_fmt pixel format (@see https://ffmpeg.org/doxygen/trunk/pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf8…)
+ * @param[out] xeve_col_fmt XEVE pre-defined color format (@see xeve.h)
+ *
+ * @return 0 on success, negative value on failure
+ */
+static int libxeve_color_fmt(enum AVPixelFormat av_pix_fmt, int *xeve_col_fmt)
+{
+ switch (av_pix_fmt) {
+ case AV_PIX_FMT_YUV420P:
+ *xeve_col_fmt = XEVE_CF_YCBCR420;
+ break;
+ case AV_PIX_FMT_YUV420P10:
+ *xeve_col_fmt = XEVE_CF_YCBCR420;
+ break;
+ default:
+ *xeve_col_fmt = XEVE_CF_UNKNOWN;
+ return AVERROR_INVALIDDATA;
+ }
+
+ return 0;
+}
+
+/**
+ * Convert FFmpeg pixel format (AVPixelFormat) into XEVE pre-defined color space
+ *
+ * @param[in] px_fmt pixel format (@see https://ffmpeg.org/doxygen/trunk/pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf8…)
+ *
+ * @return XEVE pre-defined color space (@see xeve.h) on success, XEVE_CF_UNKNOWN on failure
+ */
+static int libxeve_color_space(enum AVPixelFormat av_pix_fmt)
+{
+ /* color space of input image */
+ int cs = XEVE_CF_UNKNOWN;
+
+ switch (av_pix_fmt) {
+ case AV_PIX_FMT_YUV420P:
+ cs = XEVE_CS_YCBCR420;
+ break;
+ case AV_PIX_FMT_YUV420P10:
+#if AV_HAVE_BIGENDIAN
+ cs = XEVE_CS_SET(XEVE_CF_YCBCR420, 10, 1);
+#else
+ cs = XEVE_CS_YCBCR420_10LE;
+#endif
+
+ break;
+ default:
+ cs = XEVE_CF_UNKNOWN;
+ break;
+ }
+
+ return cs;
+}
+
+/**
+ * The function returns a pointer to the object of the XEVE_CDSC type.
+ * XEVE_CDSC contains all encoder parameters that should be initialized before the encoder is used.
+ *
+ * The field values of the XEVE_CDSC structure are populated based on:
+ * - the corresponding field values of the AvCodecConetxt structure,
+ * - the xeve encoder specific option values,
+ * (the full list of options available for xeve encoder is displayed after executing the command ./ffmpeg --help encoder = libxeve)
+ *
+ * The order of processing input data and populating the XEVE_CDSC structure
+ * 1) first, the fields of the AVCodecContext structure corresponding to the provided input options are processed,
+ * (i.e -pix_fmt yuv420p -s:v 1920x1080 -r 30 -profile:v 0)
+ * 2) then xeve-specific options added as AVOption to the xeve AVCodec implementation
+ * (i.e -preset 0)
+ *
+ * Keep in mind that, there are options that can be set in different ways.
+ * In this case, please follow the above-mentioned order of processing.
+ * The most recent assignments overwrite the previous values.
+ *
+ * @param[in] avctx codec context (AVCodecContext)
+ * @param[out] cdsc contains all Xeve MPEG-5 EVC encoder encoder parameters that should be initialized before the encoder is use
+ *
+ * @return 0 on success, negative error code on failure
+ */
+static int get_conf(AVCodecContext *avctx, XEVE_CDSC *cdsc)
+{
+ XeveContext *xectx = NULL;
+ int ret;
+
+ xectx = avctx->priv_data;
+
+ /* initialize xeve_param struct with default values */
+ ret = xeve_param_default(&cdsc->param);
+ if (XEVE_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot set_default parameter\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ /* read options from AVCodecContext */
+ if (avctx->width > 0)
+ cdsc->param.w = avctx->width;
+
+ if (avctx->height > 0)
+ cdsc->param.h = avctx->height;
+
+ if (avctx->framerate.num > 0) {
+ // fps can be float number, but xeve API doesn't support it
+ cdsc->param.fps = (int)(((float)avctx->framerate.num / avctx->framerate.den) + 0.5);
+ }
+
+ if (avctx->gop_size >= 0) // GOP size (key-frame interval, I-picture period)
+ cdsc->param.keyint = avctx->gop_size; // 0: only one I-frame at the first time; 1: every frame is coded in I-frame
+
+ if (avctx->max_b_frames == 0 || avctx->max_b_frames == 1 || avctx->max_b_frames == 3 ||
+ avctx->max_b_frames == 7 || avctx->max_b_frames == 15) // number of b-frames
+ cdsc->param.bframes = avctx->max_b_frames;
+ else {
+ av_log(avctx, AV_LOG_ERROR, "Incorrect value for maximum number of B frames: (%d) \n"
+ "Acceptable values for bf option (maximum number of B frames) are 0,1,3,7 or 15\n", avctx->max_b_frames);
+ return AVERROR_INVALIDDATA;
+ }
+
+ if (avctx->level >= 0)
+ cdsc->param.level_idc = avctx->level;
+
+ if (avctx->rc_buffer_size) // VBV buf size
+ cdsc->param.vbv_bufsize = (int)(avctx->rc_buffer_size / 1000);
+
+ cdsc->param.rc_type = xectx->rc_mode;
+
+ if (xectx->rc_mode == XEVE_RC_CQP) {
+ cdsc->param.qp = xectx->qp;
+ } else if (xectx->rc_mode == XEVE_RC_ABR) {
+ if (avctx->bit_rate / 1000 > INT_MAX || avctx->rc_max_rate / 1000 > INT_MAX) {
+ av_log(avctx, AV_LOG_ERROR, "Not supported bitrate bit_rate and rc_max_rate > %d000\n", INT_MAX);
+ return AVERROR_INVALIDDATA;
+ }
+ cdsc->param.bitrate = (int)(avctx->bit_rate / 1000);
+ } else if (xectx->rc_mode == XEVE_RC_CRF) {
+ cdsc->param.crf = xectx->crf;
+ } else {
+ av_log(avctx, AV_LOG_ERROR, "Not supported rate control type: %d\n", xectx->rc_mode);
+ return AVERROR_INVALIDDATA;
+ }
+
+ if (avctx->thread_count <= 0) {
+ int cpu_count = av_cpu_count();
+ cdsc->param.threads = (cpu_count < XEVE_MAX_THREADS) ? cpu_count : XEVE_MAX_THREADS;
+ } else if (avctx->thread_count > XEVE_MAX_THREADS)
+ cdsc->param.threads = XEVE_MAX_THREADS;
+ else
+ cdsc->param.threads = avctx->thread_count;
+
+
+ libxeve_color_fmt(avctx->pix_fmt, &xectx->color_format);
+
+ cdsc->param.cs = XEVE_CS_SET(xectx->color_format, cdsc->param.codec_bit_depth, AV_HAVE_BIGENDIAN);
+
+ cdsc->max_bs_buf_size = MAX_BS_BUF;
+
+ ret = xeve_param_ppt(&cdsc->param, xectx->profile_id, xectx->preset_id, xectx->tune_id);
+ if (XEVE_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot set profile(%d), preset(%d), tune(%d)\n", xectx->profile_id, xectx->preset_id, xectx->tune_id);
+ return AVERROR_EXTERNAL;
+ }
+
+ return 0;
+}
+
+/**
+ * Set XEVE_CFG_SET_USE_PIC_SIGNATURE for encoder
+ *
+ * @param[in] logger context
+ * @param[in] id XEVE encodec instance identifier
+ * @param[in] ctx the structure stores all the states associated with the instance of Xeve MPEG-5 EVC encoder
+ *
+ * @return 0 on success, negative error code on failure
+ */
+static int set_extra_config(AVCodecContext *avctx, XEVE id, XeveContext *ctx)
+{
+ int ret, size;
+ size = 4;
+
+ // embed SEI messages identifying encoder parameters and command line arguments
+ // - 0: off\n"
+ // - 1: emit sei info"
+ //
+ // SEI - Supplemental enhancement information contains information
+ // that is not necessary to decode the samples of coded pictures from VCL NAL units.
+ // Some SEI message information is required to check bitstream conformance
+ // and for output timing decoder conformance.
+ // @see ISO_IEC_23094-1_2020 7.4.3.5
+ // @see ISO_IEC_23094-1_2020 Annex D
+ ret = xeve_config(id, XEVE_CFG_SET_SEI_CMD, &ctx->sei_info, &size); // sei_cmd_info
+ if (XEVE_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to set config for sei command info messages\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ ret = xeve_config(id, XEVE_CFG_SET_USE_PIC_SIGNATURE, &ctx->hash, &size);
+ if (XEVE_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to set config for picture signature\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ return 0;
+}
+
+/**
+ * @brief Switch encoder to bumping mode
+ *
+ * @param id XEVE encodec instance identifier
+ * @return 0 on success, negative error code on failure
+ */
+static int setup_bumping(XEVE id)
+{
+ int val = 1;
+ int size = sizeof(int);
+ if (XEVE_FAILED(xeve_config(id, XEVE_CFG_SET_FORCE_OUT, (void *)(&val), &size)))
+ return AVERROR_EXTERNAL;
+
+ return 0;
+}
+
+/**
+ * @brief Initialize eXtra-fast Essential Video Encoder codec
+ * Create an encoder instance and allocate all the needed resources
+ *
+ * @param avctx codec context
+ * @return 0 on success, negative error code on failure
+ */
+static av_cold int libxeve_init(AVCodecContext *avctx)
+{
+ XeveContext *xectx = avctx->priv_data;
+ unsigned char *bs_buf = NULL;
+ int i;
+ int shift_h = 0;
+ int shift_v = 0;
+ int width_chroma = 0;
+ int height_chroma = 0;
+ XEVE_IMGB *imgb = NULL;
+ int ret = 0;
+
+ XEVE_CDSC *cdsc = &(xectx->cdsc);
+
+ /* allocate bitstream buffer */
+ bs_buf = av_malloc(MAX_BS_BUF);
+ if (bs_buf == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot allocate bitstream buffer\n");
+ return AVERROR(ENOMEM);
+ }
+ xectx->bitb.addr = bs_buf;
+ xectx->bitb.bsize = MAX_BS_BUF;
+
+ /* read configurations and set values for created descriptor (XEVE_CDSC) */
+ if ((ret = get_conf(avctx, cdsc)) != 0) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot get configuration\n");
+ return AVERROR(EINVAL);
+ }
+
+ if ((ret = xeve_param_check(&cdsc->param)) != 0) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid configuration\n");
+ return AVERROR(EINVAL);
+ }
+
+ {
+ AVDictionaryEntry *en = NULL;
+ while (en = av_dict_get(xectx->xeve_params, "", en, AV_DICT_IGNORE_SUFFIX)) {
+ if ((ret = xeve_param_parse(&cdsc->param, en->key, en->value)) < 0) {
+ av_log(avctx, AV_LOG_WARNING,
+ "Error parsing option '%s = %s'.\n",
+ en->key, en->value);
+ }
+ }
+ }
+
+ /* create encoder */
+ xectx->id = xeve_create(cdsc, NULL);
+ if (xectx->id == NULL) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot create XEVE encoder\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ if ((ret = set_extra_config(avctx, xectx->id, xectx)) != 0) {
+ av_log(avctx, AV_LOG_ERROR, "Cannot set extra configuration\n");
+ return AVERROR(EINVAL);
+ }
+
+ if ((ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v)) != 0) {
+ av_log(avctx, AV_LOG_ERROR, "Failed to get chroma shift\n");
+ return AVERROR(EINVAL);
+ }
+
+ // Chroma subsampling
+ //
+ // YUV format explanation
+ // shift_h == 1 && shift_v == 1 : YUV420
+ // shift_h == 1 && shift_v == 0 : YUV422
+ // shift_h == 0 && shift_v == 0 : YUV444
+ //
+ width_chroma = AV_CEIL_RSHIFT(avctx->width, shift_h);
+ height_chroma = AV_CEIL_RSHIFT(avctx->height, shift_v);
+
+ /* set default values for input image buffer */
+ imgb = &xectx->imgb;
+ imgb->cs = libxeve_color_space(avctx->pix_fmt);
+ imgb->np = 3; /* only for yuv420p, yuv420ple */
+
+ for (i = 0; i < imgb->np; i++)
+ imgb->x[i] = imgb->y[i] = 0;
+
+ imgb->w[0] = imgb->aw[0] = avctx->width; // width luma
+ imgb->w[1] = imgb->w[2] = imgb->aw[1] = imgb->aw[2] = width_chroma;
+ imgb->h[0] = imgb->ah[0] = avctx->height; // height luma
+ imgb->h[1] = imgb->h[2] = imgb->ah[1] = imgb->ah[2] = height_chroma;
+
+ xectx->state = STATE_ENCODING;
+
+ return 0;
+}
+
+/**
+ * Encode raw data frame into EVC packet
+ *
+ * @param[in] avctx codec context
+ * @param[out] pkt output AVPacket containing encoded data
+ * @param[in] frame AVFrame containing the raw data to be encoded
+ * @param[out] got_packet encoder sets to 0 or 1 to indicate that a
+ * non-empty packet was returned in pkt
+ *
+ * @return 0 on success, negative error code on failure
+ */
+static int libxeve_encode(AVCodecContext *avctx, AVPacket *avpkt,
+ const AVFrame *frame, int *got_packet)
+{
+ XeveContext *xectx = avctx->priv_data;
+ int ret = -1;
+
+ // No more input frames are available but encoder still can have some data in its internal buffer to process
+ // and some frames to dump.
+ if (xectx->state == STATE_ENCODING && frame == NULL) {
+ if (setup_bumping(xectx->id) == 0)
+ xectx->state = STATE_BUMPING; // Entering bumping process
+ else {
+ av_log(avctx, AV_LOG_ERROR, "Failed to setup bumping\n");
+ return 0;
+ }
+ }
+
+ if (xectx->state == STATE_ENCODING) {
+ int i;
+ XEVE_IMGB *imgb = NULL;
+
+ imgb = &xectx->imgb;
+
+ for (i = 0; i < imgb->np; i++) {
+ imgb->a[i] = frame->data[i];
+ imgb->s[i] = frame->linesize[i];
+ }
+
+ imgb->ts[0] = frame->pts;
+ imgb->ts[1] = 0;
+
+ /* push image to encoder */
+ ret = xeve_push(xectx->id, imgb);
+ if (XEVE_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "xeve_push() failed\n");
+ return AVERROR_EXTERNAL;
+ }
+ }
+ if (xectx->state == STATE_ENCODING || xectx->state == STATE_BUMPING) {
+ /* encoding */
+ ret = xeve_encode(xectx->id, &(xectx->bitb), &(xectx->stat));
+ if (XEVE_FAILED(ret)) {
+ av_log(avctx, AV_LOG_ERROR, "xeve_encode() failed\n");
+ return AVERROR_EXTERNAL;
+ }
+
+ /* store bitstream */
+ if (ret == XEVE_OK_OUT_NOT_AVAILABLE) { // Return OK but picture is not available yet
+ *got_packet = 0;
+ return 0;
+ } else if (ret == XEVE_OK) {
+ int av_pic_type;
+
+ if (xectx->stat.write > 0) {
+
+ ret = ff_get_encode_buffer(avctx, avpkt, xectx->stat.write, 0);
+ if (ret < 0)
+ return ret;
+
+ memcpy(avpkt->data, xectx->bitb.addr, xectx->stat.write);
+
+ avpkt->pts = xectx->bitb.ts[0];
+ avpkt->dts = xectx->bitb.ts[1];
+
+ switch(xectx->stat.stype) {
+ case XEVE_ST_I:
+ av_pic_type = AV_PICTURE_TYPE_I;
+ avpkt->flags |= AV_PKT_FLAG_KEY;
+ break;
+ case XEVE_ST_P:
+ av_pic_type = AV_PICTURE_TYPE_P;
+ break;
+ case XEVE_ST_B:
+ av_pic_type = AV_PICTURE_TYPE_B;
+ break;
+ case XEVE_ST_UNKNOWN:
+ av_log(avctx, AV_LOG_ERROR, "Unknown slice type\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ ff_side_data_set_encoder_stats(avpkt, xectx->stat.qp * FF_QP2LAMBDA, NULL, 0, av_pic_type);
+
+ *got_packet = 1;
+ }
+ } else if (ret == XEVE_OK_NO_MORE_FRM) {
+ // Return OK but no more frames
+ return 0;
+ } else {
+ av_log(avctx, AV_LOG_ERROR, "Invalid return value: %d\n", ret);
+ return AVERROR_EXTERNAL;
+ }
+ } else {
+ av_log(avctx, AV_LOG_ERROR, "Udefined encoder state\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ return 0;
+}
+
+/**
+ * Destroy the encoder and release all the allocated resources
+ *
+ * @param avctx codec context
+ * @return 0 on success, negative error code on failure
+ */
+static av_cold int libxeve_close(AVCodecContext *avctx)
+{
+ XeveContext *xectx = avctx->priv_data;
+
+ if (xectx->id) {
+ xeve_delete(xectx->id);
+ xectx->id = NULL;
+ }
+
+ av_free(xectx->bitb.addr); /* release bitstream buffer */
+
+ return 0;
+}
+
+#define OFFSET(x) offsetof(XeveContext, x)
+#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+
+static const enum AVPixelFormat supported_pixel_formats[] = {
+ AV_PIX_FMT_YUV420P,
+ AV_PIX_FMT_YUV420P10,
+ AV_PIX_FMT_NONE
+};
+
+// Consider using following options (./ffmpeg --help encoder=libxeve)
+//
+static const AVOption libxeve_options[] = {
+ { "preset", "Encoding preset for setting encoding speed", OFFSET(preset_id), AV_OPT_TYPE_INT, { .i64 = XEVE_PRESET_MEDIUM }, XEVE_PRESET_DEFAULT, XEVE_PRESET_PLACEBO, VE, "preset" },
+ { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_DEFAULT }, INT_MIN, INT_MAX, VE, "preset" },
+ { "fast", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_FAST }, INT_MIN, INT_MAX, VE, "preset" },
+ { "medium", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_MEDIUM }, INT_MIN, INT_MAX, VE, "preset" },
+ { "slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_SLOW }, INT_MIN, INT_MAX, VE, "preset" },
+ { "placebo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_PLACEBO }, INT_MIN, INT_MAX, VE, "preset" },
+ { "tune", "Tuning parameter for special purpose operation", OFFSET(tune_id), AV_OPT_TYPE_INT, { .i64 = XEVE_TUNE_NONE }, XEVE_TUNE_NONE, XEVE_TUNE_PSNR, VE, "tune"},
+ { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_NONE }, INT_MIN, INT_MAX, VE, "tune" },
+ { "zerolatency", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_ZEROLATENCY }, INT_MIN, INT_MAX, VE, "tune" },
+ { "psnr", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_PSNR }, INT_MIN, INT_MAX, VE, "tune" },
+ { "profile", "Encoding profile", OFFSET(profile_id), AV_OPT_TYPE_INT, { .i64 = XEVE_PROFILE_BASELINE }, XEVE_PROFILE_BASELINE, XEVE_PROFILE_MAIN, VE, "profile" },
+ { "baseline", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PROFILE_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
+ { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PROFILE_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
+ { "rc_mode", "Rate control mode", OFFSET(rc_mode), AV_OPT_TYPE_INT, { .i64 = XEVE_RC_CQP }, XEVE_RC_CQP, XEVE_RC_CRF, VE, "rc_mode" },
+ { "CQP", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_CQP }, INT_MIN, INT_MAX, VE, "rc_mode" },
+ { "ABR", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_ABR }, INT_MIN, INT_MAX, VE, "rc_mode" },
+ { "CRF", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_CRF }, INT_MIN, INT_MAX, VE, "rc_mode" },
+ { "qp", "Quantization parameter value for CQP rate control mode", OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 51, VE },
+ { "crf", "Constant rate factor value for CRF rate control mode", OFFSET(crf), AV_OPT_TYPE_INT, { .i64 = 32 }, 10, 49, VE },
+ { "hash", "Embed picture signature (HASH) for conformance checking in decoding", OFFSET(hash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
+ { "sei_info", "Embed SEI messages identifying encoder parameters and command line arguments", OFFSET(sei_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
+ { "xeve-params", "Override the xeve configuration using a :-separated list of key=value parameters", OFFSET(xeve_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
+ { NULL }
+};
+
+static const AVClass libxeve_class = {
+ .class_name = "libxeve",
+ .item_name = av_default_item_name,
+ .option = libxeve_options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
+/**
+ * libavcodec generic global options, which can be set on all the encoders and decoders
+ * @see https://www.ffmpeg.org/ffmpeg-codecs.html#Codec-Options
+ */
+static const FFCodecDefault libxeve_defaults[] = {
+ { "b", "0" }, // bitrate in terms of kilo-bits per second
+ { "g", "0" }, // gop_size (key-frame interval 0: only one I-frame at the first time; 1: every frame is coded in I-frame)
+ { "bf", "15"}, // maximum number of B frames (0: no B-frames, 1,3,7,15)
+ { "threads", "0"}, // number of threads to be used (0: automatically select the number of threads to set)
+ { NULL },
+};
+
+const FFCodec ff_libxeve_encoder = {
+ .p.name = "libxeve",
+ .p.long_name = NULL_IF_CONFIG_SMALL("libxeve MPEG-5 EVC"),
+ .p.type = AVMEDIA_TYPE_VIDEO,
+ .p.id = AV_CODEC_ID_EVC,
+ .init = libxeve_init,
+ FF_CODEC_ENCODE_CB(libxeve_encode),
+ .close = libxeve_close,
+ .priv_data_size = sizeof(XeveContext),
+ .p.priv_class = &libxeve_class,
+ .defaults = libxeve_defaults,
+ .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_DR1,
+ .p.profiles = NULL_IF_CONFIG_SMALL(ff_evc_profiles),
+ .p.wrapper_name = "libxeve",
+ .p.pix_fmts = supported_pixel_formats,
+ .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_NOT_INIT_THREADSAFE,
+};
--
2.17.1
1
0
[PATCH v13 4/9] avformat/evc_demuxer: Added demuxer to handle reading EVC video files
by Dawid Kozinski 07 Oct '22
by Dawid Kozinski 07 Oct '22
07 Oct '22
- Provided AVInputFormat structure describing EVC input format (ff_evc_demuxer)
Signed-off-by: Dawid Kozinski <d.kozinski(a)samsung.com>
---
libavformat/Makefile | 1 +
libavformat/allformats.c | 1 +
libavformat/evcdec.c | 124 +++++++++++++++++++++++++++++++++++++++
3 files changed, 126 insertions(+)
create mode 100644 libavformat/evcdec.c
diff --git a/libavformat/Makefile b/libavformat/Makefile
index a14a759c1f..af175d2097 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -251,6 +251,7 @@ OBJS-$(CONFIG_HCOM_DEMUXER) += hcom.o pcm.o
OBJS-$(CONFIG_HDS_MUXER) += hdsenc.o
OBJS-$(CONFIG_HEVC_DEMUXER) += hevcdec.o rawdec.o
OBJS-$(CONFIG_HEVC_MUXER) += rawenc.o
+OBJS-$(CONFIG_EVC_DEMUXER) += evcdec.o rawdec.o
OBJS-$(CONFIG_EVC_MUXER) += rawenc.o
OBJS-$(CONFIG_HLS_DEMUXER) += hls.o hls_sample_encryption.o
OBJS-$(CONFIG_HLS_MUXER) += hlsenc.o hlsplaylist.o avc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 615d2bc3b1..e4cc112b90 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -150,6 +150,7 @@ extern const AVInputFormat ff_ea_cdata_demuxer;
extern const AVInputFormat ff_eac3_demuxer;
extern const AVOutputFormat ff_eac3_muxer;
extern const AVInputFormat ff_epaf_demuxer;
+extern const AVInputFormat ff_evc_demuxer;
extern const AVOutputFormat ff_evc_muxer;
extern const AVOutputFormat ff_f4v_muxer;
extern const AVInputFormat ff_ffmetadata_demuxer;
diff --git a/libavformat/evcdec.c b/libavformat/evcdec.c
new file mode 100644
index 0000000000..ffb7449c4d
--- /dev/null
+++ b/libavformat/evcdec.c
@@ -0,0 +1,124 @@
+/*
+ * RAW EVC video demuxer
+ *
+ * Copyright (c) 2021 Dawid Kozinski <d.kozinski(a)samsung.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ */
+
+#include "libavcodec/get_bits.h"
+#include "libavcodec/golomb.h"
+#include "libavcodec/internal.h"
+#include "libavcodec/evc.h"
+
+#include "rawdec.h"
+#include "avformat.h"
+
+typedef struct EVCParserContext {
+ int got_sps;
+ int got_pps;
+ int got_idr;
+ int got_nonidr;
+} EVCParserContext;
+
+static int get_nalu_type(const uint8_t *bits, int bits_size)
+{
+ int unit_type_plus1 = 0;
+
+ if(bits_size >= EVC_NAL_HEADER_SIZE) {
+ unsigned char *p = (unsigned char *)bits;
+ // forbidden_zero_bit
+ if ((p[0] & 0x80) != 0) { // Cannot get bitstream information. Malformed bitstream.
+ return -1;
+ }
+
+ // nal_unit_type
+ unit_type_plus1 = (p[0] >> 1) & 0x3F;
+ }
+
+ return unit_type_plus1 - 1;
+}
+
+static uint32_t read_nal_unit_length(const uint8_t *bits, int bits_size)
+{
+ uint32_t nalu_len = 0;
+
+ if(bits_size >= EVC_NAL_UNIT_LENGTH_BYTE) {
+
+ int t = 0;
+ unsigned char *p = (unsigned char *)bits;
+
+ for(int i=0; i<EVC_NAL_UNIT_LENGTH_BYTE; i++) {
+ t = (t << 8) | p[i];
+ }
+
+ nalu_len = t;
+ if(nalu_len == 0) { // Invalid bitstream size
+ return 0;
+ }
+ }
+
+ return nalu_len;
+}
+
+static int parse_nal_units(const AVProbeData *p, EVCParserContext *ev)
+{
+ int nalu_type;
+ size_t nalu_size;
+ unsigned char *bits = (unsigned char *)p->buf;
+ int bytes_to_read = p->buf_size;
+
+ while(bytes_to_read > EVC_NAL_UNIT_LENGTH_BYTE) {
+
+ nalu_size = read_nal_unit_length(bits, EVC_NAL_UNIT_LENGTH_BYTE);
+ if(nalu_size == 0) break;
+
+ bits += EVC_NAL_UNIT_LENGTH_BYTE;
+ bytes_to_read -= EVC_NAL_UNIT_LENGTH_BYTE;
+
+ if(bytes_to_read < nalu_size) break;
+
+ nalu_type = get_nalu_type(bits, bytes_to_read);
+
+ bits += nalu_size;
+ bytes_to_read -= nalu_size;
+
+ if (nalu_type == EVC_SPS_NUT)
+ ev->got_sps++;
+ else if (nalu_type == EVC_PPS_NUT)
+ ev->got_pps++;
+ else if (nalu_type == EVC_IDR_NUT )
+ ev->got_idr++;
+ else if (nalu_type == EVC_NOIDR_NUT)
+ ev->got_nonidr++;
+ }
+
+ return 0;
+}
+
+static int evc_probe(const AVProbeData *p)
+{
+ EVCParserContext ev = {0};
+ int ret = parse_nal_units(p, &ev);
+
+ if (ret == 0 && ev.got_sps && ev.got_pps && (ev.got_idr || ev.got_nonidr > 3))
+ return AVPROBE_SCORE_EXTENSION + 1; // 1 more than .mpg
+
+ return 0;
+}
+
+FF_DEF_RAWVIDEO_DEMUXER(evc, "raw EVC video", evc_probe, "evc", AV_CODEC_ID_EVC)
--
2.17.1
1
0
[PATCH v13 3/9] avformat/evc_muxer: Added muxer to handle writing EVC encoded data into file or output bytestream
by Dawid Kozinski 07 Oct '22
by Dawid Kozinski 07 Oct '22
07 Oct '22
- Provided AVOutputFormat structure describing EVC output format (ff_evc_muxer)
- Added documentation for EVC muxer
Signed-off-by: Dawid Kozinski <d.kozinski(a)samsung.com>
---
doc/muxers.texi | 6 ++++++
libavformat/Makefile | 1 +
libavformat/allformats.c | 1 +
libavformat/rawenc.c | 13 +++++++++++++
4 files changed, 21 insertions(+)
diff --git a/doc/muxers.texi b/doc/muxers.texi
index b2f4326aae..08ab20c09e 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -2124,6 +2124,12 @@ DTS Coherent Acoustics (DCA) audio.
Dolby Digital Plus, also known as Enhanced AC-3, audio.
+@subsection evc
+
+MPEG-5 Essential Video Coding (EVC) / EVC / MPEG-5 Part 1 EVC video.
+
+Extensions: evc
+
@subsection g722
ITU-T G.722 audio.
diff --git a/libavformat/Makefile b/libavformat/Makefile
index d7f198bf39..a14a759c1f 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -251,6 +251,7 @@ OBJS-$(CONFIG_HCOM_DEMUXER) += hcom.o pcm.o
OBJS-$(CONFIG_HDS_MUXER) += hdsenc.o
OBJS-$(CONFIG_HEVC_DEMUXER) += hevcdec.o rawdec.o
OBJS-$(CONFIG_HEVC_MUXER) += rawenc.o
+OBJS-$(CONFIG_EVC_MUXER) += rawenc.o
OBJS-$(CONFIG_HLS_DEMUXER) += hls.o hls_sample_encryption.o
OBJS-$(CONFIG_HLS_MUXER) += hlsenc.o hlsplaylist.o avc.o
OBJS-$(CONFIG_HNM_DEMUXER) += hnm.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 47c419a009..615d2bc3b1 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -150,6 +150,7 @@ extern const AVInputFormat ff_ea_cdata_demuxer;
extern const AVInputFormat ff_eac3_demuxer;
extern const AVOutputFormat ff_eac3_muxer;
extern const AVInputFormat ff_epaf_demuxer;
+extern const AVOutputFormat ff_evc_muxer;
extern const AVOutputFormat ff_f4v_muxer;
extern const AVInputFormat ff_ffmetadata_demuxer;
extern const AVOutputFormat ff_ffmetadata_muxer;
diff --git a/libavformat/rawenc.c b/libavformat/rawenc.c
index 267fce252d..b7b2aff453 100644
--- a/libavformat/rawenc.c
+++ b/libavformat/rawenc.c
@@ -401,6 +401,19 @@ const AVOutputFormat ff_hevc_muxer = {
};
#endif
+#if CONFIG_EVC_MUXER
+AVOutputFormat ff_evc_muxer = {
+ .name = "evc",
+ .long_name = NULL_IF_CONFIG_SMALL("raw EVC video"),
+ .extensions = "evc",
+ .audio_codec = AV_CODEC_ID_NONE,
+ .video_codec = AV_CODEC_ID_EVC,
+ .write_header = force_one_stream,
+ .write_packet = ff_raw_write_packet,
+ .flags = AVFMT_NOTIMESTAMPS,
+};
+#endif
+
#if CONFIG_M4V_MUXER
const AVOutputFormat ff_m4v_muxer = {
.name = "m4v",
--
2.17.1
1
0
Added prerequisites that must be met before providing support for the MPEG-5 EVC codec
- Added new entry to codec IDs list
- Added new entry to the codec descriptor list
- Bumped libavcodec minor version
- Added profiles for EVC codec
Signed-off-by: Dawid Kozinski <d.kozinski(a)samsung.com>
---
libavcodec/avcodec.h | 3 +++
libavcodec/codec_desc.c | 8 ++++++++
libavcodec/codec_id.h | 1 +
libavcodec/profiles.c | 6 ++++++
libavcodec/profiles.h | 1 +
libavcodec/version.h | 2 +-
6 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7365eb5cc0..43f3732b58 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1668,6 +1668,9 @@ typedef struct AVCodecContext {
#define FF_PROFILE_KLVA_SYNC 0
#define FF_PROFILE_KLVA_ASYNC 1
+#define FF_PROFILE_EVC_BASELINE 0
+#define FF_PROFILE_EVC_MAIN 1
+
/**
* level
* - encoding: Set by user.
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 93b18f9072..6f745e5499 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1916,6 +1916,14 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("Media 100i"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
},
+ {
+ .id = AV_CODEC_ID_EVC,
+ .type = AVMEDIA_TYPE_VIDEO,
+ .name = "evc",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-5 EVC (Essential Video Coding)"),
+ .props = AV_CODEC_PROP_LOSSY | AV_CODEC_PROP_REORDER,
+ .profiles = NULL_IF_CONFIG_SMALL(ff_evc_profiles),
+ },
/* various PCM "codecs" */
{
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index 82874daaa3..84cb9edc0b 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -319,6 +319,7 @@ enum AVCodecID {
AV_CODEC_ID_RADIANCE_HDR,
AV_CODEC_ID_WBMP,
AV_CODEC_ID_MEDIA100,
+ AV_CODEC_ID_EVC,
/* various PCM "codecs" */
AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/profiles.c b/libavcodec/profiles.c
index 7af7fbeb13..a31244e0db 100644
--- a/libavcodec/profiles.c
+++ b/libavcodec/profiles.c
@@ -181,4 +181,10 @@ const AVProfile ff_arib_caption_profiles[] = {
{ FF_PROFILE_UNKNOWN }
};
+const AVProfile ff_evc_profiles[] = {
+ { FF_PROFILE_EVC_BASELINE, "Baseline" },
+ { FF_PROFILE_EVC_MAIN, "Main" },
+ { FF_PROFILE_UNKNOWN },
+};
+
#endif /* !CONFIG_SMALL */
diff --git a/libavcodec/profiles.h b/libavcodec/profiles.h
index 41a19aa9ad..cf92b5f126 100644
--- a/libavcodec/profiles.h
+++ b/libavcodec/profiles.h
@@ -72,5 +72,6 @@ extern const AVProfile ff_sbc_profiles[];
extern const AVProfile ff_prores_profiles[];
extern const AVProfile ff_mjpeg_profiles[];
extern const AVProfile ff_arib_caption_profiles[];
+extern const AVProfile ff_evc_profiles[];
#endif /* AVCODEC_PROFILES_H */
diff --git a/libavcodec/version.h b/libavcodec/version.h
index ade18cddcd..f8abc803b6 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
#include "version_major.h"
-#define LIBAVCODEC_VERSION_MINOR 50
+#define LIBAVCODEC_VERSION_MINOR 51
#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
--
2.17.1
1
0
[PATCH v2] lavc/cbs_av1: restore CodedBitstreamAV1Context when AVERROR(ENOSPC)
by Xiang, Haihao 07 Oct '22
by Xiang, Haihao 07 Oct '22
07 Oct '22
From: Haihao Xiang <haihao.xiang(a)intel.com>
The current pbc might be small for an obu frame, so a new pbc is
required then parse this obu frame again. Because
CodedBitstreamAV1Context has already been updated for this obu frame, we
need to restore CodedBitstreamAV1Context, otherwise
CodedBitstreamAV1Context doesn't match this obu frame when parsing obu
frame again, e.g. CodedBitstreamAV1Context.order_hint.
$ ffmpeg -i input.ivf -c:v copy -f null -
[...]
[av1_frame_merge @ 0x558bc3d6f880] ref_order_hint[i] does not match
inferred value: 20, but should be 22.
[av1_frame_merge @ 0x558bc3d6f880] Failed to write unit 1 (type 6).
[av1_frame_merge @ 0x558bc3d6f880] Failed to write packet.
[obu @ 0x558bc3d6e040] av1_frame_merge filter failed to send output
packet
---
libavcodec/cbs_av1.c | 64 ++++++++++++++++++++++++++++++++------------
1 file changed, 47 insertions(+), 17 deletions(-)
diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c
index 154d9156cf..45e1288a51 100644
--- a/libavcodec/cbs_av1.c
+++ b/libavcodec/cbs_av1.c
@@ -1058,15 +1058,31 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
AV1RawTileData *td;
size_t header_size;
int err, start_pos, end_pos, data_pos;
+ CodedBitstreamAV1Context av1ctx;
// OBUs in the normal bitstream format must contain a size field
// in every OBU (in annex B it is optional, but we don't support
// writing that).
obu->header.obu_has_size_field = 1;
+ av1ctx = *priv;
+
+ if (priv->sequence_header_ref) {
+ av1ctx.sequence_header_ref = av_buffer_ref(priv->sequence_header_ref);
+ if (!av1ctx.sequence_header_ref)
+ return AVERROR(ENOMEM);
+ }
+
+ if (priv->frame_header_ref) {
+ av1ctx.frame_header_ref = av_buffer_ref(priv->frame_header_ref);
+ if (!av1ctx.frame_header_ref) {
+ err = AVERROR(ENOMEM);
+ goto error;
+ }
+ }
err = cbs_av1_write_obu_header(ctx, pbc, &obu->header);
if (err < 0)
- return err;
+ goto error;
if (obu->header.obu_has_size_field) {
pbc_tmp = *pbc;
@@ -1084,18 +1100,21 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
err = cbs_av1_write_sequence_header_obu(ctx, pbc,
&obu->obu.sequence_header);
if (err < 0)
- return err;
+ goto error;
av_buffer_unref(&priv->sequence_header_ref);
priv->sequence_header = NULL;
err = ff_cbs_make_unit_refcounted(ctx, unit);
if (err < 0)
- return err;
+ goto error;
priv->sequence_header_ref = av_buffer_ref(unit->content_ref);
- if (!priv->sequence_header_ref)
- return AVERROR(ENOMEM);
+ if (!priv->sequence_header_ref) {
+ err = AVERROR(ENOMEM);
+ goto error;
+ }
+
priv->sequence_header = &obu->obu.sequence_header;
}
break;
@@ -1103,7 +1122,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
{
err = cbs_av1_write_temporal_delimiter_obu(ctx, pbc);
if (err < 0)
- return err;
+ goto error;
}
break;
case AV1_OBU_FRAME_HEADER:
@@ -1115,7 +1134,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
AV1_OBU_REDUNDANT_FRAME_HEADER,
NULL);
if (err < 0)
- return err;
+ goto error;
}
break;
case AV1_OBU_TILE_GROUP:
@@ -1123,7 +1142,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
err = cbs_av1_write_tile_group_obu(ctx, pbc,
&obu->obu.tile_group);
if (err < 0)
- return err;
+ goto error;
td = &obu->obu.tile_group.tile_data;
}
@@ -1132,7 +1151,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
{
err = cbs_av1_write_frame_obu(ctx, pbc, &obu->obu.frame, NULL);
if (err < 0)
- return err;
+ goto error;
td = &obu->obu.frame.tile_group.tile_data;
}
@@ -1141,7 +1160,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
{
err = cbs_av1_write_tile_list_obu(ctx, pbc, &obu->obu.tile_list);
if (err < 0)
- return err;
+ goto error;
td = &obu->obu.tile_list.tile_data;
}
@@ -1150,18 +1169,19 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
{
err = cbs_av1_write_metadata_obu(ctx, pbc, &obu->obu.metadata);
if (err < 0)
- return err;
+ goto error;
}
break;
case AV1_OBU_PADDING:
{
err = cbs_av1_write_padding_obu(ctx, pbc, &obu->obu.padding);
if (err < 0)
- return err;
+ goto error;
}
break;
default:
- return AVERROR(ENOSYS);
+ err = AVERROR(ENOSYS);
+ goto error;
}
end_pos = put_bits_count(pbc);
@@ -1172,7 +1192,7 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
// Add trailing bits and recalculate.
err = cbs_av1_write_trailing_bits(ctx, pbc, 8 - end_pos % 8);
if (err < 0)
- return err;
+ goto error;
end_pos = put_bits_count(pbc);
obu->obu_size = header_size = (end_pos - start_pos + 7) / 8;
} else {
@@ -1190,14 +1210,19 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
*pbc = pbc_tmp;
err = cbs_av1_write_leb128(ctx, pbc, "obu_size", obu->obu_size);
if (err < 0)
- return err;
+ goto error;
data_pos = put_bits_count(pbc) / 8;
flush_put_bits(pbc);
av_assert0(data_pos <= start_pos);
- if (8 * obu->obu_size > put_bits_left(pbc))
+ if (8 * obu->obu_size > put_bits_left(pbc)) {
+ av_buffer_unref(&priv->sequence_header_ref);
+ av_buffer_unref(&priv->frame_header_ref);
+ *priv = av1ctx;
+
return AVERROR(ENOSPC);
+ }
if (obu->obu_size > 0) {
memmove(pbc->buf + data_pos,
@@ -1213,8 +1238,13 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
// OBU data must be byte-aligned.
av_assert0(put_bits_count(pbc) % 8 == 0);
+ err = 0;
- return 0;
+error:
+ av_buffer_unref(&av1ctx.sequence_header_ref);
+ av_buffer_unref(&av1ctx.frame_header_ref);
+
+ return err;
}
static int cbs_av1_assemble_fragment(CodedBitstreamContext *ctx,
--
2.25.1
2
3
From: Haihao Xiang <haihao.xiang(a)intel.com>
HEVC spec has CRA frame which allows random access with open GOP, hence
it can achieve higher compression efficiency.
Signed-off-by: Haihao Xiang <haihao.xiang(a)intel.com>
---
libavcodec/qsvenc_hevc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libavcodec/qsvenc_hevc.c b/libavcodec/qsvenc_hevc.c
index a5bf915954..ba83987151 100644
--- a/libavcodec/qsvenc_hevc.c
+++ b/libavcodec/qsvenc_hevc.c
@@ -294,7 +294,7 @@ static const FFCodecDefault qsv_enc_defaults[] = {
{ "qmin", "-1" },
{ "qmax", "-1" },
{ "trellis", "-1" },
- { "flags", "+cgop" },
+ { "flags", "-cgop" },
{ NULL },
};
--
2.17.1
2
2
06 Oct '22
Although the DSP function only uses single precision from RISC-V F, the
caller may leave double precision values in the spilled registers if the
calling convention supports double precision hardware floats. Then, we
need to save and restore FS registers as double precision.
Conversely, we do not need to save anything at all if an integer calling
convention is in use. However we can assume that single precision floats
are supported, since the Zve32f extension implies the F extension.
So for the sake of simplicity, we always save at least single precision
values.
In theory, we should even save quadruple precision values if the LP64Q
ABI is in use. I have yet to see a compiler that supports it though.
---
libavcodec/riscv/aacpsdsp_rvv.S | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/libavcodec/riscv/aacpsdsp_rvv.S b/libavcodec/riscv/aacpsdsp_rvv.S
index 1d6e73fd2d..80bd19f6ad 100644
--- a/libavcodec/riscv/aacpsdsp_rvv.S
+++ b/libavcodec/riscv/aacpsdsp_rvv.S
@@ -55,9 +55,10 @@ endfunc
func ff_ps_hybrid_analysis_rvv, zve32f
/* We need 26 FP registers, for 20 scratch ones. Spill fs0-fs5. */
- addi sp, sp, -32
+ addi sp, sp, -48
.irp n, 0, 1, 2, 3, 4, 5
- fsw fs\n, (4 * \n)(sp)
+HWD fsd fs\n, (8 * \n)(sp)
+NOHWD fsw fs\n, (4 * \n)(sp)
.endr
.macro input, j, fd0, fd1, fd2, fd3
@@ -142,9 +143,10 @@ func ff_ps_hybrid_analysis_rvv, zve32f
bnez a4, 1b
.irp n, 5, 4, 3, 2, 1, 0
- flw fs\n, (4 * \n)(sp)
+HWD fld fs\n, (8 * \n)(sp)
+NOHWD flw fs\n, (4 * \n)(sp)
.endr
- addi sp, sp, 32
+ addi sp, sp, 48
ret
.purgem input
.purgem filter
--
2.37.2
1
0