]> git.sesse.net Git - ffmpeg/blob - libavcodec/hcom.c
avcodec/hcom: Check that there are dictionary entries
[ffmpeg] / libavcodec / hcom.c
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         s->dict_entries == 0)
57         return AVERROR_INVALIDDATA;
58     s->delta_compression = AV_RB32(avctx->extradata + 2);
59     s->sample = s->first_sample = avctx->extradata[avctx->extradata_size - 1];
60
61     s->dict = av_calloc(s->dict_entries, sizeof(*s->dict));
62     if (!s->dict)
63         return AVERROR(ENOMEM);
64     for (int i = 0; i < s->dict_entries; i++) {
65         s->dict[i].l = AV_RB16(avctx->extradata + 6 + 4 * i);
66         s->dict[i].r = AV_RB16(avctx->extradata + 6 + 4 * i + 2);
67         if (s->dict[i].l >= 0 &&
68             (s->dict[i].l >= s->dict_entries ||
69              s->dict[i].r >= s->dict_entries))
70             return AVERROR_INVALIDDATA;
71     }
72
73     avctx->sample_fmt = AV_SAMPLE_FMT_U8;
74     s->dict_entry = 0;
75
76     return 0;
77 }
78
79 static int hcom_decode(AVCodecContext *avctx, void *data,
80                        int *got_frame, AVPacket *pkt)
81 {
82     HCOMContext *s = avctx->priv_data;
83     AVFrame *frame = data;
84     GetBitContext gb;
85     int ret, n = 0;
86
87     if (pkt->size > INT16_MAX)
88         return AVERROR_INVALIDDATA;
89
90     frame->nb_samples = pkt->size * 8;
91     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
92         return ret;
93
94     if ((ret = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
95         return ret;
96
97     while (get_bits_left(&gb) > 0) {
98         if (get_bits1(&gb))
99             s->dict_entry = s->dict[s->dict_entry].r;
100         else
101             s->dict_entry = s->dict[s->dict_entry].l;
102
103         if (s->dict[s->dict_entry].l < 0) {
104             int16_t datum;
105
106             datum = s->dict[s->dict_entry].r;
107
108             if (!s->delta_compression)
109                 s->sample = 0;
110             s->sample = (s->sample + datum) & 0xFF;
111
112             frame->data[0][n++] = s->sample;
113
114             s->dict_entry = 0;
115         }
116     }
117
118     frame->nb_samples = n;
119
120     *got_frame = 1;
121
122     return pkt->size;
123 }
124
125 static av_cold int hcom_close(AVCodecContext *avctx)
126 {
127     HCOMContext *s = avctx->priv_data;
128
129     av_freep(&s->dict);
130
131     return 0;
132 }
133
134 AVCodec ff_hcom_decoder = {
135     .name           = "hcom",
136     .long_name      = NULL_IF_CONFIG_SMALL("HCOM Audio"),
137     .type           = AVMEDIA_TYPE_AUDIO,
138     .id             = AV_CODEC_ID_HCOM,
139     .priv_data_size = sizeof(HCOMContext),
140     .init           = hcom_init,
141     .close          = hcom_close,
142     .decode         = hcom_decode,
143     .capabilities   = AV_CODEC_CAP_DR1,
144 };