History

avcodec: add HCOM decoder

Paul B Mahol committed 7 years ago

Commit 894cbcd1 parent 301cee66 files · +152 −2Browse files

Unified · Split

6 files
  1. libavcodec/Makefile +1 −0
  2. libavcodec/allcodecs.c +1 −0
  3. libavcodec/avcodec.h +1 −0
  4. libavcodec/codec_desc.c +7 −0
  5. libavcodec/hcom.c +140 −0
  6. libavcodec/version.h +2 −2
libavcodec/Makefile+1−0
@@ -362,6 +362,7 @@ OBJS-$(CONFIG_H264_V4L2M2M_DECODER) += v4l2_m2m_dec.o · 361 unmodified lines
362362 OBJS-$(CONFIG_H264_V4L2M2M_ENCODER) += v4l2_m2m_enc.o
363363 OBJS-$(CONFIG_HAP_DECODER) += hapdec.o hap.o
364364 OBJS-$(CONFIG_HAP_ENCODER) += hapenc.o hap.o
365+OBJS-$(CONFIG_HCOM_DECODER) += hcom.o
365366 OBJS-$(CONFIG_HEVC_DECODER) += hevcdec.o hevc_mvs.o \
366367 hevc_cabac.o hevc_refs.o hevcpred.o \
367368 hevcdsp.o hevc_filter.o hevc_data.o
libavcodec/allcodecs.c+1−0
@@ -424,6 +424,7 @@ extern AVCodec ff_g723_1_decoder; · 423 unmodified lines
424424 extern AVCodec ff_g729_decoder;
425425 extern AVCodec ff_gsm_decoder;
426426 extern AVCodec ff_gsm_ms_decoder;
427+extern AVCodec ff_hcom_decoder;
427428 extern AVCodec ff_iac_decoder;
428429 extern AVCodec ff_ilbc_decoder;
429430 extern AVCodec ff_imc_decoder;
libavcodec/avcodec.h+1−0
@@ -646,6 +646,7 @@ enum AVCodecID { · 645 unmodified lines
646646 AV_CODEC_ID_APTX_HD,
647647 AV_CODEC_ID_SBC,
648648 AV_CODEC_ID_ATRAC9,
649+ AV_CODEC_ID_HCOM,
649650
650651 /* subtitle codecs */
651652 AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs.
libavcodec/codec_desc.c+7−0
@@ -2943,6 +2943,13 @@ static const AVCodecDescriptor codec_descriptors[] = { · 2942 unmodified lines
29432943 .long_name = NULL_IF_CONFIG_SMALL("ATRAC9 (Adaptive TRansform Acoustic Coding 9)"),
29442944 .props = AV_CODEC_PROP_LOSSY,
29452945 },
2946+ {
2947+ .id = AV_CODEC_ID_HCOM,
2948+ .type = AVMEDIA_TYPE_AUDIO,
2949+ .name = "hcom",
2950+ .long_name = NULL_IF_CONFIG_SMALL("HCOM Audio"),
2951+ .props = AV_CODEC_PROP_LOSSY,
2952+ },
29462953
29472954 /* subtitle codecs */
29482955 {
libavcodec/hcom.c+140−0
@@ -0,0 +1,140 @@
1+/*
2+ * HCOM audio decoder
3+ *
4+ * This file is part of FFmpeg.
5+ *
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.
10+ *
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.
15+ *
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
19+ */
20+
21+#include "libavutil/intreadwrite.h"
22+
23+#include "avcodec.h"
24+#include "get_bits.h"
25+#include "internal.h"
26+
27+typedef struct HEntry {
28+ int16_t l, r;
29+} HEntry;
30+
31+typedef struct HCOMContext {
32+ AVCodecContext *avctx;
33+
34+ uint8_t first_sample;
35+ uint8_t sample;
36+ int dict_entries;
37+ int dict_entry;
38+ int delta_compression;
39+
40+ HEntry *dict;
41+} HCOMContext;
42+
43+static av_cold int hcom_init(AVCodecContext *avctx)
44+{
45+ HCOMContext *s = avctx->priv_data;
46+
47+ if (avctx->channels != 1) {
48+ av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
49+ return AVERROR_INVALIDDATA;
50+ }
51+
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];
59+
60+ s->dict = av_calloc(s->dict_entries, sizeof(*s->dict));
61+ if (!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+ }
67+
68+ avctx->sample_fmt = AV_SAMPLE_FMT_U8;
69+ s->dict_entry = 0;
70+
71+ return 0;
72+}
73+
74+static int hcom_decode(AVCodecContext *avctx, void *data,
75+ int *got_frame, AVPacket *pkt)
76+{
77+ HCOMContext *s = avctx->priv_data;
78+ AVFrame *frame = data;
79+ GetBitContext gb;
80+ int ret, n = 0;
81+
82+ if (pkt->size > INT16_MAX)
83+ return AVERROR_INVALIDDATA;
84+
85+ frame->nb_samples = pkt->size * 8;
86+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
87+ return ret;
88+
89+ if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
90+ return ret;
91+
92+ while (get_bits_left(&gb) > 0) {
93+ if (get_bits1(&gb)) {
94+ s->dict_entry = s->dict[s->dict_entry].r;
95+ } else {
96+ s->dict_entry = s->dict[s->dict_entry].l;
97+ }
98+
99+ if (s->dict[s->dict_entry].l < 0) {
100+ int16_t datum;
101+
102+ datum = s->dict[s->dict_entry].r;
103+
104+ if (!s->delta_compression)
105+ s->sample = 0;
106+ s->sample = (s->sample + datum) & 0xFF;
107+
108+ frame->data[0][n++] = s->sample;
109+
110+ s->dict_entry = 0;
111+ }
112+ }
113+
114+ frame->nb_samples = n;
115+
116+ *got_frame = 1;
117+
118+ return pkt->size;
119+}
120+
121+static av_cold int hcom_close(AVCodecContext *avctx)
122+{
123+ HCOMContext *s = avctx->priv_data;
124+
125+ av_freep(&s->dict);
126+
127+ return 0;
128+}
129+
130+AVCodec ff_hcom_decoder = {
131+ .name = "hcom",
132+ .long_name = NULL_IF_CONFIG_SMALL("HCOM Audio"),
133+ .type = AVMEDIA_TYPE_AUDIO,
134+ .id = AV_CODEC_ID_HCOM,
135+ .priv_data_size = sizeof(HCOMContext),
136+ .init = hcom_init,
137+ .close = hcom_close,
138+ .decode = hcom_decode,
139+ .capabilities = AV_CODEC_CAP_DR1,
140+};
libavcodec/version.h+2−2
@@ -28,8 +28,8 @@ · 27 unmodified lines
2828 #include "libavutil/version.h"
2929
3030 #define LIBAVCODEC_VERSION_MAJOR 58
31−#define LIBAVCODEC_VERSION_MINOR 43
32−#define LIBAVCODEC_VERSION_MICRO 101
31+#define LIBAVCODEC_VERSION_MINOR 44
32+#define LIBAVCODEC_VERSION_MICRO 100
3333
3434 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
3535 LIBAVCODEC_VERSION_MINOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t