From patchwork Thu Jul 20 19:46:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paras X-Patchwork-Id: 4398 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.1.76 with SMTP id 73csp24327vsb; Thu, 20 Jul 2017 12:46:49 -0700 (PDT) X-Received: by 10.223.167.9 with SMTP id c9mr7495310wrd.163.1500580009558; Thu, 20 Jul 2017 12:46:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1500580009; cv=none; d=google.com; s=arc-20160816; b=AzJU0NMTWSYJFRSGmyiTYp3QCmN1PH22txPuGowRqCoQ3TJ9/1+V6HX1TNk0M+lGUl 3MoKmh6KZmITqUwJzM1C5m9TN4L8BmNpkrEaNClXMfZ5MfLBAeEGHgA+gJUropGH4lXA xpz44WROMpPkssSAQ2OXVljRnDj2sQe7arVC4KPECa+RF5BdmswQOIImhnduWbLLsRJB H464d5vLHsNR2UiHSNaS/CtGwihFUz4fTww6YyCFyK+fKEiDFiwmzWQ5Wc9oY/YKy+WJ GlARjXMB2FamO03y5p5djjgrkYinmi0rSjbaBQrQQrBg0WyyQ4HF8CdmLzZJ2crQc7jN lhVg== 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=GOZa5z3mJeYuoTlc2fI0HvSNrJp6zu6JTqytL9rI9uE=; b=0tpO8+oupQmd4gHkF2UHw7iQqlw9VVsrGxOnFznH7ct5MkGth7bm/pPl227bhatghQ agfuaaHhi10/P1oAfXmpnniFrMb44dulIIwWgDaj6yhaOPfWEHrFRVQEgXDIjMpewkGY xe4zH/m8FIfi67wBfpq7WowFobVwHeYWLxiT61zaeyY5yce1dpq5LivTAY8IIamdSotM 0mnYxLo2iXruKh4qtOd4Hk1h8lpkvVrh7w+HTADKyQ9ps1rmqAxNpmrBtxX0LiHQIS4O 91yJHWclR7M7P2JqO0LpwHrqpuTqtVevvbJXwj8c/9AzAecBezRB2+pv/Wg8hJiWS2UN H+3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.b=SKa3/il0; 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 o5si6394374wro.63.2017.07.20.12.46.47; Thu, 20 Jul 2017 12:46:49 -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=SKa3/il0; 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 6DE8A689A7F; Thu, 20 Jul 2017 22:46:36 +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 1E3F56899C4 for ; Thu, 20 Jul 2017 22:46:29 +0300 (EEST) Received: by mail-pg0-f68.google.com with SMTP id v190so3535496pgv.1 for ; Thu, 20 Jul 2017 12:46:37 -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=sLvIpxF+82ZoqcRw6854hcrX3jU7LVqM6j/r5kHrJGg=; b=SKa3/il0UgYYR35dpsSEBzD6+KedfBM+O0eE7zO+irVnYQtpsLpzqOcNhb885pjdIl mBp7uz4wsynV4DSrm8o5ecdwmFRZZRXD3/VoonFcM8WEf97OfXMyw3abnallpsDWY39K h1JNxjA5QGinTLyUF06B7jXGZrKMNT0q2wc1VMuBZLRLffw1Y2goNzfqo3vir7bERsI/ nVznCz7Rm9NL7QYfIju3+q+GtWLIlQlc5jMpT7Ocx1PpQlpRTHkko90JBxBXGcKcp/YG YNwIwvkTDO1edtHQHB6JeDaINMSZ+Azp3Yj6QtR1J2OhuWAKn8cwrlutrl26d1Bkk3Nf vzWA== 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=sLvIpxF+82ZoqcRw6854hcrX3jU7LVqM6j/r5kHrJGg=; b=OvOjFBSa2/hUKRXVDMkK5rbUzNaES3yMTfxMn2YkBNHKrgF3egx1gIKwOaOOTuxizE xt+UcxHTaVf7tB+QNjtlzC6RghC0olWCtK7lB51c0bEbIECxW7mlj/lFev0tICzPx9Wv Kqhit5LyZF28nhjo4lK/a+rp/WIb0/KOM9gHGof3fAc4JrakYsShz9ZhUPf5QyyFw0J/ dx7gtiHPw1Rfs6OPYt3/J7nK8wLngUz9r+lHWHnMWIdQrQHQMNjTCtZE+qGgpcO4N5nB rf8ixK762hhQS62TJa1X2DkTKnXwgPrEKnTBzYItiL8dgJcdvfDepf5I45PsFBGq4akL zu/Q== X-Gm-Message-State: AIVw113ty/1uPwBVeFRTqi4CtwFWuuz23ArAntP2U+30tS1+Vj60V1KX SFC61RqpjT0AhL1d X-Received: by 10.84.215.130 with SMTP id l2mr5463248pli.116.1500579995277; Thu, 20 Jul 2017 12:46:35 -0700 (PDT) Received: from dhcppc9.localdomain ([124.253.29.164]) by smtp.gmail.com with ESMTPSA id b7sm5610412pgr.11.2017.07.20.12.46.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jul 2017 12:46:34 -0700 (PDT) From: Paras Chadha To: ffmpeg-devel@ffmpeg.org Date: Fri, 21 Jul 2017 01:16:27 +0530 Message-Id: <1500579987-14511-1-git-send-email-paraschadha18@gmail.com> X-Mailer: git-send-email 2.4.11 In-Reply-To: <46134546-6F8C-4CF8-89BE-6EFC271917BE@gmx.de> References: <46134546-6F8C-4CF8-89BE-6EFC271917BE@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 --- Made the changes suggested. doc/general.texi | 2 +- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 2 +- libavcodec/fitsenc.c | 237 +++++++++++++++++++++++++++++++++++++++++++++++++ libavformat/img2enc.c | 1 + 5 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 libavcodec/fitsenc.c -- 2.4.11 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 5348ed9..9b1429f 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 +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..cdb662b --- /dev/null +++ b/libavcodec/fitsenc.c @@ -0,0 +1,237 @@ +/* + * 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" + +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); + + memset(header, ' ', 80); + memcpy(header, keyword, len); + header[8] = '='; + header[9] = ' '; + header += 10; + ret = snprintf(header, 70, "%d", value); + header[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,