From patchwork Wed Jun 24 18:27:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gautam Ramakrishnan X-Patchwork-Id: 20590 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2A55344A982 for ; Wed, 24 Jun 2020 21:55:31 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F1CA868B578; Wed, 24 Jun 2020 21:55:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io1-f65.google.com (mail-io1-f65.google.com [209.85.166.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BFA7C68B15A for ; Wed, 24 Jun 2020 21:55:23 +0300 (EEST) Received: by mail-io1-f65.google.com with SMTP id y2so3377267ioy.3 for ; Wed, 24 Jun 2020 11:55:23 -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; bh=N6Tf2hiXs2W23CGEX0ww2yaeh0DXPNwpRpbvFUkk3Bw=; b=V9l2VtzOFFslWiq0Jl93WxA3qlR8m/0BFLRJYn2ojE13IxkZI/s4+TdjtAePXVxUMG wa77akxSyM1PV8sgmTymybUhGqaKDYXNPw/fWJBXSOjZfBZWMVqmVxTM8TpD1Y0Dir3+ gV7lvRx5jFvJx6c4VUK7gtmay2R8QGCVU5hhSL9LeKxVZzkG9uoG3DpO67ZiCPDIcT8h UhYLA6V6etOvAJrQ2/B1b0JZWhw5v4HZV/n1qvkOLUIUghlNHYHv/ygA0TGahBQLLTVB cjYVdUK4wKEI6LYNTU5r/P2I5WTcdLUL3VCu/ktWdpGLrNvOVYV3BehVEh6/yOYxX0Gt oyNQ== 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; bh=N6Tf2hiXs2W23CGEX0ww2yaeh0DXPNwpRpbvFUkk3Bw=; b=logudIngqM+G5VO3XIW6YiMUbaPqr0Rjz133xxpEnDTO+qt3/hFaiQjPPnnCd1jbTa 63KrUENsoEgpzTodc096kOivyySjB97h79jdDrxqZAhzutRu/mp/d6CqyJv5Jexfydrj nzcAy6lESveE0T6D9Z7/SckEYexCBVZEmLNaPNKWrxial74XqRHjryEJCImq6QJrzO2l Xzn4ZoPiAafxxEipXBT8cXzbOcdBc95pFuFgly/KPS/3RbyCbtxH/IIoWGHZQar973/c QXwjRSvCUQnNiM6ljRxfYn84IkfsJ3ftpr64JTedGDuysRZwaa4jtmF1pSzptsy9YinL fwyw== X-Gm-Message-State: AOAM530LjqbOkx2q1eDMcHHEg3lm83r03XKREFU3hntPI9nQSV2msjz1 oHdQo6wbvp7Ng/w2a92uNWDnz3Ujeio= X-Google-Smtp-Source: ABdhPJyk3FB0p2TCePrDeEP9iOwiL6FL+9y+J1IiNEv2dK0qNL+YUbC0o1Bfu3JMAVLO5vE9MJU8mQ== X-Received: by 2002:a65:6393:: with SMTP id h19mr6133634pgv.278.1593023237406; Wed, 24 Jun 2020 11:27:17 -0700 (PDT) Received: from localhost.localdomain ([122.181.59.182]) by smtp.gmail.com with ESMTPSA id y204sm4557089pfb.111.2020.06.24.11.27.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jun 2020 11:27:16 -0700 (PDT) From: gautamramk@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 24 Jun 2020 23:57:10 +0530 Message-Id: <20200624182710.12549-1-gautamramk@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] libavcodec/pgx: Added pgx 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 Cc: Gautam Ramakrishnan MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Gautam Ramakrishnan This patch support to read and decode pgx files. --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/codec_id.h | 1 + libavcodec/pgx.h | 38 +++++++++ libavcodec/pgxdec.c | 176 +++++++++++++++++++++++++++++++++++++++ libavformat/allformats.c | 1 + libavformat/img2dec.c | 11 +++ 7 files changed, 229 insertions(+) create mode 100644 libavcodec/pgx.h create mode 100644 libavcodec/pgxdec.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 5a6ea59715..0198c244e0 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -533,6 +533,7 @@ OBJS-$(CONFIG_PCX_ENCODER) += pcxenc.o OBJS-$(CONFIG_PFM_DECODER) += pnmdec.o pnm.o OBJS-$(CONFIG_PGM_DECODER) += pnmdec.o pnm.o OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o +OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o OBJS-$(CONFIG_PGMYUV_DECODER) += pnmdec.o pnm.o OBJS-$(CONFIG_PGMYUV_ENCODER) += pnmenc.o OBJS-$(CONFIG_PGSSUB_DECODER) += pgssubdec.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index fa0c08d42e..b0217e6d6a 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -236,6 +236,7 @@ extern AVCodec ff_pcx_decoder; extern AVCodec ff_pfm_decoder; extern AVCodec ff_pgm_encoder; extern AVCodec ff_pgm_decoder; +extern AVCodec ff_pgx_decoder; extern AVCodec ff_pgmyuv_encoder; extern AVCodec ff_pgmyuv_decoder; extern AVCodec ff_pictor_decoder; diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index d885962c9c..027ef21c62 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -111,6 +111,7 @@ enum AVCodecID { AV_CODEC_ID_PPM, AV_CODEC_ID_PBM, AV_CODEC_ID_PGM, + AV_CODEC_ID_PGX, AV_CODEC_ID_PGMYUV, AV_CODEC_ID_PAM, AV_CODEC_ID_FFVHUFF, diff --git a/libavcodec/pgx.h b/libavcodec/pgx.h new file mode 100644 index 0000000000..bbe93fafe7 --- /dev/null +++ b/libavcodec/pgx.h @@ -0,0 +1,38 @@ +/* + * PGX image format + * Copyright (c) 2020 Gautam Ramakrishnan + * + * 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 + */ + +#ifndef AVCODEC_PGX_H +#define AVCODEC_PGX_H + +#include "avcodec.h" +#include "bytestream.h" + +typedef struct PGXContext { + GetByteContext g; + int depth; + int sgnd; + int width; + int height; +} PGXContext; + +int ff_pgx_decode_header(AVCodecContext *avctx, PGXContext * const s); + +#endif /* AVCODEC_PNM_H */ \ No newline at end of file diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c new file mode 100644 index 0000000000..233bf34717 --- /dev/null +++ b/libavcodec/pgxdec.c @@ -0,0 +1,176 @@ +/* + * PGX image format + * Copyright (c) 2020 Gautam Ramakrishnan + * + * 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 "avcodec.h" +#include "internal.h" +#include "pgx.h" + +static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) { + char number[10]; + int i; + int ret; + number[9] = '\0'; + for (i = 0; i < 9; i++) { + if (!bytestream2_get_bytes_left(&s->g)) + return AVERROR_INVALIDDATA; + number[i] = (char)bytestream2_get_byteu(&s->g); + if (number[i] == ' ' || number[i] == 0xA || number[i] == 0xD) { + number[i] = '\0'; + break; + } else if (number[i] < '0' || number[i] > '9') { + return AVERROR_INVALIDDATA; + } + } + if (i > 9) + return AVERROR_INVALIDDATA; + ret = (int)strtol(number, NULL, 10); + if (ret >= 0) + return ret; + return AVERROR_INVALIDDATA; +} + +int ff_pgx_decode_header(AVCodecContext *avctx, PGXContext * s) +{ + const char *header_start = "PG ML "; + int i; + int byte; + if (bytestream2_get_bytes_left(&s->g) < 13) { + return AVERROR_INVALIDDATA; + } + + for (i = 0; i < 6; i++) { + if (header_start[i] != (char)bytestream2_get_byteu(&s->g)) { + return AVERROR_INVALIDDATA; + } + } + byte = bytestream2_peek_byte(&s->g); + if (byte == '+') { + s->sgnd = 0; + bytestream2_skip(&s->g, 1); + } else if (byte == '-') { + s->sgnd = 1; + bytestream2_skip(&s->g, 1); + } + + if (bytestream2_peek_byte(&s->g) == ' ') + bytestream2_skip(&s->g, 1); + s->depth = pgx_get_number(avctx, s); + s->width = pgx_get_number(avctx, s); + s->height = pgx_get_number(avctx, s); + if (bytestream2_peek_byte(&s->g) == 0xA) + bytestream2_skip(&s->g, 1); + return 0; +} + +static inline int write_frame_16(AVPacket *avpkt, AVFrame *frame, PGXContext * const s) +{ + int i, j; + for (i = 0; i < s->height; i++) { + uint16_t* line = (uint16_t*)frame->data[0] + i*frame->linesize[0]/2; + for (j = 0; j < s->width; j++) { + int val; + if (s->sgnd) { + val = (int16_t)bytestream2_get_be16(&s->g) + (1 << (s->depth - 1)); + val <<= (16 - s->depth); + *(line + j) = val; + } else { + val = bytestream2_get_be16u(&s->g); + val <<= (16 - s->depth); + *(line + j) = val; + } + } + line += frame->linesize[0]/2; + } + return 0; +} + +static inline int write_frame_8(AVPacket *avpkt, AVFrame *frame, PGXContext * const s) +{ + int i, j; + for (i = 0; i < s->height; i++) { + uint8_t* line = frame->data[0] + i*frame->linesize[0]; + for (j = 0; j < s->width; j++) { + int val; + if (s->sgnd) { + val = bytestream2_get_byte(&s->g) + (1 << (s->depth - 1)); + val <<= (8 - s->depth); + *(line + j) = val; + } else { + val = bytestream2_get_byteu(&s->g); + val <<= (8 - s->depth); + *(line + j) = val; + } + } + line += frame->linesize[0]; + } + return 0; +} + +static int pgx_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame, AVPacket *avpkt) +{ + PGXContext * s = avctx->priv_data; + AVFrame * const p = data; + int ret; + int num_vals; + int bpp; + bytestream2_init(&s->g, avpkt->data, avpkt->size); + if ((ret = ff_pgx_decode_header(avctx, s)) < 0) + return ret; + + num_vals = s->width * s->height; + + if (ret = ff_set_dimensions(avctx, s->width, s->height) < 0) + return ret; + + if (s->depth <= 8) { + avctx->pix_fmt = AV_PIX_FMT_GRAY8; + bpp = 8; + } else if (s->depth <= 16) { + avctx->pix_fmt = AV_PIX_FMT_GRAY16BE; + bpp = 16; + } else { + av_log(avctx, AV_LOG_ERROR, "Maximum depth of 16 bits supported.\n"); + return AVERROR_PATCHWELCOME; + } + + if ((ret = ff_get_buffer(avctx, p, 0)) < 0) + return ret; + *got_frame = 1; + p->pict_type = AV_PICTURE_TYPE_I; + p->key_frame = 1; + avctx->bits_per_raw_sample = s->depth; + if (bpp == 8) + write_frame_8(avpkt, p, s); + else if (bpp == 16) + write_frame_16(avpkt, p, s); + return 0; +} + +AVCodec ff_pgx_decoder = { + .name = "pgx", + .long_name = NULL_IF_CONFIG_SMALL("PGX image"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_PGX, + .priv_data_size = sizeof(PGXContext), + .decode = pgx_decode_frame, + .capabilities = AV_CODEC_CAP_DR1, +}; \ No newline at end of file diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 97fd06debb..f8527b1fd4 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -488,6 +488,7 @@ extern AVInputFormat ff_image_pbm_pipe_demuxer; extern AVInputFormat ff_image_pcx_pipe_demuxer; extern AVInputFormat ff_image_pgmyuv_pipe_demuxer; extern AVInputFormat ff_image_pgm_pipe_demuxer; +extern AVInputFormat ff_image_pgx_pipe_demuxer; extern AVInputFormat ff_image_pictor_pipe_demuxer; extern AVInputFormat ff_image_png_pipe_demuxer; extern AVInputFormat ff_image_ppm_pipe_demuxer; diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c index ee7ceed08f..43270901ff 100644 --- a/libavformat/img2dec.c +++ b/libavformat/img2dec.c @@ -1000,6 +1000,16 @@ static int pgmyuv_probe(const AVProbeData *p) // custom FFmpeg format recognized return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0; } +static int pgx_probe(const AVProbeData *p) +{ + const uint8_t *b = p->buf; + int ret = (AV_RB32(b) & 0xFFFFFF00) == 0x50472000; + ret = ret && av_match_ext(p->filename, "pgx"); + if (ret) + return AVPROBE_SCORE_EXTENSION + 1; + return 0; +} + static int ppm_probe(const AVProbeData *p) { return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0; @@ -1094,6 +1104,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM) IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX) IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM) IMAGEAUTO_DEMUXER(pgmyuv, AV_CODEC_ID_PGMYUV) +IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX) IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR) IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG) IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)