From patchwork Sun Jul 16 12:31:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paras X-Patchwork-Id: 4326 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp3504461vsb; Sun, 16 Jul 2017 05:39:45 -0700 (PDT) X-Received: by 10.28.218.145 with SMTP id r139mr1350695wmg.69.1500208785678; Sun, 16 Jul 2017 05:39:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500208785; cv=none; d=google.com; s=arc-20160816; b=Z9EkquA2jvN6dTnTY2Fx8YHUqS7qZ51gSY47j8y+/dO4EQZVz7MhnsuaA2VWhFMxFy nof9mWuctbqqGrLxIXifd3W0ck0GRNhfKlTykk25b0D6VjOVaNDy5mqThGye2tAwO6wk 1sQJK69tPixBXA0qoOHf97AD3nnAPUrSvPqEMtMJHmNgSS/ECIPJlA5TR4w7xKFT7r62 iseNVy9xqnbEwonvZrZt6yxGLvstDPUfhnKiSo3vDVakhpBi3JCA5BkrwzOC0Hi6fgF7 NDCCkIG0ZBxQwokFPqUSxXDBRoqbIFjeLDxYQrDrjaeHR+TNcII2xLQjfFwtA4oFd5k7 v9tA== 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=wbkWUddaVeR7YsUNN6cIok3sWnuZ34hvZ/XIWp6co9A=; b=d8d5MSEOazqScyr0GC3a6z6Jmv2bSJoyowteeu47YF3NeTh8aM9dPOllQYdKe/QQ9u BZ0Oj7IqtHl533VnjbZEjvIZRUoP5cIdNfWYjmU1ShGuZsQX/oS1hB/gbd/V3M4NMbj7 vcXqjwEfnn8Lkm9j2R1a3SWCFd+Qs7yYj2rz5iTH1dbhfgMuyLmvlwhUoHAShgFrVCZw OvxrVhtkKNGFAli6juoYuw5rDkZSuGq3ykwIoSozgLIXCjLUvoR+DJnjru4JK9WEA/LM wVU1VI2bqt6dhQGRJPo+9ZcJKtglTGboccFFpu5zKOt6GVKkjQuXjN4K4tQLTbV1IWbk GS2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=eu6nY5IF; 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 g141si1036056wme.130.2017.07.16.05.39.45; Sun, 16 Jul 2017 05:39:45 -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.b=eu6nY5IF; 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 6AC3C689AA2; Sun, 16 Jul 2017 15:39:36 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f196.google.com (mail-pf0-f196.google.com [209.85.192.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 776296804F8 for ; Sun, 16 Jul 2017 15:39:29 +0300 (EEST) Received: by mail-pf0-f196.google.com with SMTP id a11so155639pfj.3 for ; Sun, 16 Jul 2017 05:39:36 -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=kU3n7Q81YufxcoO5aBSLKBFFlSOaxB4TeGNXaennRB4=; b=eu6nY5IFEOvPvjgzGCXX+CdMLwJXSo1PPTcVn6SE/D5VkS/GwasvSafPpOUwSWbx5G ITRmpMHpjprgVs4HILoVS7lEO/fRANvdbWXwdaaAFWsFhqipzsc+OddpWye/OrPhe3z5 Yoe19EdjDq6H/zs1EKeliqKxkTotxT5wSWBsBgmsZe2B07RIrZd6Eu/8SV7I2e6x4Uhp 7zVGU6tO1VrsAAo90ELu0Wda55xwKEb2TLsKG/5ntQDgPD20E3qBMV9p4oAPZBvGg5j1 QjWP5YK/ZdkwXh2akb6Htwa7xrt9ZLDbA5rJeEUmcBNwOEPOAKmM266mpddoM6xqSi+D qqzQ== 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=kU3n7Q81YufxcoO5aBSLKBFFlSOaxB4TeGNXaennRB4=; b=h4JzmXeK7a0bcaoDFE7SREzPBU8TR6Qo2xkaNOdd3Sy2iZmcogksi6M3Z2wrHcDuhV 3D+Sq/9PkATdxxlLOQrwQGcHVFZHzfw/76xeBVKTMMwahcO7JDATN9OtfQv+ustV/Mv6 ayJ1ffTLjj9Zdvs3wolA+Up4dPSPiVooepB19/yrP4ZTySyWTFIpMYf8MMOZz4kSKDHg 9SoXeuU5Mff5N7tMTcwiPc1xo9nIjtUqMk5XrXFPjFiIiHorwVKu51EDnLOp+Dbliivg K1yb8VCczlzN9qHJYUvmSTaqH/M+VKyMmvYi76KaefkpkH/nTirEjqoBO8x9Rxc4wJMk jAfQ== X-Gm-Message-State: AIVw110VoM/W7UdRvS1NxDEULH04ELHLNIvfugkI/+iEjq/A32NOkpMg C47Ihlcfv8IOCtYv X-Received: by 10.98.202.141 with SMTP id y13mr14322057pfk.145.1500208287823; Sun, 16 Jul 2017 05:31:27 -0700 (PDT) Received: from dhcppc9.localdomain ([125.62.103.67]) by smtp.gmail.com with ESMTPSA id o1sm27209837pgq.10.2017.07.16.05.31.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 16 Jul 2017 05:31:27 -0700 (PDT) From: Paras Chadha To: ffmpeg-devel@ffmpeg.org Date: Sun, 16 Jul 2017 18:01:20 +0530 Message-Id: <1500208280-8475-1-git-send-email-paraschadha18@gmail.com> X-Mailer: git-send-email 2.4.11 In-Reply-To: <3701A443-8B2B-4901-939C-447FB52B2589@gmx.de> References: <3701A443-8B2B-4901-939C-447FB52B2589@gmx.de> 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 --- doc/general.texi | 2 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/fitsenc.c | 238 +++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/img2enc.c | 1 + 5 files changed, 243 insertions(+) create mode 100644 libavcodec/fitsenc.c diff --git a/doc/general.texi b/doc/general.texi index 8f582d5..7f389ce 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -591,6 +591,8 @@ following image formats are supported: @tab Digital Picture Exchange @item EXR @tab @tab X @tab OpenEXR +@item FITS @tab X @tab X + @tab Flexible Image Transport System @item JPEG @tab X @tab X @tab Progressive JPEG is not supported. @item JPEG 2000 @tab X @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index de1187f..9ad44e0 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -291,6 +291,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 +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 0243f47..d180dde 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -192,6 +192,7 @@ static void register_all(void) REGISTER_ENCDEC (FFV1, ffv1); REGISTER_ENCDEC (FFVHUFF, ffvhuff); REGISTER_DECODER(FIC, fic); + 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..cc3fe77 --- /dev/null +++ b/libavcodec/fitsenc.c @@ -0,0 +1,238 @@ +/* + * 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 CTYPE = '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" + +typedef struct FITSContext { + int first_image; +} FITSContext; + +static av_cold int fits_encode_init(AVCodecContext *avctx) +{ + FITSContext * fitsctx = avctx->priv_data; + fitsctx->first_image = 1; + return 0; +} + +static int write_keyword_value(uint8_t **bytestream, const char * keyword, int value) +{ + int len, ret; + uint8_t * header = * bytestream; + len = strlen(keyword); + + memcpy(header, keyword, len); + memset(header + len, ' ', 8 - len); + header[8] = '='; + header[9] = ' '; + header += 10; + + ret = snprintf(header, 70, "%d", value); + memset(header + ret, ' ', 70 - ret); + + *bytestream += 80; + return 0; +} + +static int fits_encode_frame(AVCodecContext *avctx, AVPacket *pkt, + const AVFrame *pict, int *got_packet) +{ + AVFrame * const p = (AVFrame *)pict; + FITSContext * fitsctx = avctx->priv_data; + uint8_t *bytestream, *bytestream_start, *ptr; + uint64_t header_size = 2880, data_size = 0, padded_data_size = 0; + int ret, bitpix, naxis, naxis3 = 1, bzero = 0, i, j, k, t, rgb = 0; + + switch (avctx->pix_fmt) { + case AV_PIX_FMT_GRAY8: + bitpix = 8; + naxis = 2; + break; + case AV_PIX_FMT_GRAY16BE: + bitpix = 16; + naxis = 2; + bzero = 32768; + break; + case AV_PIX_FMT_RGB24: + case AV_PIX_FMT_RGBA: + bitpix = 8; + naxis = 3; + rgb = 1; + if (avctx->pix_fmt == AV_PIX_FMT_RGB24) { + naxis3 = 3; + } else { + naxis3 = 4; + } + break; + case AV_PIX_FMT_RGB48BE: + case AV_PIX_FMT_RGBA64BE: + bitpix = 16; + naxis = 3; + bzero = 32768; + rgb = 1; + if (avctx->pix_fmt == AV_PIX_FMT_RGB48BE) { + 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, header_size + padded_data_size, 0)) < 0) + return ret; + + bytestream_start = + bytestream = pkt->data; + + if (fitsctx->first_image) { + memcpy(bytestream, "SIMPLE = ", 10); + memset(bytestream + 10, ' ', 70); + bytestream[29] = 'T'; + } else { + memcpy(bytestream, "XTENSION= 'IMAGE '", 20); + memset(bytestream + 20, ' ', 60); + } + bytestream += 80; + + write_keyword_value(&bytestream, "BITPIX", bitpix); // no of bits per pixel + write_keyword_value(&bytestream, "NAXIS", naxis); // no of dimensions of image + write_keyword_value(&bytestream, "NAXIS1", avctx->width); // first dimension i.e. width + write_keyword_value(&bytestream, "NAXIS2", avctx->height); // second dimension i.e. height + + if (rgb) + write_keyword_value(&bytestream, "NAXIS3", naxis3); // third dimension to store RGBA planes + + if (!fitsctx->first_image) { + write_keyword_value(&bytestream, "PCOUNT", 0); + write_keyword_value(&bytestream, "GCOUNT", 1); + } else { + fitsctx->first_image = 0; + } + + /* + * Since FITS does not support unsigned 16 bit integers, + * BZERO = 32768 is used to store unsigned 16 bit integers as + * signed integers so that it can be read properly. + */ + if (bitpix == 16) + write_keyword_value(&bytestream, "BZERO", bzero); + + if (rgb) { + memcpy(bytestream, "CTYPE3 = 'RGB '", 20); + memset(bytestream + 20, ' ', 60); + bytestream += 80; + } + + memcpy(bytestream, "END", 3); + memset(bytestream + 3, ' ', 77); + bytestream += 80; + + t = header_size - (bytestream - bytestream_start); + memset(bytestream, ' ', t); + bytestream += t; + + if (rgb) { + switch (avctx->pix_fmt) { + case AV_PIX_FMT_RGB24: + case AV_PIX_FMT_RGBA: + for (k = 0; k < naxis3; k++) { + for (i = 0; i < avctx->height; i++) { + ptr = p->data[0] + (avctx->height - i - 1) * p->linesize[0] + k; + for (j = 0; j < avctx->width; j++) { + bytestream_put_byte(&bytestream, ptr[0]); + ptr += naxis3; + } + } + } + break; + case AV_PIX_FMT_RGB48BE: + case AV_PIX_FMT_RGBA64BE: + for (k = 0; k < naxis3; k++) { + for (i = 0; i < avctx->height; i++) { + ptr = p->data[0] + (avctx->height - i - 1) * p->linesize[0] + k * 2; + for (j = 0; j < avctx->width; j++) { + bytestream_put_be16(&bytestream, AV_RB16(ptr) - bzero); + ptr += naxis3 * 2; + } + } + } + break; + } + } else { + for (i = 0; i < avctx->height; i++) { + ptr = p->data[0] + (avctx->height - i - 1) * p->linesize[0]; + if (bitpix == 16) { + for (j = 0; j < avctx->width; j++) { + bytestream_put_be16(&bytestream, AV_RB16(ptr) - bzero); + ptr += 2; + } + } else { + memcpy(bytestream, ptr, avctx->width); + bytestream += avctx->width; + } + } + } + + t = padded_data_size - data_size; + memset(bytestream, 0, t); + bytestream += t; + + 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, + .priv_data_size = sizeof(FITSContext), + .init = fits_encode_init, + .encode2 = fits_encode_frame, + .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_RGBA64BE, + AV_PIX_FMT_RGB48BE, + AV_PIX_FMT_RGBA, + AV_PIX_FMT_RGB24, + AV_PIX_FMT_GRAY16BE, + AV_PIX_FMT_GRAY8, + AV_PIX_FMT_NONE }, +}; diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c index 1297b1a..25283cc 100644 --- a/libavformat/img2enc.c +++ b/libavformat/img2enc.c @@ -237,6 +237,7 @@ AVOutputFormat ff_image2_muxer = { AVOutputFormat ff_image2pipe_muxer = { .name = "image2pipe", .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"), + .extensions = "fits", .priv_data_size = sizeof(VideoMuxData), .video_codec = AV_CODEC_ID_MJPEG, .write_header = write_header,