2 * Copyright (C) 2017 foo86
4 * This file is part of FFmpeg.
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.
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.
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
25 static const uint8_t nb_programs_tab[MAX_PROG_CONF + 1] = {
26 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 8, 1, 2, 3, 3, 4, 5, 6, 1, 2, 3, 4, 1, 1
29 static const uint8_t nb_channels_tab[MAX_PROG_CONF + 1] = {
30 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 8, 8
33 static int skip_input(DBEContext *s, int nb_words)
35 if (nb_words > s->input_size) {
36 return AVERROR_INVALIDDATA;
39 s->input += nb_words * s->word_bytes;
40 s->input_size -= nb_words;
44 static int parse_key(DBEContext *s)
47 const uint8_t *key = s->input;
48 int ret = skip_input(s, 1);
51 return AV_RB24(key) >> 24 - s->word_bits;
56 static int convert_input(DBEContext *s, int nb_words, int key)
58 const uint8_t *src = s->input;
59 uint8_t *dst = s->buffer;
63 av_assert0(nb_words <= 1024u);
65 if (nb_words > s->input_size) {
66 return AVERROR_INVALIDDATA;
69 switch (s->word_bits) {
71 for (i = 0; i < nb_words; i++, src += 2, dst += 2)
72 AV_WB16(dst, AV_RB16(src) ^ key);
75 init_put_bits(&pb, s->buffer, sizeof(s->buffer));
76 for (i = 0; i < nb_words; i++, src += 3)
77 put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
81 for (i = 0; i < nb_words; i++, src += 3, dst += 3)
82 AV_WB24(dst, AV_RB24(src) ^ key);
88 return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
91 int ff_dolby_e_parse_init(DBEContext *s, const uint8_t *buf, int buf_size)
96 return AVERROR_INVALIDDATA;
99 if ((hdr & 0xfffffe) == 0x7888e) {
101 } else if ((hdr & 0xffffe0) == 0x788e0) {
103 } else if ((hdr & 0xfffe00) == 0x78e00) {
107 av_log(s->avctx, AV_LOG_ERROR, "Invalid frame header\n");
108 return AVERROR_INVALIDDATA;
111 s->word_bytes = s->word_bits + 7 >> 3;
112 s->input = buf + s->word_bytes;
113 s->input_size = buf_size / s->word_bytes - 1;
114 s->key_present = hdr >> 24 - s->word_bits & 1;
119 int ff_dolby_e_parse_header(DBEContext *s, DolbyEHeaderInfo *hdr)
121 int i, ret, key, mtd_size;
123 if ((key = parse_key(s)) < 0)
125 if ((ret = convert_input(s, 1, key)) < 0)
128 skip_bits(&s->gb, 4);
129 mtd_size = get_bits(&s->gb, 10);
132 av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
133 return AVERROR_INVALIDDATA;
136 if ((ret = convert_input(s, mtd_size, key)) < 0)
139 skip_bits(&s->gb, 14);
140 hdr->prog_conf = get_bits(&s->gb, 6);
141 if (hdr->prog_conf > MAX_PROG_CONF) {
143 av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
144 return AVERROR_INVALIDDATA;
147 hdr->nb_channels = nb_channels_tab[hdr->prog_conf];
148 hdr->nb_programs = nb_programs_tab[hdr->prog_conf];
150 hdr->fr_code = get_bits(&s->gb, 4);
151 hdr->fr_code_orig = get_bits(&s->gb, 4);
152 if (!sample_rate_tab[hdr->fr_code] ||
153 !sample_rate_tab[hdr->fr_code_orig]) {
155 av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
156 return AVERROR_INVALIDDATA;
159 skip_bits_long(&s->gb, 88);
160 for (i = 0; i < hdr->nb_channels; i++)
161 hdr->ch_size[i] = get_bits(&s->gb, 10);
162 hdr->mtd_ext_size = get_bits(&s->gb, 8);
163 hdr->meter_size = get_bits(&s->gb, 8);
165 skip_bits_long(&s->gb, 10 * hdr->nb_programs);
166 for (i = 0; i < hdr->nb_channels; i++) {
167 hdr->rev_id[i] = get_bits(&s->gb, 4);
169 hdr->begin_gain[i] = get_bits(&s->gb, 10);
170 hdr->end_gain[i] = get_bits(&s->gb, 10);
173 if (get_bits_left(&s->gb) < 0) {
175 av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
176 return AVERROR_INVALIDDATA;
179 return skip_input(s, mtd_size + 1);