From patchwork Sat Aug 19 20:41:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paras X-Patchwork-Id: 4753 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.46.211 with SMTP id u202csp2281829vsu; Sat, 19 Aug 2017 13:49:54 -0700 (PDT) X-Received: by 10.223.138.237 with SMTP id z42mr8259370wrz.195.1503175794596; Sat, 19 Aug 2017 13:49:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503175794; cv=none; d=google.com; s=arc-20160816; b=NYlNEvgzSxg3jKs8vMGz1IfB7JpR+eTzPdtCY8J+jc4l3/aie9QVCmEwy7oqEAE9tq t+N2F81WSNBfnmIJhVIrA3DWFwQ9BocpmPXPZhAaSYhgGw8eyz36iGQgFAP7SWyN61H8 X9mXm2bUdxaxw4D/0idAzty6Ho16Jj3Gb6ENf6tQ8S94vvaSbHme7GGSLghvhGaWy5BS H2p2fqUiE+ZAudEYkMQTT3orPXscmVtSE590UvJAoRRL2ok5Als8PKv8+CNpE0QMgeey ySGQcg9TeX9c4j2EmkAeGMp8WcExe/MGbfFhvX5fzrHmHniG3pRr6ICgcBn3EII/TvzZ /jNw== 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=09p1kpAO09/J8xIO/qpZVDi6mrpsV06FFJ+JKQlI2Xw=; b=aPhbYuLfCyOnAyd+GFwBPLRPW9BbOYkxDKc3o2YY11ALYVAyTKao7xGmovbLdhfyMH lEQ6bTTSaj6iuWOl1/huMOx0ic6dcy6cCFnQzAhz8o2Rc8Vsp4xNkv5v0rAuQoX87iFZ gE21pzc0ccxwLwKMjFwhKO4ofxgUm/d+C4/rn3s1A84xMGLxOsEz0ZJ0jo8UerPSXdG8 Fry7yV//xfilPmAdCxcNzO/BUoiRZttsQtrZkcG2X5h9SLhyzIaH16ctqTdAhu+PZA5M +OYxXm2AppZvWlY/nJkRmj9lTZ/MNTPbFx4aT/I53oHwf4EjfKvg0grG+DypxWRU98J8 9U7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=HzMpybCA; 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 w6si6741357wra.112.2017.08.19.13.49.54; Sat, 19 Aug 2017 13:49:54 -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=HzMpybCA; 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 DA4BD6882ED; Sat, 19 Aug 2017 23:49:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f65.google.com (mail-pg0-f65.google.com [74.125.83.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 104E56805E1 for ; Sat, 19 Aug 2017 23:49:39 +0300 (EEST) Received: by mail-pg0-f65.google.com with SMTP id i12so3634922pgr.1 for ; Sat, 19 Aug 2017 13:49:45 -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=TAasieNVaF/IShjboyWtin4fs8WG9u4g49Fffs0JBnI=; b=HzMpybCADFX1/bqaofsk56SstrE3AD6cpkWUOjpMDbjJHYDIeSRq+6iR7rnP+e0/+X dIUf6HlrCS2qzRfLZYuHUr6WLegJNLlCXqIUakqrqEFWNibTFYu+ZUv58jbBFlMidc0k cC11Bx8QByt2RO5K6GZ+fOAIxKTIBZi5ohKyV3yhOQcwZGiFDmH8/tFKyhqJbCNg8qWQ QplbtwZ4Jby5GW5QOCOVogcOtk6U5C1xCT515o+lzQP97OzVM93hb4e/eDUmy7/YuRY5 hM00mRqnhJdAUctAX944ZqGnbyaXV4Iq+Kf2/lseDxE+hI49HtmdMhX/RbXJivmPvByb qawA== 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=TAasieNVaF/IShjboyWtin4fs8WG9u4g49Fffs0JBnI=; b=QMQDTtnPO4AYyOBDSYhgK4q2pzV9eMEPTcpRFC27Nh0DNkxCXog6a0krmVCLJwlxaj 6KNjJnJEHp8BeJ6cnmM5Yh6VqxM9rVbhuB1xcqzUKcVIJa5Ho6tpRUB6WcOVeZ5UK5RT TTfvhK/TuwjO4BNZwnyXYNr/fjJPs3vJJ2hFhbe9tltnQLYAVu/uy0xNGjqeNxxE/eHz sjjc7BsOskyJSvmpi3kV+qk+kSaFcY6ach2QIgjpEX9wk+jvF/IulNw2Nncch5rJglZi 2TNW1NZNB+Zyj2z8Ua4sbW7lbHnmvE4qTfJ7acqJOKb0lS+RlAIgtG/Atee9OjFsVuAa 9jDQ== X-Gm-Message-State: AHYfb5g6tYqTcJNMUGdLFqepUaF8tGgsaLgo66dpkUdahvkef/BweZ3C d04YVrCkq3yziQTw X-Received: by 10.98.200.24 with SMTP id z24mr12284326pff.205.1503175316427; Sat, 19 Aug 2017 13:41:56 -0700 (PDT) Received: from dhcppc9.localdomain ([124.253.9.90]) by smtp.gmail.com with ESMTPSA id l80sm17175410pfb.107.2017.08.19.13.41.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Aug 2017 13:41:54 -0700 (PDT) From: Paras Chadha To: ffmpeg-devel@ffmpeg.org Date: Sun, 20 Aug 2017 02:11:37 +0530 Message-Id: <1503175297-26903-1-git-send-email-paraschadha18@gmail.com> X-Mailer: git-send-email 2.4.11 In-Reply-To: <1502030515-25557-1-git-send-email-paraschadha18@gmail.com> References: <1502030515-25557-1-git-send-email-paraschadha18@gmail.com> Subject: [FFmpeg-devel] [PATCH] Add FITS Demuxer 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 libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/fitsdec.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- tests/fate/demux.mak | 3 + tests/ref/fate/fits-demux | 10 ++ 6 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 libavformat/fitsdec.c create mode 100644 tests/ref/fate/fits-demux -- 2.4.11 diff --git a/libavformat/Makefile b/libavformat/Makefile index b0ef82c..266b77a 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -164,6 +164,7 @@ OBJS-$(CONFIG_FFMETADATA_MUXER) += ffmetaenc.o OBJS-$(CONFIG_FIFO_MUXER) += fifo.o OBJS-$(CONFIG_FILMSTRIP_DEMUXER) += filmstripdec.o OBJS-$(CONFIG_FILMSTRIP_MUXER) += filmstripenc.o +OBJS-$(CONFIG_FITS_DEMUXER) += fitsdec.o OBJS-$(CONFIG_FLAC_DEMUXER) += flacdec.o rawdec.o \ flac_picture.o \ oggparsevorbis.o \ diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 1ebc142..3c12760 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -121,6 +121,7 @@ static void register_all(void) REGISTER_MUXDEMUX(FFMETADATA, ffmetadata); REGISTER_MUXER (FIFO, fifo); REGISTER_MUXDEMUX(FILMSTRIP, filmstrip); + REGISTER_DEMUXER (FITS, fits); REGISTER_MUXDEMUX(FLAC, flac); REGISTER_DEMUXER (FLIC, flic); REGISTER_MUXDEMUX(FLV, flv); diff --git a/libavformat/fitsdec.c b/libavformat/fitsdec.c new file mode 100644 index 0000000..4b288b3 --- /dev/null +++ b/libavformat/fitsdec.c @@ -0,0 +1,231 @@ +/* + * FITS demuxer + * 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 demuxer. + */ + +#include "libavutil/intreadwrite.h" +#include "internal.h" +#include "libavutil/opt.h" +#include "libavcodec/fits.h" +#include "libavutil/bprint.h" + +#define FITS_BLOCK_SIZE 2880 + +typedef struct FITSContext { + const AVClass *class; + AVRational framerate; + int first_image; + int64_t pts; +} FITSContext; + +static int fits_probe(AVProbeData *p) +{ + const uint8_t *b = p->buf; + if (!memcmp(b, "SIMPLE = T", 30)) + return AVPROBE_SCORE_MAX - 1; + return 0; +} + +static int fits_read_header(AVFormatContext *s) +{ + AVStream *st; + FITSContext * fits = s->priv_data; + + st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + + st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; + st->codecpar->codec_id = AV_CODEC_ID_FITS; + + avpriv_set_pts_info(st, 64, fits->framerate.den, fits->framerate.num); + fits->pts = 0; + fits->first_image = 1; + return 0; +} + +/** + * Parses header and checks that the current HDU contains image or not + * It also stores the header in the avbuf and stores the size of data part in data_size + * @param s pointer to AVFormat Context + * @param fits pointer to FITSContext + * @param header pointer to FITSHeader + * @param avbuf pointer to AVBPrint to store the header + * @param data_size to store the size of data part + * @return 1 if image found, 0 if any other extension and AVERROR_INVALIDDATA otherwise + */ +static int64_t is_image(AVFormatContext *s, FITSContext *fits, FITSHeader *header, + AVBPrint *avbuf, uint64_t *data_size) +{ + int i, ret, image = 0; + char buf[FITS_BLOCK_SIZE] = { 0 }; + int64_t buf_size = 0, size = 0, t; + + do { + ret = avio_read(s->pb, buf, FITS_BLOCK_SIZE); + if (ret < 0) { + return ret; + } else if (ret < FITS_BLOCK_SIZE) { + return AVERROR_INVALIDDATA; + } + + av_bprint_append_data(avbuf, buf, FITS_BLOCK_SIZE); + ret = 0; + buf_size = 0; + while(!ret && buf_size < FITS_BLOCK_SIZE) { + ret = avpriv_fits_header_parse_line(s, header, buf + buf_size, NULL); + buf_size += 80; + } + } while (!ret); + if (ret < 0) + return ret; + + image = fits->first_image || header->image_extension; + fits->first_image = 0; + + if (header->groups) { + image = 0; + if (header->naxis > 1) + size = 1; + } else if (header->naxis) { + size = header->naxisn[0]; + } else { + image = 0; + } + + for (i = 1; i < header->naxis; i++) { + if(size && header->naxisn[i] > UINT64_MAX / size) + return AVERROR_INVALIDDATA; + size *= header->naxisn[i]; + } + + if(header->pcount > UINT64_MAX - size) + return AVERROR_INVALIDDATA; + size += header->pcount; + + t = (abs(header->bitpix) >> 3) * ((int64_t) header->gcount); + if(size && t > UINT64_MAX / size) + return AVERROR_INVALIDDATA; + size *= t; + + if (!size) { + image = 0; + } else { + if(FITS_BLOCK_SIZE - 1 > UINT64_MAX - size) + return AVERROR_INVALIDDATA; + size = ((size + FITS_BLOCK_SIZE - 1) / FITS_BLOCK_SIZE) * FITS_BLOCK_SIZE; + } + *data_size = size; + return image; +} + +static int fits_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + int64_t pos, ret; + uint64_t size; + FITSContext *fits = s->priv_data; + FITSHeader header; + AVBPrint avbuf; + char *buf; + + if (fits->first_image) { + avpriv_fits_header_init(&header, STATE_SIMPLE); + } else { + avpriv_fits_header_init(&header, STATE_XTENSION); + } + + av_bprint_init(&avbuf, FITS_BLOCK_SIZE, AV_BPRINT_SIZE_UNLIMITED); + while ((ret = is_image(s, fits, &header, &avbuf, &size)) == 0) { + pos = avio_skip(s->pb, size); + if (pos < 0) + return pos; + + av_bprint_finalize(&avbuf, NULL); + av_bprint_init(&avbuf, FITS_BLOCK_SIZE, AV_BPRINT_SIZE_UNLIMITED); + avpriv_fits_header_init(&header, STATE_XTENSION); + } + if (ret < 0) + goto fail; + + if (!av_bprint_is_complete(&avbuf)) { + ret = AVERROR(ENOMEM); + goto fail; + } + + // Header is sent with the first line removed... + ret = av_new_packet(pkt, avbuf.len - 80 + size); + if (ret < 0) + goto fail; + + pkt->stream_index = 0; + pkt->flags |= AV_PKT_FLAG_KEY; + + ret = av_bprint_finalize(&avbuf, &buf); + if (ret < 0) { + av_packet_unref(pkt); + return ret; + } + + memcpy(pkt->data, buf + 80, avbuf.len - 80); + pkt->size = avbuf.len - 80; + av_freep(&buf); + ret = avio_read(s->pb, pkt->data + pkt->size, size); + if (ret < 0) { + av_packet_unref(pkt); + return ret; + } + + pkt->size += ret; + pkt->pts = fits->pts; + fits->pts++; + + return 0; + +fail: + av_bprint_finalize(&avbuf, NULL); + return ret; +} + +static const AVOption fits_options[] = { + { "framerate", "set the framerate", offsetof(FITSContext, framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "1"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM}, + { NULL }, +}; + +static const AVClass fits_demuxer_class = { + .class_name = "FITS demuxer", + .item_name = av_default_item_name, + .option = fits_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +AVInputFormat ff_fits_demuxer = { + .name = "fits", + .long_name = NULL_IF_CONFIG_SMALL("Flexible Image Transport System"), + .priv_data_size = sizeof(FITSContext), + .read_probe = fits_probe, + .read_header = fits_read_header, + .read_packet = fits_read_packet, + .priv_class = &fits_demuxer_class, + .raw_codec_id = AV_CODEC_ID_FITS, +}; diff --git a/libavformat/version.h b/libavformat/version.h index 48b81f2..a8cf4c1 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium) // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 57 -#define LIBAVFORMAT_VERSION_MINOR 76 +#define LIBAVFORMAT_VERSION_MINOR 77 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak index 8a2703f..66e26ed 100644 --- a/tests/fate/demux.mak +++ b/tests/fate/demux.mak @@ -36,6 +36,9 @@ fate-d-cinema-demux: CMD = framecrc -i $(TARGET_SAMPLES)/d-cinema/THX_Science_FL FATE_SAMPLES_DEMUX-$(CONFIG_EA_DEMUXER) += fate-d-eavp6-demux fate-d-eavp6-demux: CMD = framecrc -i $(TARGET_SAMPLES)/ea-vp6/SmallRing.vp6 -map 0 -vcodec copy +FATE_SAMPLES_DEMUX-$(CONFIG_FITS_DEMUXER) += fate-fits-demux +fate-fits-demux: CMD = framecrc -i $(TARGET_SAMPLES)/fits/fits-multi.fits -vcodec copy + FATE_SAMPLES_DEMUX-$(CONFIG_FLV_DEMUXER) += fate-flv-demux fate-flv-demux: CMD = framecrc -i $(TARGET_SAMPLES)/flv/Enigma_Principles_of_Lust-part.flv -codec copy diff --git a/tests/ref/fate/fits-demux b/tests/ref/fate/fits-demux new file mode 100644 index 0000000..85605ab --- /dev/null +++ b/tests/ref/fate/fits-demux @@ -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, 14320, 0x0ecf72e0 +0, 1, 1, 1, 14320, 0xd94af6eb +0, 2, 2, 1, 14320, 0x15c21892 +0, 3, 3, 1, 14320, 0xb18adc01 +0, 4, 4, 1, 14320, 0xc2be706d