2019-01-02 17:50:38 +00:00
|
|
|
/*
|
|
|
|
* HCOM audio decoder
|
|
|
|
*
|
|
|
|
* 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"
|
2024-03-25 00:30:37 +00:00
|
|
|
#include "libavutil/mem.h"
|
2019-01-02 17:50:38 +00:00
|
|
|
|
|
|
|
#include "avcodec.h"
|
2022-03-16 17:18:28 +00:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 19:28:16 +00:00
|
|
|
#include "decode.h"
|
2019-01-02 17:50:38 +00:00
|
|
|
#include "get_bits.h"
|
|
|
|
|
|
|
|
typedef struct HEntry {
|
|
|
|
int16_t l, r;
|
|
|
|
} HEntry;
|
|
|
|
|
|
|
|
typedef struct HCOMContext {
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
|
|
|
|
uint8_t first_sample;
|
|
|
|
uint8_t sample;
|
|
|
|
int dict_entries;
|
|
|
|
int dict_entry;
|
|
|
|
int delta_compression;
|
|
|
|
|
|
|
|
HEntry *dict;
|
|
|
|
} HCOMContext;
|
|
|
|
|
|
|
|
static av_cold int hcom_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
HCOMContext *s = avctx->priv_data;
|
|
|
|
|
2019-05-29 07:36:27 +00:00
|
|
|
if (avctx->ch_layout.nb_channels != 1) {
|
2019-01-02 17:50:38 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avctx->extradata_size <= 7)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
s->dict_entries = AV_RB16(avctx->extradata);
|
2019-09-25 16:38:03 +00:00
|
|
|
if (avctx->extradata_size < s->dict_entries * 4 + 7 ||
|
|
|
|
s->dict_entries == 0)
|
2019-01-02 17:50:38 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
s->delta_compression = AV_RB32(avctx->extradata + 2);
|
|
|
|
s->sample = s->first_sample = avctx->extradata[avctx->extradata_size - 1];
|
|
|
|
|
|
|
|
s->dict = av_calloc(s->dict_entries, sizeof(*s->dict));
|
|
|
|
if (!s->dict)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (int i = 0; i < s->dict_entries; i++) {
|
|
|
|
s->dict[i].l = AV_RB16(avctx->extradata + 6 + 4 * i);
|
|
|
|
s->dict[i].r = AV_RB16(avctx->extradata + 6 + 4 * i + 2);
|
2019-03-03 19:21:20 +00:00
|
|
|
if (s->dict[i].l >= 0 &&
|
|
|
|
(s->dict[i].l >= s->dict_entries ||
|
2019-10-07 14:56:11 +00:00
|
|
|
s->dict[i].r >= s->dict_entries ||
|
2020-09-14 15:39:55 +00:00
|
|
|
s->dict[i].r < 0 ))
|
2019-03-03 19:21:20 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2019-10-07 14:56:11 +00:00
|
|
|
}
|
2020-09-14 15:39:55 +00:00
|
|
|
if (s->dict[0].l < 0)
|
2019-10-07 14:56:11 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2019-01-02 17:50:38 +00:00
|
|
|
|
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_U8;
|
|
|
|
s->dict_entry = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:33:24 +00:00
|
|
|
static int hcom_decode(AVCodecContext *avctx, AVFrame *frame,
|
2019-01-02 17:50:38 +00:00
|
|
|
int *got_frame, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
HCOMContext *s = avctx->priv_data;
|
|
|
|
GetBitContext gb;
|
|
|
|
int ret, n = 0;
|
|
|
|
|
|
|
|
if (pkt->size > INT16_MAX)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
frame->nb_samples = pkt->size * 8;
|
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
while (get_bits_left(&gb) > 0) {
|
2019-01-15 10:34:11 +00:00
|
|
|
if (get_bits1(&gb))
|
2019-01-02 17:50:38 +00:00
|
|
|
s->dict_entry = s->dict[s->dict_entry].r;
|
2019-01-15 10:34:11 +00:00
|
|
|
else
|
2019-01-02 17:50:38 +00:00
|
|
|
s->dict_entry = s->dict[s->dict_entry].l;
|
|
|
|
|
|
|
|
if (s->dict[s->dict_entry].l < 0) {
|
|
|
|
int16_t datum;
|
|
|
|
|
|
|
|
datum = s->dict[s->dict_entry].r;
|
|
|
|
|
|
|
|
if (!s->delta_compression)
|
|
|
|
s->sample = 0;
|
|
|
|
s->sample = (s->sample + datum) & 0xFF;
|
|
|
|
|
|
|
|
frame->data[0][n++] = s->sample;
|
|
|
|
|
|
|
|
s->dict_entry = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->nb_samples = n;
|
|
|
|
|
|
|
|
*got_frame = 1;
|
|
|
|
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int hcom_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
HCOMContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
av_freep(&s->dict);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:09:54 +00:00
|
|
|
const FFCodec ff_hcom_decoder = {
|
|
|
|
.p.name = "hcom",
|
2022-08-29 11:38:02 +00:00
|
|
|
CODEC_LONG_NAME("HCOM Audio"),
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.p.id = AV_CODEC_ID_HCOM,
|
2019-01-02 17:50:38 +00:00
|
|
|
.priv_data_size = sizeof(HCOMContext),
|
|
|
|
.init = hcom_init,
|
|
|
|
.close = hcom_close,
|
2022-03-30 21:28:24 +00:00
|
|
|
FF_CODEC_DECODE_CB(hcom_decode),
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2022-07-09 22:05:45 +00:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2019-01-02 17:50:38 +00:00
|
|
|
};
|