From patchwork Mon Mar 16 10:30:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 18244 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 D45F644A9BF for ; Mon, 16 Mar 2020 12:31:14 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BE2BF68B534; Mon, 16 Mar 2020 12:31:14 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5F88B68B535 for ; Mon, 16 Mar 2020 12:31:06 +0200 (EET) Received: by mail-wm1-f65.google.com with SMTP id a141so17368830wme.2 for ; Mon, 16 Mar 2020 03:31:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=w1vF6zwuSbvov9HDR7+DNTgCmPG4UPnFPqQP1eFJozY=; b=cLHQyNSGBBepS0extSVYlnK4FXsck84/FdTzhpMxL4jaKBMFe74zZIob/fnPGRdHa3 PVWlpciF3lJbZjFgu8dgZMEfnet+1idX8f3qIX8t2P7AIXlefQbdl6RnYUEs/EJgmkdA 8AwRnwWpZzPYDE85wL7LRow/SY3PcDafBPWPjP0vXvdknkKUIO5G6E1LfqLhaRMi5GCy 3fsjZgo05W3ymMEVtM5BrXl0MO9cR44H62nirbdVF5VGXA6gBmxB+vCabZ3ha+o7RHJO rv4bkvbSWSQvG473xaNmorQLkMLVh1XGw7nCB9lajWQYxD9yILrvdoAIoszlwtWhN52e fChg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=w1vF6zwuSbvov9HDR7+DNTgCmPG4UPnFPqQP1eFJozY=; b=fMnqy80H5m/+zdJdqWOfqsh5tk51Wwiq7fHYYUz4ic4Aow0awB+Y+PVS4V+DUOLHFo SpOdyhnzjuO5dI4X8YRUkiz1jhVNTGfl1zf0x0Q0LIR48ziRcSrmHK9IaWkzxv496L1q 4v8grf6thE1fXJK6IY6ojK1RRiDU4Em7ezkqcYOjTEPSFY599fRIpTvSLQwltEuRfK1H gbY2ukip+JiCK8pKjUfo7CeGK8yxTTIyq5/R69NNVf1s3Kj1A8IiuhKyaoNPRDxAYGiP 32afACtFxp57/rgjamXZuGuQRBl6+ZRUXCadFyfzMJ2Zbh4By8/LUw/fpcF22PHBEm8W ZWgA== X-Gm-Message-State: ANhLgQ0AU57EVDYAJZdlXQeFm179zpUYWFz15gBKLJ9VkOkV+yvXJKXJ +Fv4u22AwaX8Gqh8u3uJ5VBdGwDT X-Google-Smtp-Source: ADFU+vsI+Gq0fXgucT7dc/0uLA2+021V/CFqtWS/+IN89NnWgxSjNXbJ4a+Dtxgz1EsWqeKJlVVWDw== X-Received: by 2002:a1c:196:: with SMTP id 144mr27832108wmb.100.1584354665383; Mon, 16 Mar 2020 03:31:05 -0700 (PDT) Received: from localhost.localdomain ([37.244.255.176]) by smtp.gmail.com with ESMTPSA id o4sm1504431wrp.84.2020.03.16.03.31.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2020 03:31:04 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 16 Mar 2020 11:30:50 +0100 Message-Id: <20200316103050.22529-7-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200316103050.22529-1-onemda@gmail.com> References: <20200316103050.22529-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 7/7] avformat: add hca 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/hca.c | 148 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 libavformat/hca.c diff --git a/libavformat/Makefile b/libavformat/Makefile index 27615cd746..5196f998ed 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -224,6 +224,7 @@ OBJS-$(CONFIG_H263_MUXER) += rawenc.o OBJS-$(CONFIG_H264_DEMUXER) += h264dec.o rawdec.o OBJS-$(CONFIG_H264_MUXER) += rawenc.o OBJS-$(CONFIG_HASH_MUXER) += hashenc.o +OBJS-$(CONFIG_HCA_DEMUXER) += hca.o OBJS-$(CONFIG_HCOM_DEMUXER) += hcom.o pcm.o OBJS-$(CONFIG_HDS_MUXER) += hdsenc.o OBJS-$(CONFIG_HEVC_DEMUXER) += hevcdec.o rawdec.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index af10def88b..39d2c352f5 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -182,6 +182,7 @@ extern AVOutputFormat ff_h263_muxer; extern AVInputFormat ff_h264_demuxer; extern AVOutputFormat ff_h264_muxer; extern AVOutputFormat ff_hash_muxer; +extern AVInputFormat ff_hca_demuxer; extern AVInputFormat ff_hcom_demuxer; extern AVOutputFormat ff_hds_muxer; extern AVInputFormat ff_hevc_demuxer; diff --git a/libavformat/hca.c b/libavformat/hca.c new file mode 100644 index 0000000000..58575356aa --- /dev/null +++ b/libavformat/hca.c @@ -0,0 +1,148 @@ +/* + * HCA demuxer + * Copyright (c) 2020 Paul B Mahol + * + * 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 "libavutil/intreadwrite.h" +#include "avformat.h" +#include "internal.h" + +static int hca_probe(const AVProbeData *p) +{ + if (AV_RL32(p->buf) != MKTAG('H', 'C', 'A', 0)) + return 0; + + if (AV_RL32(p->buf + 8) != MKTAG('f', 'm', 't', 0)) + return 0; + + return AVPROBE_SCORE_MAX / 3; +} + +static int hca_read_header(AVFormatContext *s) +{ + AVCodecParameters *par; + AVIOContext *pb = s->pb; + AVStream *st; + uint32_t chunk; + uint16_t version; + uint32_t block_count; + uint16_t block_size; + int ret, ath_type; + + avio_skip(pb, 4); + version = avio_rb16(pb); + ath_type = version < 0x200 ? 1 : 0; + s->internal->data_offset = avio_rb16(pb); + + if (avio_rl32(pb) != MKTAG('f', 'm', 't', 0)) + return AVERROR_INVALIDDATA; + + st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + + par = st->codecpar; + par->codec_type = AVMEDIA_TYPE_AUDIO; + par->codec_id = AV_CODEC_ID_HCA; + par->codec_tag = 0; + par->channels = avio_r8(pb); + par->sample_rate = avio_rb24(pb); + block_count = avio_rb32(pb); + avio_skip(pb, 4); + ret = ff_alloc_extradata(par, 12); + if (ret < 0) + return ret; + memset(par->extradata, 0, 12); + + chunk = avio_rl32(pb); + if (chunk == MKTAG('c', 'o', 'm', 'p')) { + block_size = avio_rb16(pb); + avio_read(pb, par->extradata, 8); + avio_skip(pb, 2); + } else if (chunk == MKTAG('d', 'e', 'c', 0)) { + int count1, count2, count3, enable; + + block_size = avio_rb16(pb); + par->extradata[0] = avio_r8(pb); + par->extradata[1] = avio_r8(pb); + count1 = avio_r8(pb); + count2 = avio_r8(pb); + count3 = avio_r8(pb); + enable = avio_r8(pb); + par->extradata[2] = FFMAX(count3 >> 4, 1); + par->extradata[3] = count3 & 0xf; + par->extradata[4] = count1 + 1; + par->extradata[5] = (enable ? count2 : count1) + 1; + par->extradata[6] = par->extradata[4] - par->extradata[5]; + } else { + return AVERROR_INVALIDDATA; + } + + if (block_size < 8) { + return AVERROR_INVALIDDATA; + } + par->block_align = block_size; + st->duration = 1024 * block_count; + + while (avio_tell(pb) < s->internal->data_offset && !avio_feof(pb)) { + chunk = avio_rl32(pb) & 0x7f7f7f7f; + + if (chunk == MKTAG('v', 'b', 'r', 0)) { + avio_skip(pb, 4); + } else if (chunk == MKTAG('a', 't', 'h', 0)) { + ath_type = avio_rb16(pb); + } else if (chunk == MKTAG('l', 'o', 'o', 'p')) { + avio_skip(pb, 12); + } else if (chunk == MKTAG('c', 'i', 'p', 'h')) { + par->extradata[8] = avio_r8(pb); + par->extradata[9] = avio_r8(pb); + } else if (chunk == MKTAG('r', 'v', 'a', 0)) { + avio_skip(pb, 4); + } else if (chunk == MKTAG('c', 'o', 'm', 'm')) { + avio_skip(pb, avio_r8(pb)); + } else if (chunk == MKTAG('p', 'a', 'd', 0)) { + break; + } + } + + avio_seek(pb, s->internal->data_offset, SEEK_SET); + avpriv_set_pts_info(st, 64, 1, par->sample_rate); + + return 0; +} + +static int hca_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + AVCodecContext *codec = s->streams[0]->codec; + int ret; + + ret = av_get_packet(s->pb, pkt, codec->block_align); + pkt->duration = 1024; + return ret; +} + +AVInputFormat ff_hca_demuxer = { + .name = "hca", + .long_name = NULL_IF_CONFIG_SMALL("CRI HCA"), + .read_probe = hca_probe, + .read_header = hca_read_header, + .read_packet = hca_read_packet, + .extensions = "hca", + .flags = AVFMT_GENERIC_INDEX, +};