[FFmpeg-cvslog] avcodec/hapqa_extract_bsf : add new bsf filter

Martin Vignali git at videolan.org
Tue Mar 13 21:27:14 EET 2018


ffmpeg | branch: master | Martin Vignali <martin.vignali at gmail.com> | Sun Mar 11 19:16:09 2018 +0100| [7b0cb2e032d38b914a98dedc82c5262c14eeccb7] | committer: Martin Vignali

avcodec/hapqa_extract_bsf : add new bsf filter

convert HapQA data to HAPQ or HAPAlphaOnly
by copying the corresponding texture

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=7b0cb2e032d38b914a98dedc82c5262c14eeccb7
---

 Changelog                      |   1 +
 doc/bitstream_filters.texi     |  25 ++++++++
 libavcodec/Makefile            |   1 +
 libavcodec/bitstream_filters.c |   1 +
 libavcodec/hapqa_extract_bsf.c | 135 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 163 insertions(+)

diff --git a/Changelog b/Changelog
index c1b9df46bc..7969b414c4 100644
--- a/Changelog
+++ b/Changelog
@@ -46,6 +46,7 @@ version <next>:
   They can be found at http://git.videolan.org/?p=ffmpeg/nv-codec-headers.git
 - native SBC encoder and decoder
 - drmeter audio filter
+- hapqa_extract bitstream filter
 
 
 version 3.4:
diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index b7ea549322..aac4705be4 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -93,6 +93,31 @@ When this option is enabled, the long-term headers are removed from the
 bitstream after extraction.
 @end table
 
+ at section hapqa_extract
+
+Extract Rgb or Alpha part of an HAPQA file, without recompression, in order to create an HAPQ or an HAPAlphaOnly file.
+
+ at table @option
+ at item texture
+Specifies the texture to keep.
+
+ at table @option
+ at item color
+ at item alpha
+ at end table
+
+ at end table
+
+Convert HAPQA to HAPQ
+ at example
+ffmpeg -i hapqa_inputfile.mov -c copy -bsf:v hapqa_extract=texture=color -tag:v HapY -metadata:s:v:0 encoder="HAPQ" hapq_file.mov
+ at end example
+
+Convert HAPQA to HAPAlphaOnly
+ at example
+ffmpeg -i hapqa_inputfile.mov -c copy -bsf:v hapqa_extract=texture=alpha -tag:v HapA -metadata:s:v:0 encoder="HAPAlpha Only" hapalphaonly_file.mov
+ at end example
+
 @section h264_metadata
 
 Modify metadata embedded in an H.264 stream.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index ff6c9f8b2c..0984455ec4 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1043,6 +1043,7 @@ OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF)      += extract_extradata_bsf.o    \
 OBJS-$(CONFIG_H264_METADATA_BSF)          += h264_metadata_bsf.o
 OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF)       += h264_mp4toannexb_bsf.o
 OBJS-$(CONFIG_H264_REDUNDANT_PPS_BSF)     += h264_redundant_pps_bsf.o
+OBJS-$(CONFIG_HAPQA_EXTRACT_BSF)          += hapqa_extract_bsf.o hap.o
 OBJS-$(CONFIG_HEVC_METADATA_BSF)          += h265_metadata_bsf.o
 OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF)       += hevc_mp4toannexb_bsf.o
 OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF)        += imx_dump_header_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 338ef8251b..68e652286d 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -32,6 +32,7 @@ extern const AVBitStreamFilter ff_extract_extradata_bsf;
 extern const AVBitStreamFilter ff_h264_metadata_bsf;
 extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_h264_redundant_pps_bsf;
+extern const AVBitStreamFilter ff_hapqa_extract_bsf;
 extern const AVBitStreamFilter ff_hevc_metadata_bsf;
 extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf;
 extern const AVBitStreamFilter ff_imx_dump_header_bsf;
diff --git a/libavcodec/hapqa_extract_bsf.c b/libavcodec/hapqa_extract_bsf.c
new file mode 100644
index 0000000000..45fe9fbe8f
--- /dev/null
+++ b/libavcodec/hapqa_extract_bsf.c
@@ -0,0 +1,135 @@
+/*
+ * HAPQA extract bitstream filter
+ * Copyright (c) 2017 Jokyo Images
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * HAPQA extract bitstream filter
+ * extract one of the two textures of the HAQA
+ */
+
+#include "avcodec.h"
+#include "bsf.h"
+#include "bytestream.h"
+#include "hap.h"
+
+typedef struct HapqaExtractContext {
+    const AVClass *class;
+    int texture;/* index of the texture to keep (0 for rgb or 1 for alpha) */
+} HapqaExtractContext;
+
+static int check_texture(HapqaExtractContext *ctx, int section_type) {
+    if (((ctx->texture == 0)&&((section_type & 0x0F) == 0x0F)) || /* HapQ texture and rgb extract */
+        ((ctx->texture == 1)&&((section_type & 0x0F) == 0x01))) /* HapAlphaOnly texture and alpha extract */
+    {
+        return 1; /* the texture is the one to keep */
+    } else {
+        return 0;
+    }
+}
+
+static int hapqa_extract(AVBSFContext *bsf, AVPacket *out)
+{
+    HapqaExtractContext *ctx = bsf->priv_data;
+    GetByteContext gbc;
+    int section_size;
+    enum HapSectionType section_type;
+    int start_section_size;
+    int target_packet_size = 0;
+    AVPacket *in;
+    int ret = 0;
+
+    ret = ff_bsf_get_packet(bsf, &in);
+    if (ret < 0)
+        return ret;
+
+    bytestream2_init(&gbc, in->data, in->size);
+    ret = ff_hap_parse_section_header(&gbc, &section_size, &section_type);
+    if (ret != 0)
+        goto fail;
+
+    if ((section_type & 0x0F) != 0x0D) {
+        av_log(bsf, AV_LOG_ERROR, "Invalid section type for HAPQA %#04x.\n", section_type & 0x0F);
+        goto fail;
+    }
+
+    start_section_size = 4;
+
+    bytestream2_seek(&gbc, start_section_size, SEEK_SET);/* go to start of the first texture */
+
+    ret = ff_hap_parse_section_header(&gbc, &section_size, &section_type);
+    if (ret != 0)
+        goto fail;
+
+    target_packet_size = section_size + 4;
+
+    if (check_texture(ctx, section_type) == 0) { /* the texture is not the one to keep */
+        start_section_size += 4 + section_size;
+        bytestream2_seek(&gbc, start_section_size, SEEK_SET);/* go to start of the second texture */
+        ret = ff_hap_parse_section_header(&gbc, &section_size, &section_type);
+        if (ret != 0)
+            goto fail;
+
+        target_packet_size = section_size + 4;
+
+        if (check_texture(ctx, section_type) == 0){ /* the second texture is not the one to keep */
+            av_log(bsf, AV_LOG_ERROR, "No valid texture found.\n");
+            goto fail;
+        }
+    }
+
+    av_packet_move_ref(out, in);
+    out->data += start_section_size;
+    out->size = target_packet_size;
+
+fail:
+    if (ret < 0)
+        av_packet_unref(out);
+    av_packet_free(&in);
+    return ret;
+}
+
+static const enum AVCodecID codec_ids[] = {
+    AV_CODEC_ID_HAP, AV_CODEC_ID_NONE,
+};
+
+#define OFFSET(x) offsetof(HapqaExtractContext, x)
+#define FLAGS     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "texture", "texture to keep", OFFSET(texture), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS,  "texture" },
+        { "color", "keep HapQ texture", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "texture" },
+        { "alpha", "keep HapAlphaOnly texture", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "texture" },
+    { NULL },
+};
+
+static const AVClass hapqa_extract_class = {
+    .class_name = "hapqa_extract_bsf",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_MAJOR,
+};
+
+const AVBitStreamFilter ff_hapqa_extract_bsf = {
+    .name       = "hapqa_extract",
+    .filter     = hapqa_extract,
+    .priv_data_size = sizeof(HapqaExtractContext),
+    .priv_class = &hapqa_extract_class,
+    .codec_ids  = codec_ids,
+};



More information about the ffmpeg-cvslog mailing list