| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * HCA demuxer | ||
| 3 | * Copyright (c) 2020 Paul B Mahol | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "libavutil/opt.h" | ||
| 23 | #include "libavutil/intreadwrite.h" | ||
| 24 | #include "libavcodec/bytestream.h" | ||
| 25 | |||
| 26 | #include "avformat.h" | ||
| 27 | #include "demux.h" | ||
| 28 | #include "internal.h" | ||
| 29 | |||
| 30 | #define HCA_MASK 0x7f7f7f7f | ||
| 31 | |||
| 32 | typedef struct HCADemuxContext { | ||
| 33 | AVClass *class; | ||
| 34 | int64_t keyl; | ||
| 35 | int64_t keyh; | ||
| 36 | int subkey; | ||
| 37 | } HCADemuxContext; | ||
| 38 | |||
| 39 | 7279 | static int hca_probe(const AVProbeData *p) | |
| 40 | { | ||
| 41 |
1/2✓ Branch 0 taken 7279 times.
✗ Branch 1 not taken.
|
7279 | if ((AV_RL32(p->buf) & HCA_MASK) != MKTAG('H', 'C', 'A', 0)) |
| 42 | 7279 | return 0; | |
| 43 | |||
| 44 | ✗ | if ((AV_RL32(p->buf + 8) & HCA_MASK) != MKTAG('f', 'm', 't', 0)) | |
| 45 | ✗ | return 0; | |
| 46 | |||
| 47 | ✗ | return AVPROBE_SCORE_MAX / 3; | |
| 48 | } | ||
| 49 | |||
| 50 | ✗ | static int hca_read_header(AVFormatContext *s) | |
| 51 | { | ||
| 52 | ✗ | HCADemuxContext *hca = s->priv_data; | |
| 53 | AVCodecParameters *par; | ||
| 54 | GetByteContext gb; | ||
| 55 | ✗ | AVIOContext *pb = s->pb; | |
| 56 | AVStream *st; | ||
| 57 | uint32_t chunk; | ||
| 58 | uint16_t version; | ||
| 59 | uint32_t block_count; | ||
| 60 | uint16_t block_size, data_offset; | ||
| 61 | int ret; | ||
| 62 | |||
| 63 | ✗ | avio_skip(pb, 4); | |
| 64 | ✗ | version = avio_rb16(pb); | |
| 65 | |||
| 66 | ✗ | data_offset = avio_rb16(pb); | |
| 67 | ✗ | if (data_offset <= 8) | |
| 68 | ✗ | return AVERROR_INVALIDDATA; | |
| 69 | |||
| 70 | ✗ | st = avformat_new_stream(s, NULL); | |
| 71 | ✗ | if (!st) | |
| 72 | ✗ | return AVERROR(ENOMEM); | |
| 73 | |||
| 74 | ✗ | par = st->codecpar; | |
| 75 | ✗ | ret = ff_alloc_extradata(par, data_offset + 10); | |
| 76 | ✗ | if (ret < 0) | |
| 77 | ✗ | return ret; | |
| 78 | |||
| 79 | ✗ | ret = avio_read(pb, par->extradata + 8, par->extradata_size - 8 - 10); | |
| 80 | ✗ | if (ret < par->extradata_size - 8 - 10) | |
| 81 | ✗ | return AVERROR(EIO); | |
| 82 | ✗ | AV_WL32(par->extradata, MKTAG('H', 'C', 'A', 0)); | |
| 83 | ✗ | AV_WB16(par->extradata + 4, version); | |
| 84 | ✗ | AV_WB16(par->extradata + 6, data_offset); | |
| 85 | ✗ | AV_WB32(par->extradata + par->extradata_size - 10, hca->keyh); | |
| 86 | ✗ | AV_WB32(par->extradata + par->extradata_size - 6, hca->keyl); | |
| 87 | ✗ | AV_WB16(par->extradata + par->extradata_size - 2, hca->subkey); | |
| 88 | |||
| 89 | ✗ | bytestream2_init(&gb, par->extradata + 8, par->extradata_size - 8); | |
| 90 | |||
| 91 | ✗ | if ((bytestream2_get_le32(&gb) & HCA_MASK) != MKTAG('f', 'm', 't', 0)) | |
| 92 | ✗ | return AVERROR_INVALIDDATA; | |
| 93 | |||
| 94 | ✗ | par->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 95 | ✗ | par->codec_id = AV_CODEC_ID_HCA; | |
| 96 | ✗ | par->codec_tag = 0; | |
| 97 | ✗ | st->codecpar->ch_layout.nb_channels = bytestream2_get_byte(&gb); | |
| 98 | ✗ | par->sample_rate = bytestream2_get_be24(&gb); | |
| 99 | ✗ | block_count = bytestream2_get_be32(&gb); | |
| 100 | ✗ | bytestream2_skip(&gb, 4); | |
| 101 | ✗ | chunk = bytestream2_get_le32(&gb) & HCA_MASK; | |
| 102 | ✗ | if (chunk == MKTAG('c', 'o', 'm', 'p')) { | |
| 103 | ✗ | block_size = bytestream2_get_be16(&gb); | |
| 104 | ✗ | } else if (chunk == MKTAG('d', 'e', 'c', 0)) { | |
| 105 | ✗ | block_size = bytestream2_get_be16(&gb); | |
| 106 | } else { | ||
| 107 | ✗ | return AVERROR_INVALIDDATA; | |
| 108 | } | ||
| 109 | |||
| 110 | ✗ | if (block_size < 8) | |
| 111 | ✗ | return AVERROR_INVALIDDATA; | |
| 112 | ✗ | par->block_align = block_size; | |
| 113 | ✗ | st->duration = 1024 * block_count; | |
| 114 | |||
| 115 | ✗ | avio_seek(pb, data_offset, SEEK_SET); | |
| 116 | ✗ | avpriv_set_pts_info(st, 64, 1, par->sample_rate); | |
| 117 | |||
| 118 | ✗ | return 0; | |
| 119 | } | ||
| 120 | |||
| 121 | ✗ | static int hca_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 122 | { | ||
| 123 | ✗ | AVCodecParameters *par = s->streams[0]->codecpar; | |
| 124 | int ret; | ||
| 125 | |||
| 126 | ✗ | ret = av_get_packet(s->pb, pkt, par->block_align); | |
| 127 | ✗ | pkt->duration = 1024; | |
| 128 | ✗ | return ret; | |
| 129 | } | ||
| 130 | |||
| 131 | #define OFFSET(x) offsetof(HCADemuxContext, x) | ||
| 132 | static const AVOption hca_options[] = { | ||
| 133 | { "hca_lowkey", | ||
| 134 | "Low key used for handling CRI HCA files", OFFSET(keyl), | ||
| 135 | AV_OPT_TYPE_INT64, {.i64=0}, .min = 0, .max = UINT32_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM, }, | ||
| 136 | { "hca_highkey", | ||
| 137 | "High key used for handling CRI HCA files", OFFSET(keyh), | ||
| 138 | AV_OPT_TYPE_INT64, {.i64=0}, .min = 0, .max = UINT32_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM, }, | ||
| 139 | { "hca_subkey", | ||
| 140 | "Subkey used for handling CRI HCA files", OFFSET(subkey), | ||
| 141 | AV_OPT_TYPE_INT, {.i64=0}, .min = 0, .max = UINT16_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM }, | ||
| 142 | { NULL }, | ||
| 143 | }; | ||
| 144 | |||
| 145 | static const AVClass hca_class = { | ||
| 146 | .class_name = "hca", | ||
| 147 | .item_name = av_default_item_name, | ||
| 148 | .option = hca_options, | ||
| 149 | .version = LIBAVUTIL_VERSION_INT, | ||
| 150 | }; | ||
| 151 | |||
| 152 | const FFInputFormat ff_hca_demuxer = { | ||
| 153 | .p.name = "hca", | ||
| 154 | .p.long_name = NULL_IF_CONFIG_SMALL("CRI HCA"), | ||
| 155 | .p.priv_class = &hca_class, | ||
| 156 | .p.extensions = "hca", | ||
| 157 | .p.flags = AVFMT_GENERIC_INDEX, | ||
| 158 | .priv_data_size = sizeof(HCADemuxContext), | ||
| 159 | .read_probe = hca_probe, | ||
| 160 | .read_header = hca_read_header, | ||
| 161 | .read_packet = hca_read_packet, | ||
| 162 | }; | ||
| 163 |