FFmpeg
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 
21 #include "libavutil/intreadwrite.h"
22 
23 #include "avcodec.h"
24 #include "codec_internal.h"
25 #include "get_bits.h"
26 #include "internal.h"
27 
28 typedef struct HEntry {
29  int16_t l, r;
30 } HEntry;
31 
32 typedef struct HCOMContext {
34 
35  uint8_t first_sample;
36  uint8_t sample;
40 
42 } HCOMContext;
43 
44 static av_cold int hcom_init(AVCodecContext *avctx)
45 {
46  HCOMContext *s = avctx->priv_data;
47 
48  if (avctx->ch_layout.nb_channels != 1) {
49  av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
50  return AVERROR_INVALIDDATA;
51  }
52 
53  if (avctx->extradata_size <= 7)
54  return AVERROR_INVALIDDATA;
55  s->dict_entries = AV_RB16(avctx->extradata);
56  if (avctx->extradata_size < s->dict_entries * 4 + 7 ||
57  s->dict_entries == 0)
58  return AVERROR_INVALIDDATA;
59  s->delta_compression = AV_RB32(avctx->extradata + 2);
60  s->sample = s->first_sample = avctx->extradata[avctx->extradata_size - 1];
61 
62  s->dict = av_calloc(s->dict_entries, sizeof(*s->dict));
63  if (!s->dict)
64  return AVERROR(ENOMEM);
65  for (int i = 0; i < s->dict_entries; i++) {
66  s->dict[i].l = AV_RB16(avctx->extradata + 6 + 4 * i);
67  s->dict[i].r = AV_RB16(avctx->extradata + 6 + 4 * i + 2);
68  if (s->dict[i].l >= 0 &&
69  (s->dict[i].l >= s->dict_entries ||
70  s->dict[i].r >= s->dict_entries ||
71  s->dict[i].r < 0 ))
72  return AVERROR_INVALIDDATA;
73  }
74  if (s->dict[0].l < 0)
75  return AVERROR_INVALIDDATA;
76 
78  s->dict_entry = 0;
79 
80  return 0;
81 }
82 
84  int *got_frame, AVPacket *pkt)
85 {
86  HCOMContext *s = avctx->priv_data;
87  GetBitContext gb;
88  int ret, n = 0;
89 
90  if (pkt->size > INT16_MAX)
91  return AVERROR_INVALIDDATA;
92 
93  frame->nb_samples = pkt->size * 8;
94  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
95  return ret;
96 
97  if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
98  return ret;
99 
100  while (get_bits_left(&gb) > 0) {
101  if (get_bits1(&gb))
102  s->dict_entry = s->dict[s->dict_entry].r;
103  else
104  s->dict_entry = s->dict[s->dict_entry].l;
105 
106  if (s->dict[s->dict_entry].l < 0) {
107  int16_t datum;
108 
109  datum = s->dict[s->dict_entry].r;
110 
111  if (!s->delta_compression)
112  s->sample = 0;
113  s->sample = (s->sample + datum) & 0xFF;
114 
115  frame->data[0][n++] = s->sample;
116 
117  s->dict_entry = 0;
118  }
119  }
120 
121  frame->nb_samples = n;
122 
123  *got_frame = 1;
124 
125  return pkt->size;
126 }
127 
129 {
130  HCOMContext *s = avctx->priv_data;
131 
132  av_freep(&s->dict);
133 
134  return 0;
135 }
136 
138  .p.name = "hcom",
139  .p.long_name = NULL_IF_CONFIG_SMALL("HCOM Audio"),
140  .p.type = AVMEDIA_TYPE_AUDIO,
141  .p.id = AV_CODEC_ID_HCOM,
142  .priv_data_size = sizeof(HCOMContext),
143  .init = hcom_init,
144  .close = hcom_close,
146  .p.capabilities = AV_CODEC_CAP_DR1,
148 };
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:39
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_CODEC_ID_HCOM
@ AV_CODEC_ID_HCOM
Definition: codec_id.h:516
HCOMContext::avctx
AVCodecContext * avctx
Definition: hcom.c:33
HEntry
Definition: hcom.c:28
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:112
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
HCOMContext
Definition: hcom.c:32
init
static int init
Definition: av_tx.c:47
hcom_init
static av_cold int hcom_init(AVCodecContext *avctx)
Definition: hcom.c:44
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2056
GetBitContext
Definition: get_bits.h:61
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:667
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:491
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
HCOMContext::dict
HEntry * dict
Definition: hcom.c:41
get_bits.h
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
HEntry::l
int16_t l
Definition: hcom.c:29
hcom_decode
static int hcom_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: hcom.c:83
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1403
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
hcom_close
static av_cold int hcom_close(AVCodecContext *avctx)
Definition: hcom.c:128
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
codec_internal.h
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1014
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
HEntry::r
int16_t r
Definition: hcom.c:29
ff_hcom_decoder
const FFCodec ff_hcom_decoder
Definition: hcom.c:137
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:490
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:272
HCOMContext::dict_entry
int dict_entry
Definition: hcom.c:38
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
HCOMContext::first_sample
uint8_t first_sample
Definition: hcom.c:35
HCOMContext::dict_entries
int dict_entries
Definition: hcom.c:37
AVCodecContext
main external API structure.
Definition: avcodec.h:389
HCOMContext::delta_compression
int delta_compression
Definition: hcom.c:39
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
HCOMContext::sample
uint8_t sample
Definition: hcom.c:36
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98