FFmpeg
Loading...
Searching...
No Matches
hcom.c
Go to the documentation of this file.
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
22#include "libavutil/mem.h"
23
24#include "avcodec.h"
25#include "codec_internal.h"
26#include "decode.h"
27#include "get_bits.h"
28
29typedef struct HEntry {
30 int16_t l, r;
31} HEntry;
32
44
46{
47 HCOMContext *s = avctx->priv_data;
48
49 if (avctx->ch_layout.nb_channels != 1) {
50 av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
52 }
53
54 if (avctx->extradata_size <= 7)
56 s->dict_entries = AV_RB16(avctx->extradata);
57 if (avctx->extradata_size < s->dict_entries * 4 + 7 ||
58 s->dict_entries == 0)
60 s->delta_compression = AV_RB32(avctx->extradata + 2);
61 s->sample = s->first_sample = avctx->extradata[avctx->extradata_size - 1];
62
63 s->dict = av_calloc(s->dict_entries, sizeof(*s->dict));
64 if (!s->dict)
65 return AVERROR(ENOMEM);
66 for (int i = 0; i < s->dict_entries; i++) {
67 s->dict[i].l = AV_RB16(avctx->extradata + 6 + 4 * i);
68 s->dict[i].r = AV_RB16(avctx->extradata + 6 + 4 * i + 2);
69 if (s->dict[i].l >= 0 &&
70 (s->dict[i].l >= s->dict_entries ||
71 s->dict[i].r >= s->dict_entries ||
72 s->dict[i].r < 0 ))
74 }
75 if (s->dict[0].l < 0)
77
79 s->dict_entry = 0;
80
81 return 0;
82}
83
85 int *got_frame, AVPacket *pkt)
86{
87 HCOMContext *s = avctx->priv_data;
89 int ret, n = 0;
90
91 if (pkt->size > INT16_MAX)
93
94 frame->nb_samples = pkt->size * 8;
95 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
96 return ret;
97
98 if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
99 return ret;
100
101 while (get_bits_left(&gb) > 0) {
102 if (get_bits1(&gb))
103 s->dict_entry = s->dict[s->dict_entry].r;
104 else
105 s->dict_entry = s->dict[s->dict_entry].l;
106
107 if (s->dict[s->dict_entry].l < 0) {
108 int16_t datum;
109
110 datum = s->dict[s->dict_entry].r;
111
112 if (!s->delta_compression)
113 s->sample = 0;
114 s->sample = (s->sample + datum) & 0xFF;
115
116 frame->data[0][n++] = s->sample;
117
118 s->dict_entry = 0;
119 }
120 }
121
122 frame->nb_samples = n;
123
124 *got_frame = 1;
125
126 return pkt->size;
127}
128
130{
131 HCOMContext *s = avctx->priv_data;
132
133 av_freep(&s->dict);
134
135 return 0;
136}
137
139 .p.name = "hcom",
140 CODEC_LONG_NAME("HCOM Audio"),
141 .p.type = AVMEDIA_TYPE_AUDIO,
142 .p.id = AV_CODEC_ID_HCOM,
143 .priv_data_size = sizeof(HCOMContext),
144 .init = hcom_init,
145 .close = hcom_close,
147 .p.capabilities = AV_CODEC_CAP_DR1,
148 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
149};
const FFCodec ff_hcom_decoder
Definition hcom.c:138
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_HCOM
Definition codec_id.h:542
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition samplefmt.h:57
#define AV_RB32(p)
#define AV_RB16(p)
static av_cold int hcom_init(AVCodecContext *avctx)
Definition hcom.c:45
static int hcom_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition hcom.c:84
static av_cold int hcom_close(AVCodecContext *avctx)
Definition hcom.c:129
#define av_cold
Definition attributes.h:117
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int delta_compression
Definition hcom.c:40
uint8_t sample
Definition hcom.c:37
HEntry * dict
Definition hcom.c:42
AVCodecContext * avctx
Definition hcom.c:34
uint8_t first_sample
Definition hcom.c:36
int dict_entry
Definition hcom.c:39
int dict_entries
Definition hcom.c:38
Definition hcom.c:29
int16_t r
Definition hcom.c:30
int16_t l
Definition hcom.c:30
#define av_freep(p)
#define av_log(a,...)