On Fri, Jul 17, 2015 at 1:11 PM, Tom Butterworth <[email protected]> wrote:
> ---
>  libavcodec/Makefile      |   4 +-
>  libavcodec/hap.c         |  51 +++++++++
>  libavcodec/hap.h         |  68 ++++++++----
>  libavcodec/hapdec.c      | 278 
> ++++++++++++++++++++++++++++++++++++-----------
>  libavcodec/hapenc.c      | 190 ++++++++++++++++++++++++++------
>  tests/fate/video.mak     |   3 +
>  tests/ref/fate/hap-chunk |   2 +
>  7 files changed, 481 insertions(+), 115 deletions(-)
>  create mode 100644 libavcodec/hap.c
>  create mode 100644 tests/ref/fate/hap-chunk

I believe this patch should be split in two for encoder and decoder,
to facilitate review and file history.
I'll send two separate reviews for this.

> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 008207b..13efd97 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -236,8 +236,8 @@ OBJS-$(CONFIG_H264_MMAL_DECODER)       += mmaldec.o
>  OBJS-$(CONFIG_H264_NVENC_ENCODER)      += nvenc_h264.o
>  OBJS-$(CONFIG_H264_QSV_DECODER)        += qsvdec_h264.o
>  OBJS-$(CONFIG_H264_QSV_ENCODER)        += qsvenc_h264.o
> -OBJS-$(CONFIG_HAP_DECODER)             += hapdec.o
> -OBJS-$(CONFIG_HAP_ENCODER)             += hapenc.o
> +OBJS-$(CONFIG_HAP_DECODER)             += hapdec.o hap.o
> +OBJS-$(CONFIG_HAP_ENCODER)             += hapenc.o hap.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 
> hevc_parse.o hevc_data.o
> diff --git a/libavcodec/hap.c b/libavcodec/hap.c
> new file mode 100644
> index 0000000..1163be2
> --- /dev/null
> +++ b/libavcodec/hap.c
> @@ -0,0 +1,51 @@
> +/*
> + * Vidvox Hap utility functions
> + * Copyright (C) 2015 Tom Butterworth <[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 utilities
> + */
> +#include "hap.h"
> +
> +int hap_set_chunk_count(HapContext *ctx, int count, int first_in_frame)
> +{
> +    int ret = 0;
> +    if (first_in_frame == 1 && ctx->chunk_count != count) {
> +        int ret = av_reallocp_array(&ctx->chunks, count, sizeof(HapChunk));
> +        if (ret == 0)
> +            ret = av_reallocp_array(&ctx->chunk_results, count, sizeof(int));
> +        if (ret < 0) {
> +            ctx->chunk_count = 0;
> +        } else {
> +            ctx->chunk_count = count;
> +        }
> +    } else if (ctx->chunk_count != count) {
> +        ret = AVERROR_INVALIDDATA;
> +    }
> +    return ret;
> +}

would you mind adding a comment to this function, so that people
unfamiliar with hap can understand what is going on?

> diff --git a/libavcodec/hap.h b/libavcodec/hap.h
> index 1250a6f..482c239 100644
> --- a/libavcodec/hap.h
> +++ b/libavcodec/hap.h
> @@ -1,6 +1,7 @@
>  /*
>   * Vidvox Hap
>   * Copyright (C) 2015 Vittorio Giovara <[email protected]>
> + * and Tom Butterworth <[email protected]>

nit: feel free to add a full line here (and other files), like
   * Copyright (C) 2015 Tom Butterworth <[email protected]>

>   *
>   * This file is part of Libav.
>   *
> @@ -29,37 +30,66 @@
>  #include "bytestream.h"
>  #include "texturedsp.h"
>
> +enum HapTextureFormat {
> +    HAP_FMT_RGBDXT1   = 0x0B,
> +    HAP_FMT_RGBADXT5  = 0x0E,
> +    HAP_FMT_YCOCGDXT5 = 0x0F,
> +};
> +
> +enum HapCompressor {
> +    HAP_COMP_NONE    = 0xA0,
> +    HAP_COMP_SNAPPY  = 0xB0,
> +    HAP_COMP_COMPLEX = 0xC0,
> +};
> +
> +enum HapSectionType {
> +    HAP_ST_DECODE_INSTRUCTIONS = 0x01,
> +    HAP_ST_COMPRESSOR_TABLE    = 0x02,
> +    HAP_ST_SIZE_TABLE          = 0x03,
> +    HAP_ST_OFFSET_TABLE        = 0x04,
> +};
> +
> +typedef struct HapChunk {
> +    enum HapCompressor compressor;
> +    int compressed_offset;
> +    size_t compressed_size;
> +    int uncompressed_offset;
> +    size_t uncompressed_size;
> +} HapChunk;
> +
>  typedef struct HapContext {
>      AVClass *class;
>
>      TextureDSPContext dxtc;
>      GetByteContext gbc;
> -    PutByteContext pbc;
>
> -    int section_type;        /* Header type */
> +    enum HapTextureFormat opt_tex_fmt; /* Texture type (encoder only) */
> +    int opt_chunk_count; /* User-requested chunk count (encoder only) */

> -};
> +/*
> + Set the number of chunks in the frame. Returns 0 on success or an error if:
> + - first_in_frame is 0 and the number of chunks has changed
> + - any other error occurs
> +*/

nit: please keep * at the beginning of comments

> -    if ((avctx->codec_tag == MKTAG('H','a','p','1') && (ctx->section_type & 
> 0x0F) != HAP_FMT_RGBDXT1)
> -        || (avctx->codec_tag == MKTAG('H','a','p','5') && (ctx->section_type 
> & 0x0F) != HAP_FMT_RGBADXT5)
> -        || (avctx->codec_tag == MKTAG('H','a','p','Y') && (ctx->section_type 
> & 0x0F) != HAP_FMT_YCOCGDXT5))
> +    if ((avctx->codec_tag == MKTAG('H','a','p','1') && (section_type & 0x0F) 
> != HAP_FMT_RGBDXT1)
> +        || (avctx->codec_tag == MKTAG('H','a','p','5') && (section_type & 
> 0x0F) != HAP_FMT_RGBADXT5)
> +        || (avctx->codec_tag == MKTAG('H','a','p','Y') && (section_type & 
> 0x0F) != HAP_FMT_YCOCGDXT5))
>          return AVERROR_INVALIDDATA;

stray change?

> -    switch (ctx->section_type & 0x0F) {
> +    switch (section_type & 0x0F) {
>      case HAP_FMT_RGBDXT1:
>          texture_name = "DXT1";
>          break;
> @@ -94,49 +181,102 @@ static int setup_texture(AVCodecContext *avctx, size_t 
> length)
>          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 HAP_COMP_NONE:
> -        /* Only DXTC texture compression */
> -        ctx->tex_data = gbc->buffer;
> -        ctx->tex_size = length;
> -        compressorstr = "none";
> -        break;
> -    case HAP_COMP_SNAPPY:
> -        snappy_size = ff_snappy_peek_uncompressed_length(gbc);
> -        ret = av_reallocp(&ctx->snappied, snappy_size);
> -        if (ret < 0) {
> -            return ret;
> +    switch (section_type & 0xF0) {
> +        case HAP_COMP_NONE:
> +        case HAP_COMP_SNAPPY:
> +            ret = hap_set_chunk_count(ctx, 1, 1);
> +            if (ret == 0) {
> +                ctx->chunks[0].compressor = section_type & 0xF0;
> +                ctx->chunks[0].compressed_offset = 0;
> +                ctx->chunks[0].compressed_size = section_size;
> +            }
> +            if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
> +                compressorstr = "none";
> +            } else {
> +                compressorstr = "snappy";
> +            }
> +            break;
> +        case HAP_COMP_COMPLEX:
> +            ret = parse_section_header(gbc, &section_size, &section_type);
> +            if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
> +                ret = AVERROR_INVALIDDATA;
> +            if (ret == 0)
> +                ret = hap_parse_decode_instructions(ctx, section_size);
> +            compressorstr = "complex";
> +            break;
> +        default:
> +            ret = AVERROR_INVALIDDATA;
> +            break;
> +    }
> +
> +    if (ret != 0)
> +        return ret;
> +
> +    /* Check the frame is valid and read the uncompressed chunk sizes */
> +    ctx->tex_size = 0;
> +    for (i = 0; i < ctx->chunk_count; i++) {
> +        HapChunk *chunk = &ctx->chunks[i];
> +
> +        /* Check the compressed buffer is valid */
> +        if (chunk->compressed_offset + chunk->compressed_size > 
> bytestream2_get_bytes_left(gbc))
> +            return AVERROR_INVALIDDATA;
> +
> +        /* Chunks are unpacked sequentially, ctx->tex_size is the 
> uncompressed
> +           size thus far */
> +        chunk->uncompressed_offset = ctx->tex_size;
> +
> +        /* Fill out uncompressed size */
> +        if (chunk->compressor == HAP_COMP_SNAPPY) {
> +            GetByteContext gbc_tmp;
> +            int64_t uncompressed_size;
> +            bytestream2_init(&gbc_tmp, gbc->buffer + 
> chunk->compressed_offset,
> +                             chunk->compressed_size);
> +            uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
> +            if (uncompressed_size < 0) {
> +                return uncompressed_size;
> +            }
> +            chunk->uncompressed_size = uncompressed_size;
> +        } else if (chunk->compressor == HAP_COMP_NONE) {
> +            chunk->uncompressed_size = chunk->compressed_size;
> +        } else {
> +            return AVERROR_INVALIDDATA;
>          }
> +        ctx->tex_size += chunk->uncompressed_size;
> +    }
> +
> +    av_log(avctx, AV_LOG_DEBUG, "%s texture with %s compressor\n",
> +           texture_name, compressorstr);
> +
> +    return ret;
> +}
> +
> +static int decompress_chunks_thread(AVCodecContext *avctx, void *arg,
> +                                    int chunk_nb, int thread_nb)
> +{
> +    HapContext *ctx = avctx->priv_data;
> +
> +    HapChunk *chunk = &ctx->chunks[chunk_nb];
> +    GetByteContext gbc;
> +    uint8_t *dst = ctx->tex_buf + chunk->uncompressed_offset;
> +
> +    bytestream2_init(&gbc, ctx->gbc.buffer + chunk->compressed_offset, 
> chunk->compressed_size);
> +
> +    if (chunk->compressor == HAP_COMP_SNAPPY) {
> +        int ret;
> +        int64_t uncompressed_size = ctx->tex_size;
>          /* Uncompress the frame */
> -        ret = ff_snappy_uncompress(gbc, ctx->snappied, &snappy_size);
> +        ret = ff_snappy_uncompress(&gbc, dst, &uncompressed_size);
>          if (ret < 0) {
>               av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
>               return ret;
>          }
> -
> -        ctx->tex_data = ctx->snappied;
> -        ctx->tex_size = snappy_size;
> -        compressorstr = "snappy";
> -        break;
> -    case HAP_COMP_COMPLEX:
> -        compressorstr = "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;
> +    } else if (chunk->compressor == HAP_COMP_NONE) {
> +        bytestream2_get_buffer(&gbc, dst, chunk->compressed_size);
>      }
>
> -    av_log(avctx, AV_LOG_DEBUG, "%s texture with %s compressor\n",
> -           texture_name, compressorstr);
> -
>      return 0;
>  }
>
> @@ -159,20 +299,13 @@ static int hap_decode(AVCodecContext *avctx, void *data,
>  {
>      HapContext *ctx = avctx->priv_data;
>      ThreadFrame tframe;
> -    int ret, length;
> +    int ret, i;
>      int blocks = avctx->coded_width * avctx->coded_height / (TEXTURE_BLOCK_W 
> * TEXTURE_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);
> +    ret = hap_parse_frame_header(avctx);
>      if (ret < 0)
>          return ret;
>
> @@ -184,6 +317,27 @@ static int hap_decode(AVCodecContext *avctx, void *data,
>      if (avctx->codec->update_thread_context)
>          ff_thread_finish_setup(avctx);
>
> +    /* Unpack the DXT texture */
> +    if (hap_can_use_tex_in_place(ctx)) {
> +        /* Only DXTC texture compression in a contiguous block */
> +        ctx->tex_data = ctx->gbc.buffer;
> +    } else {
> +        /* Perform the second-stage decompression */
> +        ret = av_reallocp(&ctx->tex_buf, ctx->tex_size);
> +        if (ret < 0)
> +            return ret;
> +
> +        avctx->execute2(avctx, decompress_chunks_thread, NULL,
> +                        ctx->chunk_results, ctx->chunk_count);
> +
> +        for (i = 0; i < ctx->chunk_count; i++) {
> +            if (ctx->chunk_results[i] < 0)
> +                return ctx->chunk_results[i];
> +        }
> +
> +        ctx->tex_data = ctx->tex_buf;
> +    }
> +
>      /* Use the decompress function on the texture, one block per thread */
>      avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, 
> blocks);
>
> @@ -238,7 +392,7 @@ static av_cold int hap_close(AVCodecContext *avctx)
>  {
>      HapContext *ctx = avctx->priv_data;
>
> -    av_freep(&ctx->snappied);
> +    hap_free_context(ctx);
>
>      return 0;
>  }

The rest is probably ok.
-- 
Vittorio
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to