4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/intreadwrite.h"
27 typedef struct HEntry {
31 typedef struct HCOMContext {
32 AVCodecContext *avctx;
38 int delta_compression;
43 static av_cold int hcom_init(AVCodecContext *avctx)
45 HCOMContext *s = avctx->priv_data;
47 if (avctx->channels != 1) {
48 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
49 return AVERROR_INVALIDDATA;
52 if (avctx->extradata_size <= 7)
53 return AVERROR_INVALIDDATA;
54 s->dict_entries = AV_RB16(avctx->extradata);
55 if (avctx->extradata_size < s->dict_entries * 4 + 7)
56 return AVERROR_INVALIDDATA;
57 s->delta_compression = AV_RB32(avctx->extradata + 2);
58 s->sample = s->first_sample = avctx->extradata[avctx->extradata_size - 1];
60 s->dict = av_calloc(s->dict_entries, sizeof(*s->dict));
62 return AVERROR(ENOMEM);
63 for (int i = 0; i < s->dict_entries; i++) {
64 s->dict[i].l = AV_RB16(avctx->extradata + 6 + 4 * i);
65 s->dict[i].r = AV_RB16(avctx->extradata + 6 + 4 * i + 2);
66 if (s->dict[i].l >= 0 &&
67 (s->dict[i].l >= s->dict_entries ||
68 s->dict[i].r >= s->dict_entries))
69 return AVERROR_INVALIDDATA;
72 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
78 static int hcom_decode(AVCodecContext *avctx, void *data,
79 int *got_frame, AVPacket *pkt)
81 HCOMContext *s = avctx->priv_data;
82 AVFrame *frame = data;
86 if (pkt->size > INT16_MAX)
87 return AVERROR_INVALIDDATA;
89 frame->nb_samples = pkt->size * 8;
90 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
93 if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
96 while (get_bits_left(&gb) > 0) {
98 s->dict_entry = s->dict[s->dict_entry].r;
100 s->dict_entry = s->dict[s->dict_entry].l;
102 if (s->dict[s->dict_entry].l < 0) {
105 datum = s->dict[s->dict_entry].r;
107 if (!s->delta_compression)
109 s->sample = (s->sample + datum) & 0xFF;
111 frame->data[0][n++] = s->sample;
117 frame->nb_samples = n;
124 static av_cold int hcom_close(AVCodecContext *avctx)
126 HCOMContext *s = avctx->priv_data;
133 AVCodec ff_hcom_decoder = {
135 .long_name = NULL_IF_CONFIG_SMALL("HCOM Audio"),
136 .type = AVMEDIA_TYPE_AUDIO,
137 .id = AV_CODEC_ID_HCOM,
138 .priv_data_size = sizeof(HCOMContext),
141 .decode = hcom_decode,
142 .capabilities = AV_CODEC_CAP_DR1,