Signed-off-by: Vittorio Giovara <[email protected]>
---
 Changelog               |   1 +
 configure               |   6 ++
 doc/general.texi        |   1 +
 libavcodec/Makefile     |   2 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/avcodec.h    |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/hap.h        |  68 +++++++++++++
 libavcodec/hapdec.c     | 253 ++++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/hapenc.c     | 196 +++++++++++++++++++++++++++++++++++++
 libavformat/isom.c      |   4 +
 tests/fate/video.mak    |  12 +++
 tests/ref/fate/hap1     |   2 +
 tests/ref/fate/hap5     |   2 +
 tests/ref/fate/hapy     |   2 +
 15 files changed, 558 insertions(+)
 create mode 100644 libavcodec/hap.h
 create mode 100644 libavcodec/hapdec.c
 create mode 100644 libavcodec/hapenc.c
 create mode 100644 tests/ref/fate/hap1
 create mode 100644 tests/ref/fate/hap5
 create mode 100644 tests/ref/fate/hapy

diff --git a/Changelog b/Changelog
index 9d73449..f7c59e9 100644
--- a/Changelog
+++ b/Changelog
@@ -30,6 +30,7 @@ version <next>:
 - Canopus HQ/HQA decoder
 - Automatically rotate videos based on metadata in avconv
 - improved Quickdraw compatibility
+- HAP decoder and encoder
 
 
 version 11:
diff --git a/configure b/configure
index 5bcc2df..80cda8e 100755
--- a/configure
+++ b/configure
@@ -220,6 +220,7 @@ External library support:
                            native MPEG-4/Xvid encoder exists [no]
   --enable-mmal            enable decoding via MMAL [no]
   --enable-openssl         enable openssl [no]
+  --enable-snappy          enable snappy decompression [no]
   --enable-x11grab         enable X11 grabbing (legacy) [no]
   --enable-zlib            enable zlib [autodetect]
 
@@ -1189,6 +1190,7 @@ EXTERNAL_LIBRARY_LIST="
     libxvid
     mmal
     openssl
+    snappy
     x11grab
     zlib
 "
@@ -1842,6 +1844,8 @@ h264_qsv_decoder_deps="libmfx"
 h264_qsv_decoder_select="h264_mp4toannexb_bsf h264_parser qsvdec 
h264_qsv_hwaccel"
 h264_qsv_encoder_deps="libmfx"
 h264_qsv_encoder_select="qsvenc"
+hap_decoder_deps="snappy"
+hap_encoder_deps="snappy"
 hevc_decoder_select="bswapdsp cabac golomb videodsp"
 huffyuv_decoder_select="bswapdsp huffyuvdsp"
 huffyuv_encoder_select="bswapdsp huffman huffyuvencdsp"
@@ -4283,6 +4287,8 @@ enabled openssl           && { check_lib openssl/ssl.h 
SSL_library_init -lssl -l
                                check_lib openssl/ssl.h SSL_library_init 
-lssl32 -leay32 ||
                                check_lib openssl/ssl.h SSL_library_init -lssl 
-lcrypto -lws2_32 -lgdi32 ||
                                die "ERROR: openssl not found"; }
+enabled snappy            && require snappy snappy-c.h snappy_uncompress 
-lsnappy
+
 
 if enabled gnutls; then
     { check_lib nettle/bignum.h nettle_mpz_get_str_256 -lnettle -lhogweed 
-lgmp && enable nettle; } ||
diff --git a/doc/general.texi b/doc/general.texi
index ab8673a..f812f4f 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -290,6 +290,7 @@ library:
 @item Matroska audio            @tab X @tab
 @item FFmpeg metadata           @tab X @tab X
     @tab Metadata in text format.
+@item VidVox HAP                @tab X @tab X
 @item MAXIS XA                  @tab   @tab X
     @tab Used in Sim City 3000; file extension .xa.
 @item MD Studio                 @tab   @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index fc8c6e9..783d28e 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -228,6 +228,8 @@ OBJS-$(CONFIG_H264_DECODER)            += h264.o 
h264_cabac.o h264_cavlc.o \
 OBJS-$(CONFIG_H264_MMAL_DECODER)       += mmaldec.o
 OBJS-$(CONFIG_H264_QSV_DECODER)        += qsvdec_h264.o
 OBJS-$(CONFIG_H264_QSV_ENCODER)        += qsvenc_h264.o
+OBJS-$(CONFIG_HAP_DECODER)             += hapdec.o dxtc_dec.o
+OBJS-$(CONFIG_HAP_ENCODER)             += hapenc.o dxtc_enc.o
 OBJS-$(CONFIG_HEVC_DECODER)            += hevc.o hevc_mvs.o hevc_ps.o 
hevc_sei.o \
                                           hevc_cabac.o hevc_refs.o hevcpred.o  
  \
                                           hevcdsp.o hevc_filter.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index d2679ad..77464cb 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -164,6 +164,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER(H264,              h264);
     REGISTER_DECODER(H264_MMAL,         h264_mmal);
     REGISTER_DECODER(H264_QSV,          h264_qsv);
+    REGISTER_ENCDEC (HAP,               hap);
     REGISTER_DECODER(HEVC,              hevc);
     REGISTER_DECODER(HNM4_VIDEO,        hnm4_video);
     REGISTER_DECODER(HQ_HQA,            hq_hqa);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 3440126..d3c533a 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -296,6 +296,7 @@ enum AVCodecID {
     AV_CODEC_ID_HQX,
     AV_CODEC_ID_TDSC,
     AV_CODEC_ID_HQ_HQA,
+    AV_CODEC_ID_HAP,
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the 
start of audio codecs
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 4f2a63a..8980a8b 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1148,6 +1148,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_HAP,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "hap",
+        .long_name = NULL_IF_CONFIG_SMALL("VidVox HAP decoder"),
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
+    },
 
     /* image codecs */
     {
diff --git a/libavcodec/hap.h b/libavcodec/hap.h
new file mode 100644
index 0000000..1b01c92
--- /dev/null
+++ b/libavcodec/hap.h
@@ -0,0 +1,68 @@
+/*
+ * VideoVox HAP
+ * Copyright (C) 2015 Vittorio Giovara <[email protected]>
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_HAP_H
+#define AVCODEC_HAP_H
+
+#include <stdint.h>
+
+#include "libavutil/opt.h"
+
+#include "bytestream.h"
+#include "dxtc.h"
+
+typedef struct HAPContext {
+    AVClass *class;
+
+    DXTCContext dxtc;
+    GetByteContext gbc;
+    PutByteContext pbc;
+
+    int section_type;        /* Header type */
+
+    int tex_rat;             /* Compression ratio */
+    const uint8_t *tex_data; /* Compressed texture */
+    uint8_t *tex_buf;        /* Uncompressed texture */
+    size_t tex_size;         /* Size of the compressed texture */
+
+    uint8_t *snappied;       /* Buffer interacting with snappy */
+    size_t max_snappy;       /* Maximum compressed size for snappy buffer */
+
+    /* Pointer to the selected compress or decompress function */
+    int (*tex_fun)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
+} HAPContext;
+
+enum {
+    FMT_RGBDXT1   = 0x0B,
+    FMT_RGBADXT5  = 0x0E,
+    FMT_YCOCGDXT5 = 0x0F,
+} HapFormat;
+
+enum {
+    COMP_NONE    = 0xA0,
+    COMP_SNAPPY  = 0xB0,
+    COMP_COMPLEX = 0xC0,
+} HapCompressor;
+
+/* RGBA is on 4 bytes */
+#define PIXEL_SIZE 4
+
+#endif /* AVCODEC_HAP_H */
diff --git a/libavcodec/hapdec.c b/libavcodec/hapdec.c
new file mode 100644
index 0000000..1d8e89e
--- /dev/null
+++ b/libavcodec/hapdec.c
@@ -0,0 +1,253 @@
+/*
+ * VidVox HAP decoder
+ * Copyright (C) 2015 Vittorio Giovara <[email protected]>
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * HAP decoder
+ *
+ * Fourcc: HAP1, HAP5, HAPY
+ *
+ * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
+ */
+
+#include <stdint.h>
+#include "snappy-c.h"
+
+#include "libavutil/imgutils.h"
+
+#include "avcodec.h"
+#include "bytestream.h"
+#include "dxtc.h"
+#include "hap.h"
+#include "internal.h"
+#include "thread.h"
+
+/* Texture blocks are 4x4 */
+#define BLOCK_W 4
+#define BLOCK_H 4
+
+/* The first three bytes are the size of the section past the header, or zero
+ * if the length is stored in the next long word. The fourth byte in the first
+ * long word indicates the type of the current section. */
+static int parse_section_header(AVCodecContext *avctx)
+{
+    HAPContext *ctx = avctx->priv_data;
+    GetByteContext *gbc = &ctx->gbc;
+    int length;
+
+    if (bytestream2_get_bytes_left(gbc) < 8)
+        return AVERROR_INVALIDDATA;
+
+    length = bytestream2_get_le24(gbc);
+
+    ctx->section_type = bytestream2_get_byte(gbc);
+
+    if (length == 0)
+        length = bytestream2_get_le32(gbc);
+
+    if (length > bytestream2_get_bytes_left(gbc) || length == 0)
+        return AVERROR_INVALIDDATA;
+
+    return length;
+}
+
+/* Prepare the texture to be decompressed */
+static int setup_texture(AVCodecContext *avctx, size_t length)
+{
+    HAPContext *ctx = avctx->priv_data;
+    GetByteContext *gbc = &ctx->gbc;
+    size_t snappy_size;
+    const char *texture_name;
+    const char *compressor_name;
+    int ret;
+
+    switch (ctx->section_type & 0x0F) {
+    case FMT_RGBDXT1:
+        ctx->tex_rat = 8;
+        ctx->tex_fun = ctx->dxtc.dxt1_block;
+        texture_name = "DXT1";
+        break;
+    case FMT_RGBADXT5:
+        ctx->tex_rat = 16;
+        ctx->tex_fun = ctx->dxtc.dxt5_block;
+        texture_name = "DXT5";
+        break;
+    case FMT_YCOCGDXT5:
+        ctx->tex_rat = 16;
+        ctx->tex_fun = ctx->dxtc.dxt5ys_block;
+        texture_name = "DXT5-YCoCg-scaled";
+        break;
+    default:
+        av_log(avctx, AV_LOG_ERROR,
+               "Invalid format mode %02X.\n", ctx->section_type);
+        return AVERROR_INVALIDDATA;
+    }
+
+    switch (ctx->section_type & 0xF0) {
+    case COMP_NONE:
+        /* Only texture compression */
+        ctx->tex_data = gbc->buffer;
+        ctx->tex_size = length;
+        compressor_name = "none";
+        break;
+    case COMP_SNAPPY:
+        /* Get the size of the output buffer */
+        ret = snappy_uncompressed_length(gbc->buffer, length, &snappy_size);
+        if (ret != SNAPPY_OK) {
+            av_log(avctx, AV_LOG_ERROR, "Snappy size error\n");
+            return AVERROR_BUG;
+        }
+
+        /* Resize as needed */
+        ret = av_reallocp(&ctx->snappied, snappy_size);
+        if (ret < 0)
+            return ret;
+
+        /* Uncompress */
+        ret = snappy_uncompress(gbc->buffer, length,
+                                ctx->snappied, &snappy_size);
+        if (ret != SNAPPY_OK) {
+            av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
+            return AVERROR_BUG;
+        }
+
+        /* Set the pointers */
+        ctx->tex_data = ctx->snappied;
+        ctx->tex_size = snappy_size;
+        compressor_name = "snappy";
+        break;
+    case COMP_COMPLEX:
+        compressor_name = "complex";
+        avpriv_request_sample(avctx, "Complex HAP compressor");
+        return AVERROR_PATCHWELCOME;
+        break;
+    default:
+        av_log(avctx, AV_LOG_ERROR,
+               "Invalid compressor mode %02X.\n", ctx->section_type);
+        return AVERROR_INVALIDDATA;
+    }
+
+    av_log(avctx, AV_LOG_DEBUG, "%s texture with %s compressor\n",
+           texture_name, compressor_name);
+
+    return 0;
+}
+
+static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
+                                     int block_nb, int thread_nb)
+{
+    HAPContext *ctx = avctx->priv_data;
+    AVFrame *frame = arg;
+    int x = (BLOCK_W * block_nb) % avctx->coded_width;
+    int y = BLOCK_H * (BLOCK_W * block_nb / avctx->coded_width);
+    uint8_t *p = frame->data[0] + x * PIXEL_SIZE + y * frame->linesize[0];
+    const uint8_t *d = ctx->tex_data + block_nb * ctx->tex_rat;
+
+    ctx->tex_fun(p, frame->linesize[0], d);
+    return 0;
+}
+
+static int hap_decode(AVCodecContext *avctx, void *data,
+                      int *got_frame, AVPacket *avpkt)
+{
+    HAPContext *ctx = avctx->priv_data;
+    ThreadFrame tframe;
+    int ret, length;
+    int blocks = avctx->coded_width * avctx->coded_height / (BLOCK_W * 
BLOCK_H);
+
+    bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
+
+    /* Check for section header */
+    length = parse_section_header(avctx);
+    if (length < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
+        return length;
+    }
+
+    /* Prepare the texture buffer and decompress function */
+    ret = setup_texture(avctx, length);
+    if (ret < 0)
+        return ret;
+
+    /* Get the output frame ready to receive data */
+    tframe.f = data;
+    ret = ff_thread_get_buffer(avctx, &tframe, 0);
+    if (ret < 0)
+        return ret;
+    ff_thread_finish_setup(avctx);
+
+    /* Use the decompress function on the texture, one block per thread */
+    avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, blocks);
+
+    /* Frame is ready to be output */
+    tframe.f->pict_type = AV_PICTURE_TYPE_I;
+    tframe.f->key_frame = 1;
+    *got_frame = 1;
+
+    return avpkt->size;
+}
+
+static av_cold int hap_init(AVCodecContext *avctx)
+{
+    HAPContext *ctx = avctx->priv_data;
+    int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
+
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
+               avctx->width, avctx->height);
+        return ret;
+    }
+
+    /* Since codec is based on 4x4 blocks, size is aligned to 4 */
+    avctx->coded_width  = FFALIGN(avctx->width,  BLOCK_W);
+    avctx->coded_height = FFALIGN(avctx->height, BLOCK_H);
+
+    /* Technically only one mode has alpha, but 32 bits are easier to handle */
+    avctx->pix_fmt = AV_PIX_FMT_RGBA;
+
+    ff_dxtc_decompression_init(&ctx->dxtc);
+
+    return 0;
+}
+
+static av_cold int hap_close(AVCodecContext *avctx)
+{
+    HAPContext *ctx = avctx->priv_data;
+
+    av_freep(&ctx->snappied);
+
+    return 0;
+}
+
+AVCodec ff_hap_decoder = {
+    .name           = "hap",
+    .long_name      = NULL_IF_CONFIG_SMALL("VidVox HAP decoder"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_HAP,
+    .init           = hap_init,
+    .decode         = hap_decode,
+    .close          = hap_close,
+    .priv_data_size = sizeof(HAPContext),
+    .capabilities   = CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS |
+                      CODEC_CAP_DR1,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
+                      FF_CODEC_CAP_INIT_CLEANUP,
+};
diff --git a/libavcodec/hapenc.c b/libavcodec/hapenc.c
new file mode 100644
index 0000000..2cd39d8
--- /dev/null
+++ b/libavcodec/hapenc.c
@@ -0,0 +1,196 @@
+/*
+ * VidVox Hap encoder
+ * Copyright (C) 2015 Vittorio Giovara <[email protected]>
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * HAP encoder
+ *
+ * Fourcc: HAP1, HAP5, HAPY
+ *
+ * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
+ */
+
+#include <stdint.h>
+#include "snappy-c.h"
+
+#include "libavutil/frame.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/opt.h"
+
+#include "avcodec.h"
+#include "bytestream.h"
+#include "hap.h"
+#include "internal.h"
+
+/* A fixed header size allows to skip a memcpy */
+#define HEADER_SIZE 8
+
+static void compress_texture(AVCodecContext *avctx, const AVFrame *f)
+{
+    HAPContext *ctx = avctx->priv_data;
+    uint8_t *out = ctx->tex_buf;
+    int i, j;
+
+    for (j = 0; j < avctx->height; j += 4) {
+        for (i = 0; i < avctx->width; i += 4) {
+            uint8_t *p = f->data[0] + i * PIXEL_SIZE + j * f->linesize[0];
+            const int step = ctx->tex_fun(out, f->linesize[0], p);
+            out += step;
+        }
+    }
+}
+
+static int hap_encode(AVCodecContext *avctx, AVPacket *pkt,
+                      const AVFrame *frame, int *got_packet)
+{
+    HAPContext *ctx = avctx->priv_data;
+    size_t final_size = ctx->max_snappy;
+    int ret, comp = COMP_SNAPPY;
+    int pktsize = FFMAX(ctx->tex_size, ctx->max_snappy) + HEADER_SIZE;
+
+    /* Allocate maximum size packet, shrink later */
+    ret = ff_alloc_packet(pkt, pktsize);
+    if (ret < 0)
+        return ret;
+
+    /* DXTC compression */
+    compress_texture(avctx, frame);
+
+    /* Compress with snappy too, write directly on packet buffer */
+    ret = snappy_compress(ctx->tex_buf, ctx->tex_size,
+                          pkt->data + HEADER_SIZE, &final_size);
+    if (ret != SNAPPY_OK) {
+        av_log(avctx, AV_LOG_ERROR, "Snappy compress error.\n");
+        return AVERROR_BUG;
+    }
+
+    /* If there is no gain from snappy, just use the raw texture */
+    if (final_size > ctx->tex_size) {
+        comp = COMP_NONE;
+        av_log(avctx, AV_LOG_WARNING,
+               "Snappy buffer bigger than uncompressed (%lu > %lu bytes).\n",
+               final_size, ctx->tex_size);
+        memcpy(pkt->data + HEADER_SIZE, ctx->tex_buf, ctx->tex_size);
+        final_size = ctx->tex_size;
+    }
+
+    /* Write header at the start */
+    AV_WL24(pkt->data, 0);
+    AV_WL32(pkt->data + 4, final_size);
+    pkt->data[3] = comp | ctx->section_type;
+
+    av_shrink_packet(pkt, final_size + HEADER_SIZE);
+    pkt->flags |= AV_PKT_FLAG_KEY;
+    *got_packet = 1;
+    return 0;
+}
+
+static av_cold int hap_init(AVCodecContext *avctx)
+{
+    HAPContext *ctx = avctx->priv_data;
+    int ratio;
+    int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
+
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
+               avctx->width, avctx->height);
+        return ret;
+    }
+
+    ff_dxtc_compression_init(&ctx->dxtc);
+
+    switch (ctx->section_type & 0x0F) {
+    case FMT_RGBDXT1:
+        ratio = 8;
+        avctx->codec_tag = MKTAG('H', 'a', 'p', '1');
+        ctx->tex_fun = ctx->dxtc.dxt1_block;
+        break;
+    case FMT_RGBADXT5:
+        ratio = 4;
+        avctx->codec_tag = MKTAG('H', 'a', 'p', '5');
+        ctx->tex_fun = ctx->dxtc.dxt5_block;
+        break;
+    case FMT_YCOCGDXT5:
+        ratio = 4;
+        avctx->codec_tag = MKTAG('H', 'a', 'p', 'Y');
+        ctx->tex_fun = ctx->dxtc.dxt5ys_block;
+        break;
+    default:
+        av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", 
ctx->section_type);
+        return AVERROR_INVALIDDATA;
+    }
+
+    /* Texture compression ratio is constant, so can we computer
+     * beforehand the final size of the uncompressed buffer */
+    ctx->tex_size   = avctx->width * avctx->height * PIXEL_SIZE / ratio;
+    ctx->max_snappy = snappy_max_compressed_length(ctx->tex_size);
+
+    ctx->tex_buf  = av_malloc(ctx->tex_size);
+    if (!ctx->tex_buf)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static av_cold int hap_close(AVCodecContext *avctx)
+{
+    HAPContext *ctx = avctx->priv_data;
+
+    av_freep(&ctx->tex_buf);
+
+    return 0;
+}
+
+#define OFFSET(x) offsetof(HAPContext, x)
+#define FLAGS     AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "profile", NULL, OFFSET(section_type), AV_OPT_TYPE_INT, { .i64 = 
FMT_RGBDXT1 }, FMT_RGBDXT1, FMT_YCOCGDXT5, FLAGS, "profile" },
+        { "hap",       NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FMT_RGBDXT1   }, 
0, 0, FLAGS, "profile" },
+        { "hap_alpha", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FMT_RGBADXT5  }, 
0, 0, FLAGS, "profile" },
+        { "hap_q",     NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FMT_YCOCGDXT5 }, 
0, 0, FLAGS, "profile" },
+
+    { NULL },
+};
+
+static const AVClass hapenc_class = {
+    .class_name = "Hap encoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_hap_encoder = {
+    .name           = "hap",
+    .long_name      = NULL_IF_CONFIG_SMALL("VidVox HAP encoder"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_HAP,
+    .priv_data_size = sizeof(HAPContext),
+    .priv_class     = &hapenc_class,
+    .init           = hap_init,
+    .encode2        = hap_encode,
+    .close          = hap_close,
+    .pix_fmts       = (const enum AVPixelFormat[]) {
+        AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE
+    },
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
+                      FF_CODEC_CAP_INIT_CLEANUP,
+};
diff --git a/libavformat/isom.c b/libavformat/isom.c
index f8cc4b2..2825423 100644
--- a/libavformat/isom.c
+++ b/libavformat/isom.c
@@ -250,6 +250,10 @@ const AVCodecTag ff_codec_movvideo_tags[] = {
 
     { AV_CODEC_ID_AIC, MKTAG('i', 'c', 'o', 'd') },
 
+    { AV_CODEC_ID_HAP, MKTAG('H', 'A', 'P', '1') },
+    { AV_CODEC_ID_HAP, MKTAG('H', 'A', 'P', '5') },
+    { AV_CODEC_ID_HAP, MKTAG('H', 'A', 'P', 'Y') },
+
     { AV_CODEC_ID_NONE, 0 },
 };
 
diff --git a/tests/fate/video.mak b/tests/fate/video.mak
index d43754f..8480fd0 100644
--- a/tests/fate/video.mak
+++ b/tests/fate/video.mak
@@ -160,6 +160,18 @@ fate-id-cin-video: CMD = framecrc -i 
$(TARGET_SAMPLES)/idcin/idlog-2MB.cin -pix_
 FATE_SAMPLES_AVCONV-$(call ENCDEC, ROQ PGMYUV, ROQ IMAGE2) += 
fate-idroq-video-encode
 fate-idroq-video-encode: CMD = md5 -f image2 -vcodec pgmyuv -i 
$(TARGET_SAMPLES)/ffmpeg-synthetic/vsynth1/%02d.pgm -sws_flags +bitexact -vf 
pad=512:512:80:112 -f roq -t 0.2
 
+FATE_HAP += fate-hap1
+fate-hap1: CMD = framecrc -i $(TARGET_SAMPLES)/hap/hap1.mov
+
+FATE_HAP += fate-hap5
+fate-hap5: CMD = framecrc -i $(TARGET_SAMPLES)/hap/hap5.mov
+
+FATE_HAP += fate-hapy
+fate-hapy: CMD = framecrc -i $(TARGET_SAMPLES)/hap/hapy.mov
+
+FATE_SAMPLES_AVCONV-$(call DEMDEC, MOV, HAP) += $(FATE_HAP)
+fate-hap: $(FATE_HAP)
+
 FATE_IFF-$(CONFIG_IFF_BYTERUN1_DECODER) += fate-iff-byterun1
 fate-iff-byterun1: CMD = framecrc -i $(TARGET_SAMPLES)/iff/ASH.LBM -pix_fmt 
rgb24
 
diff --git a/tests/ref/fate/hap1 b/tests/ref/fate/hap1
new file mode 100644
index 0000000..0cabf8b
--- /dev/null
+++ b/tests/ref/fate/hap1
@@ -0,0 +1,2 @@
+#tb 0: 1/2997
+0,          0,          0,        0,  1228800, 0x0fa946bc
diff --git a/tests/ref/fate/hap5 b/tests/ref/fate/hap5
new file mode 100644
index 0000000..a582c39
--- /dev/null
+++ b/tests/ref/fate/hap5
@@ -0,0 +1,2 @@
+#tb 0: 1/3000
+0,          0,          0,        0,  1228800, 0xb71ecf93
diff --git a/tests/ref/fate/hapy b/tests/ref/fate/hapy
new file mode 100644
index 0000000..e6dd4f5
--- /dev/null
+++ b/tests/ref/fate/hapy
@@ -0,0 +1,2 @@
+#tb 0: 1/2997
+0,          0,          0,        0,  1228800, 0x5993522d
-- 
1.9.5 (Apple Git-50.3)

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to