FFmpeg
Loading...
Searching...
No Matches
hca.c
Go to the documentation of this file.
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"
25
26#include "avformat.h"
27#include "avio_internal.h"
28#include "demux.h"
29#include "internal.h"
30
31#define HCA_MASK 0x7f7f7f7f
32
39
40static int hca_probe(const AVProbeData *p)
41{
42 if ((AV_RL32(p->buf) & HCA_MASK) != MKTAG('H', 'C', 'A', 0))
43 return 0;
44
45 if ((AV_RL32(p->buf + 8) & HCA_MASK) != MKTAG('f', 'm', 't', 0))
46 return 0;
47
48 return AVPROBE_SCORE_MAX / 3;
49}
50
52{
53 HCADemuxContext *hca = s->priv_data;
56 AVIOContext *pb = s->pb;
57 AVStream *st;
58 uint32_t chunk;
59 uint16_t version;
60 uint32_t block_count;
61 uint16_t block_size, data_offset;
62 int ret;
63
64 avio_skip(pb, 4);
65 version = avio_rb16(pb);
66
67 data_offset = avio_rb16(pb);
68 if (data_offset <= 8)
70
72 if (!st)
73 return AVERROR(ENOMEM);
74
75 par = st->codecpar;
76 ret = ff_alloc_extradata(par, data_offset + 10);
77 if (ret < 0)
78 return ret;
79
80 ret = ffio_read_size(pb, par->extradata + 8, par->extradata_size - 8 - 10);
81 if (ret < 0)
83 AV_WL32(par->extradata, MKTAG('H', 'C', 'A', 0));
84 AV_WB16(par->extradata + 4, version);
85 AV_WB16(par->extradata + 6, data_offset);
86 AV_WB32(par->extradata + par->extradata_size - 10, hca->keyh);
87 AV_WB32(par->extradata + par->extradata_size - 6, hca->keyl);
88 AV_WB16(par->extradata + par->extradata_size - 2, hca->subkey);
89
90 bytestream2_init(&gb, par->extradata + 8, par->extradata_size - 8);
91
92 if ((bytestream2_get_le32(&gb) & HCA_MASK) != MKTAG('f', 'm', 't', 0))
94
97 par->codec_tag = 0;
98 st->codecpar->ch_layout.nb_channels = bytestream2_get_byte(&gb);
99 par->sample_rate = bytestream2_get_be24(&gb);
100 block_count = bytestream2_get_be32(&gb);
101 bytestream2_skip(&gb, 4);
102 chunk = bytestream2_get_le32(&gb) & HCA_MASK;
103 if (chunk == MKTAG('c', 'o', 'm', 'p')) {
104 block_size = bytestream2_get_be16(&gb);
105 } else if (chunk == MKTAG('d', 'e', 'c', 0)) {
106 block_size = bytestream2_get_be16(&gb);
107 } else {
108 return AVERROR_INVALIDDATA;
109 }
110
111 if (block_size < 8)
112 return AVERROR_INVALIDDATA;
113 par->block_align = block_size;
114 st->duration = 1024 * block_count;
115
116 avio_seek(pb, data_offset, SEEK_SET);
117 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
118
119 return 0;
120}
121
123{
124 AVCodecParameters *par = s->streams[0]->codecpar;
125 int ret;
126
127 ret = av_get_packet(s->pb, pkt, par->block_align);
128 pkt->duration = 1024;
129 return ret;
130}
131
132#define OFFSET(x) offsetof(HCADemuxContext, x)
133static const AVOption hca_options[] = {
134 { "hca_lowkey",
135 "Low key used for handling CRI HCA files", OFFSET(keyl),
136 AV_OPT_TYPE_INT64, {.i64=0}, .min = 0, .max = UINT32_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM, },
137 { "hca_highkey",
138 "High key used for handling CRI HCA files", OFFSET(keyh),
139 AV_OPT_TYPE_INT64, {.i64=0}, .min = 0, .max = UINT32_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM, },
140 { "hca_subkey",
141 "Subkey used for handling CRI HCA files", OFFSET(subkey),
142 AV_OPT_TYPE_INT, {.i64=0}, .min = 0, .max = UINT16_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM },
143 { NULL },
144};
145
146static const AVClass hca_class = {
147 .class_name = "hca",
148 .item_name = av_default_item_name,
149 .option = hca_options,
150 .version = LIBAVUTIL_VERSION_INT,
151};
152
154 .p.name = "hca",
155 .p.long_name = NULL_IF_CONFIG_SMALL("CRI HCA"),
156 .p.priv_class = &hca_class,
157 .p.extensions = "hca",
158 .p.flags = AVFMT_GENERIC_INDEX,
159 .priv_data_size = sizeof(HCADemuxContext),
163};
const FFInputFormat ff_hca_demuxer
Definition hca.c:153
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_HCA
Definition codec_id.h:546
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
static int hca_probe(const AVProbeData *p)
Definition hca.c:40
static int hca_read_header(AVFormatContext *s)
Definition hca.c:51
static int hca_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition hca.c:122
static const AVClass hca_class
Definition hca.c:146
#define OFFSET(x)
Definition hca.c:132
static const AVOption hca_options[]
Definition hca.c:133
#define HCA_MASK
Definition hcadec.c:32
#define AV_WB32(p, v)
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_WB16(p, v)
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
version
Definition libkvazaar.c:313
#define MKTAG(a, b, c, d)
Definition macros.h:55
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t keyl
Definition hca.c:35
int subkey
Definition hca.c:37
int64_t keyh
Definition hca.c:36