]> git.sesse.net Git - ffmpeg/blob - libavcodec/dolby_e_parse.c
avcodec/dolby_e: Avoid code duplication when converting input
[ffmpeg] / libavcodec / dolby_e_parse.c
1 /*
2  * Copyright (C) 2017 foo86
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 "get_bits.h"
22 #include "put_bits.h"
23 #include "dolby_e.h"
24
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
27 };
28
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
31 };
32
33 static int skip_input(DBEContext *s, int nb_words)
34 {
35     if (nb_words > s->input_size) {
36         return AVERROR_INVALIDDATA;
37     }
38
39     s->input      += nb_words * s->word_bytes;
40     s->input_size -= nb_words;
41     return 0;
42 }
43
44 static int parse_key(DBEContext *s)
45 {
46     if (s->key_present) {
47         const uint8_t *key = s->input;
48         int      ret = skip_input(s, 1);
49         if (ret < 0)
50             return ret;
51         return AV_RB24(key) >> 24 - s->word_bits;
52     }
53     return 0;
54 }
55
56 int ff_dolby_e_convert_input(DBEContext *s, int nb_words, int key)
57 {
58     const uint8_t *src = s->input;
59     uint8_t *dst = s->buffer;
60     PutBitContext pb;
61     int i;
62
63     av_assert0(nb_words <= 1024u);
64
65     if (nb_words > s->input_size) {
66         if (s->avctx)
67             av_log(s->avctx, AV_LOG_ERROR, "Packet too short\n");
68         return AVERROR_INVALIDDATA;
69     }
70
71     switch (s->word_bits) {
72     case 16:
73         for (i = 0; i < nb_words; i++, src += 2, dst += 2)
74             AV_WB16(dst, AV_RB16(src) ^ key);
75         break;
76     case 20:
77         init_put_bits(&pb, s->buffer, sizeof(s->buffer));
78         for (i = 0; i < nb_words; i++, src += 3)
79             put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
80         flush_put_bits(&pb);
81         break;
82     case 24:
83         for (i = 0; i < nb_words; i++, src += 3, dst += 3)
84             AV_WB24(dst, AV_RB24(src) ^ key);
85         break;
86     default:
87         av_assert0(0);
88     }
89
90     return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
91 }
92
93 int ff_dolby_e_parse_header(DBEContext *s, const uint8_t *buf, int buf_size)
94 {
95     DolbyEHeaderInfo *const header = &s->metadata;
96     int hdr, ret, key, mtd_size;
97
98     if (buf_size < 3)
99         return AVERROR_INVALIDDATA;
100
101     hdr = AV_RB24(buf);
102     if ((hdr & 0xfffffe) == 0x7888e) {
103         s->word_bits = 24;
104     } else if ((hdr & 0xffffe0) == 0x788e0) {
105         s->word_bits = 20;
106     } else if ((hdr & 0xfffe00) == 0x78e00) {
107         s->word_bits = 16;
108     } else {
109         if (s->avctx)
110             av_log(s->avctx, AV_LOG_ERROR, "Invalid frame header\n");
111         return AVERROR_INVALIDDATA;
112     }
113
114     s->word_bytes  = s->word_bits + 7 >> 3;
115     s->input       = buf + s->word_bytes;
116     s->input_size  = buf_size / s->word_bytes - 1;
117     s->key_present = hdr >> 24 - s->word_bits & 1;
118
119     if ((key = parse_key(s)) < 0)
120         return key;
121     if ((ret = ff_dolby_e_convert_input(s, 1, key)) < 0)
122         return ret;
123
124     skip_bits(&s->gb, 4);
125     mtd_size = get_bits(&s->gb, 10);
126     if (!mtd_size) {
127         if (s->avctx)
128             av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
129         return AVERROR_INVALIDDATA;
130     }
131
132     if ((ret = ff_dolby_e_convert_input(s, mtd_size, key)) < 0)
133         return ret;
134
135     skip_bits(&s->gb, 14);
136     header->prog_conf = get_bits(&s->gb, 6);
137     if (header->prog_conf > MAX_PROG_CONF) {
138         if (s->avctx)
139             av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
140         return AVERROR_INVALIDDATA;
141     }
142
143     header->nb_channels = nb_channels_tab[header->prog_conf];
144     header->nb_programs = nb_programs_tab[header->prog_conf];
145
146     header->fr_code      = get_bits(&s->gb, 4);
147     header->fr_code_orig = get_bits(&s->gb, 4);
148     if (!sample_rate_tab[header->fr_code] ||
149         !sample_rate_tab[header->fr_code_orig]) {
150         if (s->avctx)
151             av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
152         return AVERROR_INVALIDDATA;
153     }
154
155     skip_bits_long(&s->gb, 88);
156     for (int i = 0; i < header->nb_channels; i++)
157         header->ch_size[i] = get_bits(&s->gb, 10);
158     header->mtd_ext_size = get_bits(&s->gb, 8);
159     header->meter_size   = get_bits(&s->gb, 8);
160
161     skip_bits_long(&s->gb, 10 * header->nb_programs);
162     for (int i = 0; i < header->nb_channels; i++) {
163         header->rev_id[i]     = get_bits(&s->gb,  4);
164         skip_bits1(&s->gb);
165         header->begin_gain[i] = get_bits(&s->gb, 10);
166         header->end_gain[i]   = get_bits(&s->gb, 10);
167     }
168
169     if (get_bits_left(&s->gb) < 0) {
170         if (s->avctx)
171             av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
172         return AVERROR_INVALIDDATA;
173     }
174
175     return skip_input(s, mtd_size + 1);
176 }