FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hcom.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 0 57 0.0%
Functions: 0 3 0.0%
Branches: 0 36 0.0%

Line Branch Exec Source
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 #include "libavutil/mem.h"
23
24 #include "avcodec.h"
25 #include "codec_internal.h"
26 #include "decode.h"
27 #include "get_bits.h"
28
29 typedef struct HEntry {
30 int16_t l, r;
31 } HEntry;
32
33 typedef struct HCOMContext {
34 AVCodecContext *avctx;
35
36 uint8_t first_sample;
37 uint8_t sample;
38 int dict_entries;
39 int dict_entry;
40 int delta_compression;
41
42 HEntry *dict;
43 } HCOMContext;
44
45 static av_cold int hcom_init(AVCodecContext *avctx)
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");
51 return AVERROR_INVALIDDATA;
52 }
53
54 if (avctx->extradata_size <= 7)
55 return AVERROR_INVALIDDATA;
56 s->dict_entries = AV_RB16(avctx->extradata);
57 if (avctx->extradata_size < s->dict_entries * 4 + 7 ||
58 s->dict_entries == 0)
59 return AVERROR_INVALIDDATA;
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 ))
73 return AVERROR_INVALIDDATA;
74 }
75 if (s->dict[0].l < 0)
76 return AVERROR_INVALIDDATA;
77
78 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
79 s->dict_entry = 0;
80
81 return 0;
82 }
83
84 static int hcom_decode(AVCodecContext *avctx, AVFrame *frame,
85 int *got_frame, AVPacket *pkt)
86 {
87 HCOMContext *s = avctx->priv_data;
88 GetBitContext gb;
89 int ret, n = 0;
90
91 if (pkt->size > INT16_MAX)
92 return AVERROR_INVALIDDATA;
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
129 static av_cold int hcom_close(AVCodecContext *avctx)
130 {
131 HCOMContext *s = avctx->priv_data;
132
133 av_freep(&s->dict);
134
135 return 0;
136 }
137
138 const FFCodec ff_hcom_decoder = {
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,
146 FF_CODEC_DECODE_CB(hcom_decode),
147 .p.capabilities = AV_CODEC_CAP_DR1,
148 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
149 };
150