From patchwork Sun Sep 13 18:01:33 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: 22340 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 B213044B4AB for ; Sun, 13 Sep 2020 21:01:49 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8037168BBF5; Sun, 13 Sep 2020 21:01:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f65.google.com (mail-ej1-f65.google.com [209.85.218.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 617C068BB9D for ; Sun, 13 Sep 2020 21:01:43 +0300 (EEST) Received: by mail-ej1-f65.google.com with SMTP id gr14so20085250ejb.1 for ; Sun, 13 Sep 2020 11:01:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=PdBR0nGR3OZjCq9pEKGB3+m7nqRF6U/EE6tgKC8DMf4=; b=tRKAo6gYJ+B66VH1xkADiEOeJa6hY1bDg5OBjX9jI3l5VsQJpgSNmuM9Ze5Yuxm8je VVvEgXqaTRxccYw1HtUsUv/OLxnorcC69UwnXayXp1nlQEi7Tu/DJccPLJBnnfg+zYcL se6s7bU0L8K0xoE94FknGSQsLUp4w5mUZzwIPEK4TUvr4gJluLcd1B/SOJnG5uYxn1Ne GM0EMw+RczxvmfDMrhqbWE5NrXuKUwsLsDXDzj2GCvCACWa1uq006MGDhwTn2+bayWYQ 4s0kj9m1EM4//FceLNOJgEPQ2Qv0DuT5XsgSg787Vgaa/nvEyjcw0Tsah7M7v3kCrt54 rhsA== 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; bh=PdBR0nGR3OZjCq9pEKGB3+m7nqRF6U/EE6tgKC8DMf4=; b=Am5Dt428+j3ygXjnUrR081UxmC0T1nu9peQHIcK7aIw4K+QLoW6frdtDaizHQwRKf0 ciJli5wy8oKRa0+uI+LrG3TWNU96HlyaCDo6iEQxv9bN9gmeUm73WNRJNx2MAkSQcGAm miheKqopnP3itG/afS+20EhjJdaiStj3uHw5fJhnm5ATcPR+24JtPu5VQ0akIjlqwoIv OFFwHVOrBIH3cF0CHwUtVfQ4fQJSCVJOclLtGwnCBOasU1PXFGFs2/k81EsmHcOein8k umsywPEE/gWOVspkUkjROa7Wpw2TOAs2voEoQKtm2TrQcHpqymyteAw0Qof3JmFq7qzt JbBA== X-Gm-Message-State: AOAM532bohKohXVhyadQcLZp8giMsK94QSP4mL1S7OmoCxGTuay2RUTw jtuHLAQRf9XJYnX2tOBo+SKw/39cemDkSw== X-Google-Smtp-Source: ABdhPJx3MlU24oph1+ScA/BTe3n4R4lps/09zmQgUYcNFhGMgWfwWteXAOCMbGl4MUi2vgiL4bcfgg== X-Received: by 2002:a17:906:6805:: with SMTP id k5mr10742663ejr.397.1600020101979; Sun, 13 Sep 2020 11:01:41 -0700 (PDT) Received: from localhost.localdomain ([94.250.162.52]) by smtp.gmail.com with ESMTPSA id ks21sm5810244ejb.63.2020.09.13.11.01.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Sep 2020 11:01:41 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 13 Sep 2020 20:01:33 +0200 Message-Id: <20200913180133.1473-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avformat: add CRI AAX 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 --- libavcodec/adxdec.c | 16 ++ libavformat/Makefile | 1 + libavformat/aaxdec.c | 377 +++++++++++++++++++++++++++++++++++++++ libavformat/allformats.c | 1 + 4 files changed, 395 insertions(+) create mode 100644 libavformat/aaxdec.c diff --git a/libavcodec/adxdec.c b/libavcodec/adxdec.c index 40ed8e5ba7..81ffc8b296 100644 --- a/libavcodec/adxdec.c +++ b/libavcodec/adxdec.c @@ -103,6 +103,22 @@ static int adx_decode_frame(AVCodecContext *avctx, void *data, const uint8_t *buf = avpkt->data; const uint8_t *buf_end = buf + avpkt->size; int num_blocks, ch, ret; + int new_extradata_size; + uint8_t *new_extradata; + + new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, + &new_extradata_size); + if (new_extradata && new_extradata_size > 0) { + int header_size; + if ((ret = ff_adx_decode_header(avctx, new_extradata, + new_extradata_size, &header_size, + c->coeff)) < 0) { + av_log(avctx, AV_LOG_ERROR, "error parsing new ADX extradata\n"); + return AVERROR_INVALIDDATA; + } + + c->eof = 0; + } if (c->eof) { *got_frame_ptr = 0; diff --git a/libavformat/Makefile b/libavformat/Makefile index b4cc467268..883326770f 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -68,6 +68,7 @@ OBJS-$(CONFIG_SRTP) += srtp.o OBJS-$(CONFIG_A64_MUXER) += a64.o rawenc.o OBJS-$(CONFIG_AA_DEMUXER) += aadec.o OBJS-$(CONFIG_AAC_DEMUXER) += aacdec.o apetag.o img2.o rawdec.o +OBJS-$(CONFIG_AAX_DEMUXER) += aaxdec.o OBJS-$(CONFIG_AC3_DEMUXER) += ac3dec.o rawdec.o OBJS-$(CONFIG_AC3_MUXER) += rawenc.o OBJS-$(CONFIG_ACM_DEMUXER) += acm.o rawdec.o diff --git a/libavformat/aaxdec.c b/libavformat/aaxdec.c new file mode 100644 index 0000000000..a8674fde5b --- /dev/null +++ b/libavformat/aaxdec.c @@ -0,0 +1,377 @@ +/* + * AAX 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" + +typedef struct AAXColumn { + uint8_t flag; + uint8_t type; + const char *name; + uint32_t offset; + int size; +} AAXColumn; + +typedef struct AAXSegment { + int64_t start; + int64_t end; +} AAXSegment; + +typedef struct AAXContext { + int64_t table_size; + uint16_t version; + int64_t rows_offset; + int64_t strings_offset; + int64_t data_offset; + int64_t name_offset; + uint16_t columns; + uint16_t row_width; + uint32_t nb_segments; + int64_t schema_offset; + int64_t strings_size; + char *string_table; + + int64_t prev_pts; + int64_t last_segment_pts; + int current_segment; + + AAXColumn *xcolumns; + AAXSegment *segments; +} AAXContext; + +static int aax_probe(const AVProbeData *p) +{ + if (AV_RB32(p->buf) != MKBETAG('@','U','T','F')) + return 0; + if (AV_RB32(p->buf + 4) == 0) + return 0; + if (AV_RB16(p->buf + 8) > 1) + return 0; + + return AVPROBE_SCORE_MAX; +} + +enum ColumnFlag { + COLUMN_FLAG_NAME = 0x1, + COLUMN_FLAG_DEFAULT = 0x2, + COLUMN_FLAG_ROW = 0x4, + COLUMN_FLAG_UNDEFINED = 0x8 /* shouldn't exist */ +}; + +enum ColumnType { + COLUMN_TYPE_UINT8 = 0x00, + COLUMN_TYPE_SINT8 = 0x01, + COLUMN_TYPE_UINT16 = 0x02, + COLUMN_TYPE_SINT16 = 0x03, + COLUMN_TYPE_UINT32 = 0x04, + COLUMN_TYPE_SINT32 = 0x05, + COLUMN_TYPE_UINT64 = 0x06, + COLUMN_TYPE_SINT64 = 0x07, + COLUMN_TYPE_FLOAT = 0x08, + COLUMN_TYPE_DOUBLE = 0x09, + COLUMN_TYPE_STRING = 0x0a, + COLUMN_TYPE_VLDATA = 0x0b, + COLUMN_TYPE_UINT128 = 0x0c, /* for GUIDs */ + COLUMN_TYPE_UNDEFINED = -1 +}; + +static int aax_read_header(AVFormatContext *s) +{ + AAXContext *a = s->priv_data; + AVIOContext *pb = s->pb; + AVCodecParameters *par; + AVStream *st; + int64_t column_offset = 0; + int ret, extradata_size; + char *codec; + + avio_skip(pb, 4); + a->table_size = avio_rb32(pb) + 8LL; + a->version = avio_rb16(pb); + a->rows_offset = avio_rb16(pb) + 8LL; + a->strings_offset = avio_rb32(pb) + 8LL; + a->data_offset = avio_rb32(pb) + 8LL; + a->name_offset = avio_rb32(pb); + a->columns = avio_rb16(pb); + a->row_width = avio_rb16(pb); + a->nb_segments = avio_rb32(pb); + + a->schema_offset = 0x20; + a->strings_size = a->data_offset - a->strings_offset; + + if (a->rows_offset > a->table_size || + a->strings_offset > a->table_size || + a->data_offset > a->table_size) + return AVERROR_INVALIDDATA; + if (a->strings_size <= 0 || a->name_offset > a->strings_size || + a->strings_size > UINT16_MAX) + return AVERROR_INVALIDDATA; + if (a->columns <= 0) + return AVERROR_INVALIDDATA; + + a->segments = av_calloc(a->nb_segments, sizeof(*a->segments)); + if (!a->segments) + return AVERROR(ENOMEM); + + a->xcolumns = av_calloc(a->columns, sizeof(*a->xcolumns)); + if (!a->xcolumns) { + ret = AVERROR(ENOMEM); + goto fail; + } + + a->string_table = av_calloc(a->strings_size + 1, sizeof(*a->string_table)); + if (!a->string_table) { + ret = AVERROR(ENOMEM); + goto fail; + } + + for (int c = 0; c < a->columns; c++) { + uint8_t info = avio_r8(pb); + uint32_t offset = avio_rb32(pb); + int value_size; + + if (offset >= a->strings_size) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + a->xcolumns[c].flag = info >> 4; + a->xcolumns[c].type = info & 0x0F; + + switch (a->xcolumns[c].type) { + case COLUMN_TYPE_UINT8: + case COLUMN_TYPE_SINT8: + value_size = 0x01; + break; + case COLUMN_TYPE_UINT16: + case COLUMN_TYPE_SINT16: + value_size = 0x02; + break; + case COLUMN_TYPE_UINT32: + case COLUMN_TYPE_SINT32: + case COLUMN_TYPE_FLOAT: + case COLUMN_TYPE_STRING: + value_size = 0x04; + break; + case COLUMN_TYPE_VLDATA: + value_size = 0x08; + break; + case COLUMN_TYPE_UINT128: + value_size = 0x10; + break; + default: + ret = AVERROR_INVALIDDATA; + goto fail; + } + + a->xcolumns[c].size = value_size; + + if (a->xcolumns[c].flag & COLUMN_FLAG_NAME) + a->xcolumns[c].name = a->string_table + offset; + + if (a->xcolumns[c].flag & COLUMN_FLAG_DEFAULT) { + /* data is found relative to columns start */ + a->xcolumns[c].offset = avio_tell(pb) - a->schema_offset; + avio_skip(pb, value_size); + } + + if (a->xcolumns[c].flag & COLUMN_FLAG_ROW) { + /* data is found relative to row start */ + a->xcolumns[c].offset = column_offset; + column_offset += value_size; + } + } + + avio_seek(pb, a->strings_offset, SEEK_SET); + ret = avio_read(pb, a->string_table, a->strings_size); + if (ret != a->strings_size) { + if (ret < 0) + goto fail; + ret = AVERROR(EIO); + goto fail; + } + + for (int c = 0; c < a->columns; c++) { + int64_t data_offset = 0; + int64_t col_offset; + int flag, type; + + if (strcmp(a->xcolumns[c].name, "data")) + continue; + + type = a->xcolumns[c].type; + flag = a->xcolumns[c].flag; + col_offset = a->xcolumns[c].offset; + + for (int r = 0; r < a->nb_segments; r++) { + if (flag & COLUMN_FLAG_DEFAULT) { + data_offset = a->schema_offset + col_offset; + } else if (flag & COLUMN_FLAG_ROW) { + data_offset = a->rows_offset + r * a->row_width + col_offset; + } else { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + avio_seek(pb, data_offset, SEEK_SET); + if (type == COLUMN_TYPE_VLDATA) { + a->segments[r].start = avio_rb32(pb) + a->data_offset; + a->segments[r].end = a->segments[r].start + avio_rb32(pb); + } else { + ret = AVERROR_INVALIDDATA; + goto fail; + } + } + } + + st = avformat_new_stream(s, NULL); + if (!st) { + ret = AVERROR(ENOMEM); + goto fail; + } + st->start_time = 0; + par = s->streams[0]->codecpar; + par->codec_type = AVMEDIA_TYPE_AUDIO; + + codec = a->string_table + a->name_offset; + if (!strcmp(codec, "AAX")) { + par->codec_id = AV_CODEC_ID_ADPCM_ADX; + avio_seek(pb, a->segments[0].start, SEEK_SET); + if (avio_rb16(pb) != 0x8000) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + extradata_size = avio_rb16(pb) + 4; + if (extradata_size < 12) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + avio_seek(pb, -4, SEEK_CUR); + ret = ff_get_extradata(s, par, pb, extradata_size); + if (ret < 0) { + goto fail; + } + par->channels = AV_RB8 (par->extradata + 7); + par->sample_rate = AV_RB32(par->extradata + 8); + if (!par->channels || !par->sample_rate) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + avpriv_set_pts_info(st, 64, 32, par->sample_rate); + /*} else if (!strcmp(codec, "HCA") ){ + par->codec_id = AV_CODEC_ID_HCA;*/ + } else { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + return 0; +fail: + av_freep(&a->string_table); + av_freep(&a->xcolumns); + av_freep(&a->segments); + + return ret; +} + +static int aax_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + AAXContext *a = s->priv_data; + AVCodecParameters *par = s->streams[0]->codecpar; + AVIOContext *pb = s->pb; + const int size = 18 * par->channels; + int ret, extradata_size = 0; + uint8_t *extradata = NULL; + + if (avio_feof(pb)) + return AVERROR_EOF; + + if (avio_tell(pb) >= a->segments[a->current_segment].end) { + if (a->current_segment + 1 == a->nb_segments) + return AVERROR_EOF; + a->last_segment_pts = a->prev_pts; + a->current_segment++; + avio_seek(pb, a->segments[a->current_segment].start, SEEK_SET); + + if (par->codec_id == AV_CODEC_ID_ADPCM_ADX) { + if (avio_rb16(pb) != 0x8000) + return AVERROR_INVALIDDATA; + extradata_size = avio_rb16(pb) + 4; + avio_seek(pb, -4, SEEK_CUR); + if (extradata_size < 12) + return AVERROR_INVALIDDATA; + extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!extradata) + return AVERROR(ENOMEM); + if (avio_read(pb, extradata, extradata_size) != extradata_size) { + av_free(extradata); + return AVERROR(EIO); + } + memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); + } + } + + pkt->pos = avio_tell(pb); + ret = av_get_packet(pb, pkt, size); + if (ret != size) { + av_free(extradata); + return ret < 0 ? ret : AVERROR(EIO); + } + pkt->duration = 1; + pkt->stream_index = 0; + pkt->pts = a->last_segment_pts + ((pkt->pos - a->segments[a->current_segment].start) / size); + a->prev_pts = pkt->pts; + + if (extradata) { + ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, extradata, extradata_size); + if (ret < 0) { + av_free(extradata); + return ret; + } + } + + return ret; +} + +static int aax_read_close(AVFormatContext *s) +{ + AAXContext *a = s->priv_data; + + av_freep(&a->segments); + av_freep(&a->xcolumns); + av_freep(&a->string_table); + + return 0; +} + +AVInputFormat ff_aax_demuxer = { + .name = "aax", + .long_name = NULL_IF_CONFIG_SMALL("CRI AAX"), + .priv_data_size = sizeof(AAXContext), + .read_probe = aax_probe, + .read_header = aax_read_header, + .read_packet = aax_read_packet, + .read_close = aax_read_close, + .extensions = "aax", + .flags = AVFMT_GENERIC_INDEX, +}; diff --git a/libavformat/allformats.c b/libavformat/allformats.c index a23eb9e7fa..d3af6f7f18 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -31,6 +31,7 @@ extern AVOutputFormat ff_a64_muxer; extern AVInputFormat ff_aa_demuxer; extern AVInputFormat ff_aac_demuxer; +extern AVInputFormat ff_aax_demuxer; extern AVInputFormat ff_ac3_demuxer; extern AVOutputFormat ff_ac3_muxer; extern AVInputFormat ff_acm_demuxer;