From patchwork Wed Dec 21 20:16:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 1882 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp301307vsb; Wed, 21 Dec 2016 12:17:43 -0800 (PST) X-Received: by 10.194.191.161 with SMTP id gz1mr6048031wjc.22.1482351462947; Wed, 21 Dec 2016 12:17:42 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ld3si28944046wjc.127.2016.12.21.12.17.40; Wed, 21 Dec 2016 12:17:42 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2214A68A377; Wed, 21 Dec 2016 22:17:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wj0-f194.google.com (mail-wj0-f194.google.com [209.85.210.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DFD3668A1DE for ; Wed, 21 Dec 2016 22:17:31 +0200 (EET) Received: by mail-wj0-f194.google.com with SMTP id xy5so33548718wjc.1 for ; Wed, 21 Dec 2016 12:17:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=1b/uBAXK8UwWNsaZGS1xuADuoFaL4bQ1b7ImLmifNgA=; b=AqyItV0Jiu8c4SPxZ/TkXe+Ofk8//FNoO0ioSA2ZwQftfV09UT6Mip4W3oRkNzLXFW xxr1D1ce1DzjiubNAeRep4zB/JqN1LqHaSGJNWoJTgx6KPID6XiGD0MNS9YroXQF2dSd Cq36AqS7bYcUfZyP24Wp0XuwxS37tTtZzcMG3iR0Pquoz52FMu5rEU3alkHCWFPS9Sst aJYD4VnMR2tEUTwg+J/rxmYCXDedvy9qQW9sNMJKrQ6VS8iQ9/Q+0SHdllCdk1AsqR8t 3fsp1V/NmImMBeE/sFaRwps9vkfbItRmDpBGo+C7YV3DPHBnwjMvC/2/p6lQyV5h09SJ QB6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=1b/uBAXK8UwWNsaZGS1xuADuoFaL4bQ1b7ImLmifNgA=; b=MaO2nBAdDAgLQJ402/o21ByM6BJcuOdF9dXh8jppt9rsbLK/qPIUcCnHY+ZdtZ+U1k h5a778XViw94O0/VvDFzcCdCjATuSo3VhrbScEW2qJ8G6PIg5hp+a4hEbe8/dJHzGqEB rbYdjAgZdkDfJCy2VG7g9STcbZOC8ieY4ZaFKl8KWpXd8b3yQceAPR6qJTSgvxUjmuYR tDYoiK/Eb4yJ3/RkmKBP77vd2sPjXJ5KwZDm5sN6ruOEfgJ/+OqFbFLnOB+20N5DxWjE Ccz5QxBaE6tnt//MjIXfC1Q9XHEwylVwcDZzPjAzkCfwX5SNz2eW7e4uCQs6ipivP6fQ yVZA== X-Gm-Message-State: AIkVDXLeb68kVwVbs2BN8CidoTmb0ZUd7hudzs4zaCdna+aePZ/cxToQYeWLiHmAhcfPCg== X-Received: by 10.194.93.104 with SMTP id ct8mr6815413wjb.87.1482351451238; Wed, 21 Dec 2016 12:17:31 -0800 (PST) Received: from localhost.localdomain ([37.244.243.152]) by smtp.gmail.com with ESMTPSA id l67sm27496090wmf.0.2016.12.21.12.17.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 21 Dec 2016 12:17:29 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Wed, 21 Dec 2016 21:16:40 +0100 Message-Id: <1482351400-25204-1-git-send-email-onemda@gmail.com> X-Mailer: git-send-email 2.5.0 Subject: [FFmpeg-devel] [PATCH] avcodec: add Apple Pixlet decoder X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- Changelog | 1 + doc/general.texi | 1 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 + libavcodec/pixlet.c | 756 ++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/isom.c | 2 + 8 files changed, 770 insertions(+) create mode 100644 libavcodec/pixlet.c diff --git a/Changelog b/Changelog index b36a631..c03f8f2 100644 --- a/Changelog +++ b/Changelog @@ -9,6 +9,7 @@ version : - Support for spherical videos - configure now fails if autodetect-libraries are requested but not found - PSD Decoder +- Apple Pixlet decoder version 3.2: - libopenmpt demuxer diff --git a/doc/general.texi b/doc/general.texi index 9ea3ba3..8f88b37 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -627,6 +627,7 @@ following image formats are supported: @item ANSI/ASCII art @tab @tab X @item Apple Intermediate Codec @tab @tab X @item Apple MJPEG-B @tab @tab X +@item Apple Pixlet @tab @tab X @item Apple ProRes @tab X @tab X @item Apple QuickDraw @tab @tab X @tab fourcc: qdrw diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 23e41dd..5e8eb67 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -452,6 +452,7 @@ OBJS-$(CONFIG_PGMYUV_DECODER) += pnmdec.o pnm.o OBJS-$(CONFIG_PGMYUV_ENCODER) += pnmenc.o OBJS-$(CONFIG_PGSSUB_DECODER) += pgssubdec.o OBJS-$(CONFIG_PICTOR_DECODER) += pictordec.o cga_data.o +OBJS-$(CONFIG_PIXLET_DECODER) += pixlet.o OBJS-$(CONFIG_PJS_DECODER) += textdec.o ass.o OBJS-$(CONFIG_PNG_DECODER) += png.o pngdec.o pngdsp.o OBJS-$(CONFIG_PNG_ENCODER) += png.o pngenc.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index bbcecce..9df6390 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -281,6 +281,7 @@ void avcodec_register_all(void) REGISTER_ENCDEC (PGM, pgm); REGISTER_ENCDEC (PGMYUV, pgmyuv); REGISTER_DECODER(PICTOR, pictor); + REGISTER_DECODER(PIXLET, pixlet); REGISTER_ENCDEC (PNG, png); REGISTER_ENCDEC (PPM, ppm); REGISTER_ENCDEC (PRORES, prores); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 098debf..9699f70 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -412,6 +412,7 @@ enum AVCodecID { AV_CODEC_ID_SHEERVIDEO, AV_CODEC_ID_YLC, AV_CODEC_ID_PSD, + AV_CODEC_ID_PIXLET, /* 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 29ffcb9..f09d047 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1339,6 +1339,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("YUY2 Lossless Codec"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_PIXLET, + .type = AVMEDIA_TYPE_VIDEO, + .name = "pixlet", + .long_name = NULL_IF_CONFIG_SMALL("Apple Pixlet"), + .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, + }, /* image codecs */ { diff --git a/libavcodec/pixlet.c b/libavcodec/pixlet.c new file mode 100644 index 0000000..6b78fed --- /dev/null +++ b/libavcodec/pixlet.c @@ -0,0 +1,756 @@ +/* + * Apple Pixlet decoder + * Copyright (c) 2016 Paul B Mahol + * Copyright (c) 2015 Vittorio Giovara + * + * 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 + +#include "libavutil/imgutils.h" +#include "libavutil/intmath.h" +#include "libavutil/opt.h" + +#include "avcodec.h" +#include "bytestream.h" +#include "get_bits.h" +#include "unary.h" +#include "internal.h" +#include "thread.h" + +#define NB_LEVELS 4 + +#define H 0 +#define V 1 + +typedef struct SubBand { + unsigned width; + unsigned height; + unsigned size; + int16_t *coeffs; +} SubBand; + +typedef struct LowPass { + unsigned width; + unsigned height; + int16_t top_left; + int16_t *top_row; + int16_t *left_column; + int16_t *rest; + int16_t *prediction; +} LowPass; + +typedef struct PixletContext { + AVClass *class; + + GetByteContext gbc; + GetBitContext gbit; + + int levels; + int depth; + int lowres; + int h, w; + + int16_t *filter[2]; + float scaling[4][2][NB_LEVELS]; + LowPass lowpass[4]; + SubBand band[4][NB_LEVELS * 3 + 1]; +} PixletContext; + +static int allocate_buffers(AVCodecContext *avctx) +{ + PixletContext *ctx = avctx->priv_data; + int i, plane; + + ctx->filter[0] = av_malloc_array(ctx->h, sizeof(int16_t)); + ctx->filter[1] = av_malloc_array(FFMAX(ctx->h, ctx->w) + 16, sizeof(int16_t)); + if (!ctx->filter[0] || !ctx->filter[1]) + return AVERROR(ENOMEM); + + for (plane = 0; plane < 3; plane++) { + unsigned shift = plane > 0; + unsigned w = ctx->w >> shift; + unsigned h = ctx->h >> shift; + + ctx->lowpass[plane].prediction = av_malloc_array((w >> NB_LEVELS), sizeof(int16_t)); + ctx->lowpass[plane].top_row = av_malloc_array((w >> NB_LEVELS) - 1, sizeof(int16_t)); + ctx->lowpass[plane].left_column = av_malloc_array((h >> NB_LEVELS) - 1, sizeof(int16_t)); + ctx->lowpass[plane].rest = av_malloc_array(((h >> NB_LEVELS) - 1) * ((w >> NB_LEVELS) - 1), sizeof(int16_t)); + ctx->lowpass[plane].width = w >> NB_LEVELS; + ctx->lowpass[plane].height = h >> NB_LEVELS; + if (!ctx->lowpass[plane].prediction || + !ctx->lowpass[plane].top_row || + !ctx->lowpass[plane].left_column || + !ctx->lowpass[plane].rest) + return AVERROR(ENOMEM); + + ctx->band[plane][0].width = w >> NB_LEVELS; + ctx->band[plane][0].height = h >> NB_LEVELS; + ctx->band[plane][0].size = (w >> NB_LEVELS) * (h >> NB_LEVELS); + ctx->band[plane][0].coeffs = av_malloc_array(ctx->band[plane][0].size, sizeof(int16_t)); + if (!ctx->band[plane][0].coeffs) + return AVERROR(ENOMEM); + + for (i = 0; i < NB_LEVELS * 3; i++) { + unsigned scale = ctx->levels - (i / 3); + + ctx->band[plane][i + 1].width = w >> scale; + ctx->band[plane][i + 1].height = h >> scale; + ctx->band[plane][i + 1].size = (w >> scale) * (h >> scale); + ctx->band[plane][i + 1].coeffs = av_malloc_array(ctx->band[plane][i + 1].size, sizeof(int16_t)); + if (!ctx->band[plane][i + 1].coeffs) + return AVERROR(ENOMEM); + } + } + + return 0; +} + +static void free_buffers(AVCodecContext *avctx) +{ + PixletContext *ctx = avctx->priv_data; + int i, plane; + + av_freep(&ctx->filter[0]); + av_freep(&ctx->filter[1]); + + for (plane = 0; plane < 3; plane++) { + av_freep(&ctx->lowpass[plane].prediction); + av_freep(&ctx->lowpass[plane].top_row); + av_freep(&ctx->lowpass[plane].left_column); + av_freep(&ctx->lowpass[plane].rest); + + for (i = 0; i < NB_LEVELS * 3 + 1; i++) { + av_freep(&ctx->band[plane][i].coeffs); + } + } +} + +static av_cold int pixlet_close(AVCodecContext *avctx) +{ + PixletContext *ctx = avctx->priv_data; + free_buffers(avctx); + ctx->w = 0; + ctx->h = 0; + return 0; +} + +static av_cold int pixlet_init(AVCodecContext *avctx) +{ + avctx->pix_fmt = AV_PIX_FMT_YUV420P16; + return 0; +} + +static int read_low_coeffs(AVCodecContext *avctx, uint16_t *dst, int size) +{ + PixletContext *ctx = avctx->priv_data; + GetBitContext *b = &ctx->gbit; + unsigned cnt1, nbits, j, i = 0; + int64_t value, state = 3; + int rlen, escape, flag = 0; + + while (i < size) { + nbits = FFMIN(ff_clz((state >> 8) + 3) ^ 0x1F, 14); + + cnt1 = get_unary(b, 0, 8); + if (cnt1 < 8) { + value = show_bits(b, nbits); + if (value <= 1) { + skip_bits(b, nbits - 1); + escape = ((1 << nbits) - 1) * cnt1; + } else { + skip_bits(b, nbits); + escape = value + ((1 << nbits) - 1) * cnt1 - 1; + } + } else { + escape = get_bits(b, 16); + } + + value = -((escape + flag) & 1) | 1; + dst[i++] = value * ((escape + flag + 1) >> 1); + state = 120 * (escape + flag) + state - (120 * state >> 8); + flag = 0; + + if (state * 4 > 0xFF || i >= size) + continue; + + nbits = ((state + 8) >> 5) + (state ? ff_clz(state) : 32) - 24; + escape = av_mod_uintp2(16383, nbits); + cnt1 = get_unary(b, 0, 8); + if (cnt1 > 7) { + rlen = get_bits(b, 16); + } else { + value = show_bits(b, nbits); + if (value > 1) { + skip_bits(b, nbits); + rlen = value + escape * cnt1 - 1; + } else { + skip_bits(b, nbits - 1); + rlen = escape * cnt1; + } + } + + if (i + rlen > size) + return AVERROR_INVALIDDATA; + + for (j = 0; j < rlen; j++) + dst[i++] = 0; + + state = 0; + flag = rlen < 0xFFFF ? 1 : 0; + } + + align_get_bits(b); + return get_bits_count(b) >> 3; +} + +static int read_high_coeffs(AVCodecContext *avctx, uint8_t *src, int16_t *dst, int size, + int c, int a, int d) +{ + PixletContext *ctx = avctx->priv_data; + GetBitContext *b = &ctx->gbit; + unsigned cnt1, shbits, rlen, nbits, length, i = 0, j; + int ret, escape, pfx, value, yflag, xflag, flag = 0; + int64_t state = 3, tmp; + + if ((ret = init_get_bits8(b, src, bytestream2_get_bytes_left(&ctx->gbc))) < 0) + return ret; + + if ((a >= 0) + (a ^ (a >> 31)) - (a >> 31) != 1) { + nbits = 33 - ff_clz((a >= 0) + (a ^ (a >> 31)) - (a >> 31) - 1); + if (nbits > 16) + return AVERROR_INVALIDDATA; + } else { + nbits = 1; + } + + length = 25 - nbits; + + while (i < size) { + if (state >> 8 != -3) { + value = ff_clz((state >> 8) + 3) ^ 0x1F; + } else { + value = -1; + } + + cnt1 = get_unary(b, 0, length); + + if (cnt1 >= length) { + cnt1 = get_bits(b, nbits); + } else { + pfx = 14 + ((((uint64_t)(value - 14)) >> 32) & (value - 14)); + cnt1 *= (1 << pfx) - 1; + shbits = show_bits(b, pfx); + if (shbits <= 1) { + skip_bits(b, pfx - 1); + } else { + skip_bits(b, pfx); + cnt1 += shbits - 1; + } + } + + xflag = flag + cnt1; + yflag = xflag; + + if (flag + cnt1 == 0) { + value = 0; + } else { + xflag &= 1u; + tmp = c * ((yflag + 1) >> 1) + (c >> 1); + value = xflag + (tmp ^ -xflag); + } + + dst[i++] = value; + state += d * yflag - (d * state >> 8); + + flag = 0; + + if (state * 4 > 0xFF || i >= size) + continue; + + pfx = ((state + 8) >> 5) + (state ? ff_clz(state): 32) - 24; + escape = av_mod_uintp2(16383, pfx); + cnt1 = get_unary(b, 0, 8); + if (cnt1 < 8) { + value = show_bits(b, pfx); + if (value > 1) { + skip_bits(b, pfx); + rlen = value + escape * cnt1 - 1; + } else { + skip_bits(b, pfx - 1); + rlen = escape * cnt1; + } + } else { + if (get_bits1(b)) + value = get_bits(b, 16); + else + value = get_bits(b, 8); + + rlen = value + 8 * escape; + } + + if (rlen > 0xFFFF || i + rlen > size) + return AVERROR_INVALIDDATA; + + for (j = 0; j < rlen; j++) + dst[i++] = 0; + + state = 0; + flag = rlen < 0xFFFF ? 1 : 0; + } + + align_get_bits(b); + return get_bits_count(b) >> 3; +} + +static int read_highpass(AVCodecContext *avctx, uint8_t *ptr, int plane) +{ + PixletContext *ctx = avctx->priv_data; + int i, ret; + + for (i = 0; i < ctx->levels * 3; i++) { + int32_t a = bytestream2_get_be32(&ctx->gbc); + int32_t b = bytestream2_get_be32(&ctx->gbc); + int32_t c = bytestream2_get_be32(&ctx->gbc); + int32_t d = bytestream2_get_be32(&ctx->gbc); + int16_t *dest = ctx->band[plane][i + 1].coeffs; + unsigned size = ctx->band[plane][i + 1].size; + uint32_t magic; + + magic = bytestream2_get_be32(&ctx->gbc); + if (magic != 0xDEADBEEF) { + av_log(avctx, AV_LOG_ERROR, "wrong magic number: 0x%04x for plane %d, level %d\n", magic, plane, i); + return AVERROR_INVALIDDATA; + } + + ret = read_high_coeffs(avctx, ptr + bytestream2_tell(&ctx->gbc), dest, size, + c, (b >= FFABS(a)) ? b : a, d); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "error in highpass coefficients for plane %d, level %d\n", plane, i); + return ret; + } + bytestream2_skip(&ctx->gbc, ret); + } + + return 0; +} + +static void lowpass_prediction(int16_t *dst, int16_t *pred, int width, int height) +{ + int16_t *next, val; + int i, j; + + memset(pred, 0, width * sizeof(*pred)); + + for (i = 0; i < height; i++) { + val = pred[0] + dst[0]; + dst[0] = val; + pred[0] = val; + next = dst + 2; + for (j = 1; j < width; j++, next++) { + val = pred[j] + next[-1]; + next[-1] = val; + pred[j] = val; + next[-1] += next[-2]; + } + dst = next - 1; + } +} + +static void filter(int16_t *dest, int16_t *tmp, unsigned size, float SCALE) +{ + int16_t *low, *high, *ll, *lh, *hl, *hh; + int hsize, i, j; + float value; + + hsize = size >> 1; + low = tmp + 4; + high = &low[hsize + 8]; + + for (i = 0; i < hsize; i++) { + low[i] = dest[i]; + high[i] = dest[i + hsize]; + } + + ll = &low[hsize]; + lh = &low[hsize]; + hl = &high[hsize]; + hh = hl; + for (i = 4, j = 2; i; i--, j++, ll--, hh++, lh++, hl--) { + low[i - 5] = low[j - 1]; + lh[0] = ll[-1]; + high[i - 5] = high[j - 2]; + hh[0] = hl[-2]; + } + + for (i = 0; i < hsize; i++) { + value = low [i+1] * -0.07576144003329376f + + low [i ] * 0.8586296626673486f + + low [i-1] * -0.07576144003329376f + + high[i ] * 0.3535533905932737f + + high[i-1] * 0.3535533905932737f; + dest[i * 2] = av_clipf(value * SCALE, INT16_MIN, INT16_MAX); + } + + for (i = 0; i < hsize; i++) { + value = low [i+2] * -0.01515228715813062f + + low [i+1] * 0.3687056777514043f + + low [i ] * 0.3687056777514043f + + low [i-1] * -0.01515228715813062f + + high[i+1] * 0.07071067811865475f + + high[i ] * -0.8485281374238569f + + high[i-1] * 0.07071067811865475f; + dest[i * 2 + 1] = av_clipf(value * SCALE, INT16_MIN, INT16_MAX); + } +} + +static void reconstruction(AVCodecContext *avctx, + int16_t *dest, unsigned width, unsigned height, ptrdiff_t stride, int nb_levels, + float *scaling_H, float *scaling_V, int lowres) +{ + PixletContext *ctx = avctx->priv_data; + unsigned scaled_width, scaled_height; + float scale_H, scale_V; + int16_t *ptr, *tmp; + int i, j, k; + + scaled_height = height >> nb_levels; + scaled_width = width >> nb_levels; + tmp = ctx->filter[0]; + + for (i = 0; i < nb_levels; i++) { + scaled_width <<= 1; + scaled_height <<= 1; + scale_H = scaling_H[i + lowres]; + scale_V = scaling_V[i + lowres]; + + ptr = dest; + for (j = 0; j < scaled_height; j++) { + filter(ptr, ctx->filter[1], scaled_width, scale_V); + ptr += stride; + } + + for (j = 0; j < scaled_width; j++) { + ptr = dest + j; + for (k = 0; k < scaled_height; k++) { + tmp[k] = *ptr; + ptr += stride; + } + + filter(tmp, ctx->filter[1], scaled_height, scale_H); + + ptr = dest + j; + for (k = 0; k < scaled_height; k++) { + *ptr = tmp[k]; + ptr += stride; + } + } + } +} + +#define SQR(a) (a * a) + +static void postprocess_luma(AVFrame *frame, int w, int h, int depth) +{ + uint16_t *dsty = (uint16_t *)frame->data[0]; + int16_t *srcy = (int16_t *)frame->data[0]; + ptrdiff_t stridey = frame->linesize[0] / 2; + const float factor = 1. / ((1 << depth) - 1); + int i, j; + + for (j = 0; j < h; j++) { + for (i = 0; i < w; i++) { + dsty[i] = SQR(FFMAX(srcy[i], 0) * factor) * 65535; + } + dsty += stridey; + srcy += stridey; + } +} + +static void postprocess_chroma(AVFrame *frame, int w, int h, int depth) +{ + uint16_t *dstu = (uint16_t *)frame->data[1]; + uint16_t *dstv = (uint16_t *)frame->data[2]; + int16_t *srcu = (int16_t *)frame->data[1]; + int16_t *srcv = (int16_t *)frame->data[2]; + ptrdiff_t strideu = frame->linesize[1] / 2; + ptrdiff_t stridev = frame->linesize[2] / 2; + const int shift = 16 - depth; + int i, j; + + for (j = 0; j < h; j++) { + for (i = 0; i < w; i++) { + dstu[i] = 0x8000 + (srcu[i] << shift); + dstv[i] = 0x8000 + (srcv[i] << shift); + } + dstu += strideu; + dstv += stridev; + srcu += strideu; + srcv += stridev; + } +} + +static int decode_plane(AVCodecContext *avctx, int plane, AVPacket *avpkt, AVFrame *frame) +{ + PixletContext *ctx = avctx->priv_data; + unsigned shift = plane > 0; + int16_t *ptr; + int i, j, ret; + + for (i = ctx->levels - 1; i >= 0; i--) { + ctx->scaling[plane][H][i] = 1000000. / sign_extend(bytestream2_get_be32(&ctx->gbc), 32); + ctx->scaling[plane][V][i] = 1000000. / sign_extend(bytestream2_get_be32(&ctx->gbc), 32); + } + + bytestream2_skip(&ctx->gbc, 4); + + ctx->lowpass[plane].top_left = sign_extend(bytestream2_get_be16(&ctx->gbc), 16); + + if ((ret = init_get_bits8(&ctx->gbit, avpkt->data + bytestream2_tell(&ctx->gbc), bytestream2_get_bytes_left(&ctx->gbc))) < 0) + return ret; + + ret = read_low_coeffs(avctx, ctx->lowpass[plane].top_row, ctx->lowpass[plane].width - 1); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, top row\n", plane); + return ret; + } + + ret = read_low_coeffs(avctx, ctx->lowpass[plane].left_column, ctx->lowpass[plane].height - 1); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, left column\n", plane); + return ret; + } + + ret = read_low_coeffs(avctx, ctx->lowpass[plane].rest, + (ctx->lowpass[plane].width - 1) * (ctx->lowpass[plane].height - 1)); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "error in lowpass coefficients for plane %d, rest\n", plane); + return ret; + } + + bytestream2_skip(&ctx->gbc, ret); + if (bytestream2_get_bytes_left(&ctx->gbc) <= 0) { + av_log(avctx, AV_LOG_ERROR, "no bytes left\n"); + return AVERROR_INVALIDDATA; + } + + ret = read_highpass(avctx, avpkt->data, plane); + if (ret < 0) + return ret; + + ptr = ctx->band[plane][0].coeffs; + ptr[0] = ctx->lowpass[plane].top_left; + memcpy(ptr + 1, ctx->lowpass[plane].top_row, (ctx->lowpass[plane].width - 1) * 2); + ptr += ctx->lowpass[plane].width; + for (i = 0; i < ctx->lowpass[plane].height - 1; i++) { + ptr[0] = ctx->lowpass[plane].left_column[i]; + memcpy(ptr + 1, ctx->lowpass[plane].rest + i * (ctx->lowpass[plane].width - 1), (ctx->lowpass[plane].width - 1) * 2); + ptr += ctx->lowpass[plane].width; + } + + lowpass_prediction(ctx->band[plane][0].coeffs, ctx->lowpass[plane].prediction, + ctx->band[plane][0].width, ctx->band[plane][0].height); + + for (i = 0; i < ctx->band[plane][0].height; i++) { + memcpy(frame->data[plane] + i * frame->linesize[plane], ctx->band[plane][0].coeffs + i * ctx->band[plane][0].width, ctx->band[plane][0].width * 2); + memcpy(frame->data[plane] + i * frame->linesize[plane] + ctx->band[plane][1].width * 2, ctx->band[plane][1].coeffs + i * ctx->band[plane][1].width, ctx->band[plane][1].width * 2); + memcpy(frame->data[plane] + (i+ctx->band[plane][2].height) * frame->linesize[plane], ctx->band[plane][2].coeffs + i * ctx->band[plane][2].width, ctx->band[plane][2].width * 2); + memcpy(frame->data[plane] + (i+ctx->band[plane][3].height) * frame->linesize[plane] + ctx->band[plane][3].width * 2, ctx->band[plane][3].coeffs + i * ctx->band[plane][3].width, ctx->band[plane][3].width * 2); + } + + if (ctx->lowres < 3) { + for (i = 0; i < ctx->band[plane][4].height; i++) { + memcpy(frame->data[plane] + i * frame->linesize[plane] + ctx->band[plane][4].width * 2, ctx->band[plane][4].coeffs + i * ctx->band[plane][4].width, ctx->band[plane][4].width * 2); + } + for (i = ctx->band[plane][6].height, j = 0; i < ctx->band[plane][7].height; i++, j++) { + memcpy(frame->data[plane] + i * frame->linesize[plane], ctx->band[plane][5].coeffs + j * ctx->band[plane][5].width, ctx->band[plane][5].width * 2); + memcpy(frame->data[plane] + i * frame->linesize[plane] + ctx->band[plane][6].width * 2, ctx->band[plane][6].coeffs + j * ctx->band[plane][6].width, ctx->band[plane][6].width * 2); + } + } + + if (ctx->lowres < 2) { + for (i = 0; i < ctx->band[plane][7].height; i++) { + memcpy(frame->data[plane] + i * frame->linesize[plane] + ctx->band[plane][7].width * 2, ctx->band[plane][7].coeffs + i * ctx->band[plane][7].width, ctx->band[plane][7].width * 2); + } + for (i = ctx->band[plane][9].height, j = 0; i < ctx->band[plane][10].height; i++, j++) { + memcpy(frame->data[plane] + i * frame->linesize[plane], ctx->band[plane][8].coeffs + j * ctx->band[plane][8].width, ctx->band[plane][8].width * 2); + memcpy(frame->data[plane] + i * frame->linesize[plane] + ctx->band[plane][9].width * 2, ctx->band[plane][9].coeffs + j * ctx->band[plane][9].width, ctx->band[plane][9].width * 2); + } + } + + if (ctx->lowres < 1) { + for (i = 0, j = 0; i < ctx->band[plane][10].height; i++, j++) { + memcpy(frame->data[plane] + i * frame->linesize[plane] + ctx->band[plane][10].width * 2, ctx->band[plane][10].coeffs + j * ctx->band[plane][10].width, ctx->band[plane][10].width * 2); + } + for (i = ctx->band[plane][12].height, j = 0; i < ctx->band[plane][12].height * 2; i++, j++) { + memcpy(frame->data[plane] + i * frame->linesize[plane], ctx->band[plane][11].coeffs + j * ctx->band[plane][11].width, ctx->band[plane][11].width * 2); + memcpy(frame->data[plane] + i * frame->linesize[plane] + ctx->band[plane][12].width * 2, ctx->band[plane][12].coeffs + j * ctx->band[plane][12].width, ctx->band[plane][12].width * 2); + } + } + + reconstruction(avctx, (int16_t *)frame->data[plane], + ctx->w >> (shift + ctx->lowres), ctx->h >> (shift + ctx->lowres), + frame->linesize[plane] / 2, NB_LEVELS - ctx->lowres, + ctx->scaling[plane][H], ctx->scaling[plane][V], ctx->lowres); + + return 0; +} + +static int pixlet_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame, AVPacket *avpkt) +{ + PixletContext *ctx = avctx->priv_data; + int i, w, h, width, height, ret, version; + AVFrame *p = data; + ThreadFrame frame = { .f = data }; + uint32_t pktsize; + + bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size); + + pktsize = bytestream2_get_be32(&ctx->gbc); + if (pktsize <= 44 || pktsize - 4 > bytestream2_get_bytes_left(&ctx->gbc)) { + av_log(avctx, AV_LOG_ERROR, "Invalid packet size %u.\n", pktsize); + return AVERROR_INVALIDDATA; + } + + version = bytestream2_get_le32(&ctx->gbc); + if (version != 1) + avpriv_request_sample(avctx, "Version %d", version); + + bytestream2_skip(&ctx->gbc, 4); + if (bytestream2_get_be32(&ctx->gbc) != 1) + return AVERROR_INVALIDDATA; + bytestream2_skip(&ctx->gbc, 4); + + width = bytestream2_get_be32(&ctx->gbc); + height = bytestream2_get_be32(&ctx->gbc); + + w = FFALIGN(width, 1 << (NB_LEVELS + 1)); + h = FFALIGN(height, 1 << (NB_LEVELS + 1)); + + ctx->levels = bytestream2_get_be32(&ctx->gbc); + if (ctx->levels != NB_LEVELS) + return AVERROR_INVALIDDATA; + ctx->depth = bytestream2_get_be32(&ctx->gbc); + if (ctx->depth < 8 || ctx->depth > 15) { + avpriv_request_sample(avctx, "Depth %d", ctx->depth); + return AVERROR_INVALIDDATA; + } + + ret = ff_set_dimensions(avctx, FF_CEIL_RSHIFT(w, ctx->lowres), FF_CEIL_RSHIFT(h, ctx->lowres)); + if (ret < 0) + return ret; + avctx->width = FF_CEIL_RSHIFT(width, ctx->lowres); + avctx->height = FF_CEIL_RSHIFT(height, ctx->lowres); + + if (ctx->w != w || ctx->h != h) { + free_buffers(avctx); + ctx->w = w; + ctx->h = h; + + ret = allocate_buffers(avctx); + if (ret < 0) { + free_buffers(avctx); + ctx->w = 0; + ctx->h = 0; + return ret; + } + } + + bytestream2_skip(&ctx->gbc, 8); + + p->pict_type = AV_PICTURE_TYPE_I; + p->key_frame = 1; + + ret = ff_thread_get_buffer(avctx, &frame, 0); + if (ret < 0) + return ret; + + for (i = 0; i < 3; i++) { + ret = decode_plane(avctx, i, avpkt, frame.f); + if (ret < 0) + return ret; + if (avctx->flags & AV_CODEC_FLAG_GRAY) + break; + } + + postprocess_luma(frame.f, ctx->w >> ctx->lowres, ctx->h >> ctx->lowres, ctx->depth); + postprocess_chroma(frame.f, ctx->w >> (1 + ctx->lowres), ctx->h >> (1 + ctx->lowres), ctx->depth); + + *got_frame = 1; + + return pktsize; +} + +#if HAVE_THREADS +static int pixlet_init_thread_copy(AVCodecContext *avctx) +{ + PixletContext *ctx = avctx->priv_data; + int i, plane; + + ctx->filter[0] = NULL; + ctx->filter[1] = NULL; + + for (plane = 0; plane < 3; plane++) { + ctx->lowpass[plane].prediction = NULL; + ctx->lowpass[plane].top_row = NULL; + ctx->lowpass[plane].left_column = NULL; + ctx->lowpass[plane].rest = NULL; + + for (i = 0; i < NB_LEVELS * 3 + 1; i++) { + ctx->band[plane][i].coeffs = NULL; + } + } + ctx->w = ctx->h = 0; + + return 0; +} +#endif + +#define OFFSET(x) offsetof(PixletContext, x) +#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM + +static const AVOption options[] = { + { "lowres", "Lower the decoding resolution by a power of two", + OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, NB_LEVELS - 1, VD }, + { NULL }, +}; + +static const AVClass pixlet_class = { + .class_name = "pixlet", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVCodec ff_pixlet_decoder = { + .name = "pixlet", + .long_name = NULL_IF_CONFIG_SMALL("Apple Pixlet"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_PIXLET, + .init = pixlet_init, + .init_thread_copy = ONLY_IF_THREADS_ENABLED(pixlet_init_thread_copy), + .close = pixlet_close, + .decode = pixlet_decode_frame, + .priv_data_size = sizeof(PixletContext), + .priv_class = &pixlet_class, + .capabilities = AV_CODEC_CAP_DR1 | + AV_CODEC_CAP_FRAME_THREADS, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | + FF_CODEC_CAP_INIT_CLEANUP, +}; diff --git a/libavformat/isom.c b/libavformat/isom.c index ae10cb7..13ceef0 100644 --- a/libavformat/isom.c +++ b/libavformat/isom.c @@ -296,6 +296,8 @@ const AVCodecTag ff_codec_movvideo_tags[] = { { AV_CODEC_ID_SHEERVIDEO, MKTAG('S', 'h', 'r', '6') }, { AV_CODEC_ID_SHEERVIDEO, MKTAG('S', 'h', 'r', '7') }, + { AV_CODEC_ID_PIXLET, MKTAG('p', 'x', 'l', 't') }, + { AV_CODEC_ID_NONE, 0 }, };