From bb9848935165d60765cb4de632d54f30eb46078c Mon Sep 17 00:00:00 2001
From: Martin Vignali <martin.vignali@gmail.com>
Date: Mon, 13 Nov 2017 22:21:40 +0100
Subject: [PATCH 3/3] avcodec/hapqa_extract_bsf : add new bsf filter

convert HapQA data to HAPQ or HAPAlphaOnly
by copying the corresponding texture
---
 libavcodec/hapqa_extract_bsf.c | 127 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 127 insertions(+)
 create mode 100644 libavcodec/hapqa_extract_bsf.c

diff --git a/libavcodec/hapqa_extract_bsf.c b/libavcodec/hapqa_extract_bsf.c
new file mode 100644
index 0000000000..ccf159e33d
--- /dev/null
+++ b/libavcodec/hapqa_extract_bsf.c
@@ -0,0 +1,127 @@
+/*
+ * 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 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;
+    uint8_t *out_buf;
+
+    av_log(bsf, AV_LOG_DEBUG, "Texture to keep : %d.\n", ctx->texture);
+
+    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);
+        goto fail;
+    }
+
+    start_section_size = 4;
+
+    bytestream2_seek(&gbc, start_section_size, SEEK_SET);//On va au debut de la texture 0
+
+    ret = ff_hap_parse_section_header(&gbc, &section_size, &section_type);
+    if (ret != 0)
+        goto fail;
+
+    target_packet_size = section_size + 4;
+
+    if (ctx->texture == 1) {
+        start_section_size += 4 + section_size;
+        bytestream2_seek(&gbc, start_section_size, SEEK_SET);//On va au debut de la texture 1
+        ret = ff_hap_parse_section_header(&gbc, &section_size, &section_type);
+        if (ret != 0)
+            goto fail;
+
+        target_packet_size = section_size + 4;
+    }
+
+    ret = av_new_packet(out, target_packet_size);
+    if (ret < 0)
+        goto fail;
+
+    out_buf = out->data;
+    bytestream_put_buffer(&out_buf, in->data + start_section_size, target_packet_size);
+
+    ret = av_packet_copy_props(out, in);
+    if (ret < 0)
+        goto fail;
+
+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)
+static const AVOption options[] = {
+    { "texture", "texture to keep", OFFSET(texture), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, },
+    { 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,
+};
-- 
2.11.0 (Apple Git-81)

