From patchwork Sat Aug 19 20:42:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paras X-Patchwork-Id: 4754 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.46.211 with SMTP id u202csp2281406vsu; Sat, 19 Aug 2017 13:49:13 -0700 (PDT) X-Received: by 10.223.166.173 with SMTP id t42mr8509151wrc.272.1503175753486; Sat, 19 Aug 2017 13:49:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503175753; cv=none; d=google.com; s=arc-20160816; b=mtwxhxgXZgdbBnF3w+jHR8S74cmPkQ52KZScOLMSIC1lEaNxPsKFcydzMsLKac9F0H dz6z8gEft47T9fU23fjSB0O8KhUTqU9WJP0MjviBjxbT2sTCGFsIomTkPyI/DrHB6Ko1 UZCugICetxT6SZvNuba2NchWeOklNudxwvrQ7u3uN6qzLalz5HytP7+yRlGLxjXYVyoU meEvrYPLRna+JgAJPpDAKPC0xJcqYbUv+ljt6GlZHgrzcdeAlTylnfE+BiII9vgJEdW9 lub0xVVrGVKnvZBySFpFyy2mx7YAqkW95nkM6cw0rw18//vosIeaajaP5TMmWQe6IkoP BNwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=dH5VwW05XwA2ofhoWQK7N0mHf7aaPzysHwZjRS74K14=; b=Y6bFi3hpDxCKjec7WvPMeL3QJYs7denVBA5wXXaXYE/2tnkqja1uTUqnDQpMerdObj LkA6KjlDaSmn98hGGc7Gtw/P59cpSvnkd4+OZBO8r2rovjPABTAL+FIHcXQ0Q+IgypEH X1VtQXSa0sNSebLEFCKudTdO5QQTZ5YeAKbbW6dGVzbC8HbxCfv5W6ATmOSuEO6e7sCP 9eKay7441ONqCy8e0lL/1XwGYW3HEO0KbxG/fkeasMcvPyZiIChHTF09FGZWqjVS2v8K J0Tta4pS7rT7NS3KtoZXR5gkO9KjSX+oFyoCYXVEQ0kIEPEtHWYbxMKXxpJcZ2RQgLFd KXHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=jOrYUyy6; 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 sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k2si3416190wmf.80.2017.08.19.13.49.11; Sat, 19 Aug 2017 13:49:13 -0700 (PDT) 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 header.s=20161025 header.b=jOrYUyy6; 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 sp=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 5878D6882E3; Sat, 19 Aug 2017 23:49:02 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f68.google.com (mail-pg0-f68.google.com [74.125.83.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7354A6805E1 for ; Sat, 19 Aug 2017 23:48:55 +0300 (EEST) Received: by mail-pg0-f68.google.com with SMTP id 123so18916553pga.5 for ; Sat, 19 Aug 2017 13:49:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=DSnAXhvWFx2MzaLNgbEfkM+mYOvWSNvgMiuxxEksre8=; b=jOrYUyy65BfgQNXZEgGx2nU1mATlan4enzDMrklIF4+VtrC01lU0YPmKkTd7dRkMDp k6Aht/2LyltzeBPxp08iEh6HTUOvljP7jQItTprpRYdVwVsPVH59hcbIUzZ9GfPmIJy1 gLYb0kTyDdBm0DowS9db6EtHZnuWpfMzbriHRqIbPG8Ch0zI+eZhQGwxNIwwZpwlYHNQ iBc95BAwLFvks71ccqxcsankUKNVy5ulaLtPKbQjmXfKZxsb4ZsdPQeCHDrrub40hMJ3 K2nKQuz6CG58qdWOskKnbWGcQsY84ozTJgIMMRydEGW4T/4nK/bnVs/+0NrcYwHuDXfc SsQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=DSnAXhvWFx2MzaLNgbEfkM+mYOvWSNvgMiuxxEksre8=; b=lRBYXW0IOnZpCWKRIiOmei0IId8890yeRqw63n7sYlpX8PqJ3xXRVGEFLg+w/xXPl/ DkpYcn6mJIawHUa/D/S89b0hHW63n0JL++xYD17PvhRDGwRs04SRMcK2PNyPWo+sXFKS BQboj8ZOeK3bRvcQk9YXKef99v9qgaIIvr6QvfwHr9ZpiNeKXmjGgBOtKbkxv1zFYaB1 EpnRcAMrTZ3ISSUXgKSxs7rGAKs2ZBzj9FG2LePzMBNWJEuaielTJh4Udfk0sPTZ64V7 C4FygsxGRLs4YZmbMEh6buzF0U+mr7DU4hw91d7OE6JPC1CpjwJ6Oo3LJ/DrP2U6D0IM Uvkg== X-Gm-Message-State: AHYfb5j9xFsVWKjsuG5vqKx3G+UEy1sY3+LGB3UbeCcjI9ibyVl42+0h h1GEIGPt7IQuABuh X-Received: by 10.98.156.9 with SMTP id f9mr9879341pfe.35.1503175381989; Sat, 19 Aug 2017 13:43:01 -0700 (PDT) Received: from dhcppc9.localdomain ([124.253.9.90]) by smtp.gmail.com with ESMTPSA id f64sm17501317pfb.83.2017.08.19.13.42.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Aug 2017 13:43:01 -0700 (PDT) From: Paras Chadha To: ffmpeg-devel@ffmpeg.org Date: Sun, 20 Aug 2017 02:12:54 +0530 Message-Id: <1503175374-27032-1-git-send-email-paraschadha18@gmail.com> X-Mailer: git-send-email 2.4.11 In-Reply-To: <1502088185-22086-1-git-send-email-paraschadha18@gmail.com> References: <1502088185-22086-1-git-send-email-paraschadha18@gmail.com> Subject: [FFmpeg-devel] [PATCH] Add FITS Encoder 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 Cc: Paras Chadha MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paras Chadha --- Add FATE coverage Changelog | 1 + doc/general.texi | 2 +- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 2 +- libavcodec/fitsenc.c | 130 +++++++++++++++++++++++++++++++++++++++ tests/fate/fits.mak | 11 ++++ tests/ref/fate/fitsenc-gbrap | 10 +++ tests/ref/fate/fitsenc-gbrap16be | 10 +++ tests/ref/fate/fitsenc-gbrp | 10 +++ tests/ref/fate/fitsenc-gbrp16be | 10 +++ tests/ref/fate/fitsenc-gray | 10 +++ tests/ref/fate/fitsenc-gray16be | 10 +++ 12 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 libavcodec/fitsenc.c create mode 100644 tests/ref/fate/fitsenc-gbrap create mode 100644 tests/ref/fate/fitsenc-gbrap16be create mode 100644 tests/ref/fate/fitsenc-gbrp create mode 100644 tests/ref/fate/fitsenc-gbrp16be create mode 100644 tests/ref/fate/fitsenc-gray create mode 100644 tests/ref/fate/fitsenc-gray16be -- 2.4.11 diff --git a/Changelog b/Changelog index 9c7a533..8977ee6 100644 --- a/Changelog +++ b/Changelog @@ -34,6 +34,7 @@ version : - floodfill video filter - pseudocolor video filter - FITS demuxer and decoder +- FITS muxer and encoder version 3.3: - CrystalHD decoder moved to new decode API diff --git a/doc/general.texi b/doc/general.texi index 01402cb..1ea7984 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -592,7 +592,7 @@ following image formats are supported: @tab Digital Picture Exchange @item EXR @tab @tab X @tab OpenEXR -@item FITS @tab @tab X +@item FITS @tab X @tab X @tab Flexible Image Transport System @item JPEG @tab X @tab X @tab Progressive JPEG is not supported. diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 42eec07..590410c 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -292,6 +292,7 @@ OBJS-$(CONFIG_FFV1_ENCODER) += ffv1enc.o ffv1.o OBJS-$(CONFIG_FFWAVESYNTH_DECODER) += ffwavesynth.o OBJS-$(CONFIG_FIC_DECODER) += fic.o OBJS-$(CONFIG_FITS_DECODER) += fitsdec.o fits.o +OBJS-$(CONFIG_FITS_ENCODER) += fitsenc.o OBJS-$(CONFIG_FLAC_DECODER) += flacdec.o flacdata.o flac.o OBJS-$(CONFIG_FLAC_ENCODER) += flacenc.o flacdata.o flac.o vorbis_data.o OBJS-$(CONFIG_FLASHSV_DECODER) += flashsv.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 8678ac2..7fe66f4 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -192,7 +192,7 @@ static void register_all(void) REGISTER_ENCDEC (FFV1, ffv1); REGISTER_ENCDEC (FFVHUFF, ffvhuff); REGISTER_DECODER(FIC, fic); - REGISTER_DECODER(FITS, fits); + REGISTER_ENCDEC (FITS, fits); REGISTER_ENCDEC (FLASHSV, flashsv); REGISTER_ENCDEC (FLASHSV2, flashsv2); REGISTER_DECODER(FLIC, flic); diff --git a/libavcodec/fitsenc.c b/libavcodec/fitsenc.c new file mode 100644 index 0000000..f67f3ad --- /dev/null +++ b/libavcodec/fitsenc.c @@ -0,0 +1,130 @@ +/* + * FITS image encoder + * Copyright (c) 2017 Paras Chadha + * + * 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 + * FITS image encoder + * + * Specification: https://fits.gsfc.nasa.gov/fits_standard.html Version 3.0 + * + * RGBA images are encoded as planes in RGBA order. So, NAXIS3 is 3 or 4 for them. + * Also CTYPE3 = 'RGB ' is added to the header to distinguish them from 3d images. + */ + +#include "libavutil/intreadwrite.h" +#include "avcodec.h" +#include "bytestream.h" +#include "internal.h" + +static int fits_encode_frame(AVCodecContext *avctx, AVPacket *pkt, + const AVFrame *pict, int *got_packet) +{ + AVFrame * const p = (AVFrame *)pict; + uint8_t *bytestream, *bytestream_start, *ptr; + const uint16_t flip = (1 << 15); + uint64_t data_size = 0, padded_data_size = 0; + int ret, bitpix, naxis3 = 1, i, j, k, bytes_left; + int map[] = {2, 0, 1, 3}; // mapping from GBRA -> RGBA as RGBA is to be stored in FITS file.. + + switch (avctx->pix_fmt) { + case AV_PIX_FMT_GRAY8: + case AV_PIX_FMT_GRAY16BE: + map[0] = 0; // grayscale images should be directly mapped + if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { + bitpix = 8; + } else { + bitpix = 16; + } + break; + case AV_PIX_FMT_GBRP: + case AV_PIX_FMT_GBRAP: + bitpix = 8; + if (avctx->pix_fmt == AV_PIX_FMT_GBRP) { + naxis3 = 3; + } else { + naxis3 = 4; + } + break; + case AV_PIX_FMT_GBRP16BE: + case AV_PIX_FMT_GBRAP16BE: + bitpix = 16; + if (avctx->pix_fmt == AV_PIX_FMT_GBRP16BE) { + naxis3 = 3; + } else { + naxis3 = 4; + } + break; + default: + av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); + return AVERROR(EINVAL); + } + + data_size = (bitpix >> 3) * avctx->height * avctx->width * naxis3; + padded_data_size = ((data_size + 2879) / 2880 ) * 2880; + + if ((ret = ff_alloc_packet2(avctx, pkt, padded_data_size, 0)) < 0) + return ret; + + bytestream_start = + bytestream = pkt->data; + + for (k = 0; k < naxis3; k++) { + for (i = 0; i < avctx->height; i++) { + ptr = p->data[map[k]] + (avctx->height - i - 1) * p->linesize[map[k]]; + if (bitpix == 16) { + for (j = 0; j < avctx->width; j++) { + // subtracting bzero is equivalent to first bit flip + bytestream_put_be16(&bytestream, AV_RB16(ptr) ^ flip); + ptr += 2; + } + } else { + memcpy(bytestream, ptr, avctx->width); + bytestream += avctx->width; + } + } + } + + bytes_left = padded_data_size - data_size; + memset(bytestream, 0, bytes_left); + bytestream += bytes_left; + + pkt->size = bytestream - bytestream_start; + pkt->flags |= AV_PKT_FLAG_KEY; + *got_packet = 1; + + return 0; +} + +AVCodec ff_fits_encoder = { + .name = "fits", + .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_FITS, + .encode2 = fits_encode_frame, + .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_GBRAP16BE, + AV_PIX_FMT_GBRP16BE, + AV_PIX_FMT_GBRP, + AV_PIX_FMT_GBRAP, + AV_PIX_FMT_GBRP, + AV_PIX_FMT_GRAY16BE, + AV_PIX_FMT_GRAY8, + AV_PIX_FMT_NONE }, +}; diff --git a/tests/fate/fits.mak b/tests/fate/fits.mak index bd90c15..8202870 100644 --- a/tests/fate/fits.mak +++ b/tests/fate/fits.mak @@ -8,6 +8,17 @@ fate-fitsdec%: CMD = framecrc -i $(SRC) -pix_fmt $(PIXFMT) FATE_FITS_DEC_PIXFMT = gray gray16 gbrp gbrp16 gbrap16 FATE_FITS += $(FATE_FITS_DEC_PIXFMT:%=fate-fitsdec-%) +fate-fitsenc%: fate-fitsdec-multi +fate-fitsenc%: PIXFMT = $(word 3, $(subst -, ,$(@))) +fate-fitsenc%: SRC = $(TARGET_SAMPLES)/fits/fits-multi.fits +fate-fitsenc%: CMD = framecrc -i $(SRC) -c:v fits -pix_fmt $(PIXFMT) + +FATE_FITS_ENC_PIXFMT = gray gray16be gbrp gbrap gbrp16be gbrap16be +FATE_FITS_ENC-$(call ENCDEC, FITS, FITS) = $(FATE_FITS_ENC_PIXFMT:%=fate-fitsenc-%) + +FATE_FITS += $(FATE_FITS_ENC-yes) +fate-fitsenc: $(FATE_FITS_ENC-yes) + FATE_FITS-$(call DEMDEC, FITS, FITS) += $(FATE_FITS) FATE_SAMPLES_FFMPEG += $(FATE_FITS-yes) diff --git a/tests/ref/fate/fitsenc-gbrap b/tests/ref/fate/fitsenc-gbrap new file mode 100644 index 0000000..3e32fc6 --- /dev/null +++ b/tests/ref/fate/fitsenc-gbrap @@ -0,0 +1,10 @@ +#tb 0: 1/1 +#media_type 0: video +#codec_id 0: fits +#dimensions 0: 72x36 +#sar 0: 0/1 +0, 0, 0, 1, 11520, 0xeb0a0cc8 +0, 1, 1, 1, 11520, 0x9d7d8e4f +0, 2, 2, 1, 11520, 0xd9e6afe7 +0, 3, 3, 1, 11520, 0x75bd7365 +0, 4, 4, 1, 11520, 0x86f107d1 diff --git a/tests/ref/fate/fitsenc-gbrap16be b/tests/ref/fate/fitsenc-gbrap16be new file mode 100644 index 0000000..1f74f4e --- /dev/null +++ b/tests/ref/fate/fitsenc-gbrap16be @@ -0,0 +1,10 @@ +#tb 0: 1/1 +#media_type 0: video +#codec_id 0: fits +#dimensions 0: 72x36 +#sar 0: 0/1 +0, 0, 0, 1, 23040, 0xfa4ff4f0 +0, 1, 1, 1, 23040, 0xf57102d4 +0, 2, 2, 1, 23040, 0x58e0294d +0, 3, 3, 1, 23040, 0x8097502a +0, 4, 4, 1, 23040, 0x65fd7b54 diff --git a/tests/ref/fate/fitsenc-gbrp b/tests/ref/fate/fitsenc-gbrp new file mode 100644 index 0000000..4101a0e --- /dev/null +++ b/tests/ref/fate/fitsenc-gbrp @@ -0,0 +1,10 @@ +#tb 0: 1/1 +#media_type 0: video +#codec_id 0: fits +#dimensions 0: 72x36 +#sar 0: 0/1 +0, 0, 0, 1, 8640, 0xf6b9f643 +0, 1, 1, 1, 8640, 0x250d77d9 +0, 2, 2, 1, 8640, 0x652595e5 +0, 3, 3, 1, 8640, 0x4b8e5963 +0, 4, 4, 1, 8640, 0xd8d611ab diff --git a/tests/ref/fate/fitsenc-gbrp16be b/tests/ref/fate/fitsenc-gbrp16be new file mode 100644 index 0000000..d4b043f --- /dev/null +++ b/tests/ref/fate/fitsenc-gbrp16be @@ -0,0 +1,10 @@ +#tb 0: 1/1 +#media_type 0: video +#codec_id 0: fits +#dimensions 0: 72x36 +#sar 0: 0/1 +0, 0, 0, 1, 17280, 0x6b87eec5 +0, 1, 1, 1, 17280, 0x1bf0fc9a +0, 2, 2, 1, 17280, 0xaf071b1e +0, 3, 3, 1, 17280, 0xfbe941fb +0, 4, 4, 1, 17280, 0xbbc26d01 diff --git a/tests/ref/fate/fitsenc-gray b/tests/ref/fate/fitsenc-gray new file mode 100644 index 0000000..46eb76c --- /dev/null +++ b/tests/ref/fate/fitsenc-gray @@ -0,0 +1,10 @@ +#tb 0: 1/1 +#media_type 0: video +#codec_id 0: fits +#dimensions 0: 72x36 +#sar 0: 0/1 +0, 0, 0, 1, 2880, 0xd2d83752 +0, 1, 1, 1, 2880, 0x77246e79 +0, 2, 2, 1, 2880, 0xdf480c1f +0, 3, 3, 1, 2880, 0x559247a9 +0, 4, 4, 1, 2880, 0xc3df2cc4 diff --git a/tests/ref/fate/fitsenc-gray16be b/tests/ref/fate/fitsenc-gray16be new file mode 100644 index 0000000..ec4e0ef --- /dev/null +++ b/tests/ref/fate/fitsenc-gray16be @@ -0,0 +1,10 @@ +#tb 0: 1/1 +#media_type 0: video +#codec_id 0: fits +#dimensions 0: 72x36 +#sar 0: 0/1 +0, 0, 0, 1, 5760, 0x317ba180 +0, 1, 1, 1, 5760, 0x439c9e40 +0, 2, 2, 1, 5760, 0x6136ff8f +0, 3, 3, 1, 5760, 0x2c331324 +0, 4, 4, 1, 5760, 0x2f0816c1