]> git.sesse.net Git - ffmpeg/blob - libavcodec/dolby_e_parser.c
avcodec/dolby_e: Split decoder/parser files
[ffmpeg] / libavcodec / dolby_e_parser.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 "parser.h"
22
23 #if CONFIG_DOLBY_E_PARSER | CONFIG_DOLBY_E_DECODER
24
25 #include "get_bits.h"
26 #include "put_bits.h"
27 #include "dolby_e_parser.h"
28 #include "dolby_e_parser_internal.h"
29
30 static int skip_input(DBEContext *s, int nb_words)
31 {
32     if (nb_words > s->input_size) {
33         return AVERROR_INVALIDDATA;
34     }
35
36     s->input      += nb_words * s->word_bytes;
37     s->input_size -= nb_words;
38     return 0;
39 }
40
41 static int parse_key(DBEContext *s)
42 {
43     if (s->key_present) {
44         const uint8_t *key = s->input;
45         int      ret = skip_input(s, 1);
46         if (ret < 0)
47             return ret;
48         return AV_RB24(key) >> 24 - s->word_bits;
49     }
50     return 0;
51 }
52
53 static int convert_input(DBEContext *s, int nb_words, int key)
54 {
55     const uint8_t *src = s->input;
56     uint8_t *dst = s->buffer;
57     PutBitContext pb;
58     int i;
59
60     av_assert0(nb_words <= 1024u);
61
62     if (nb_words > s->input_size) {
63         return AVERROR_INVALIDDATA;
64     }
65
66     switch (s->word_bits) {
67     case 16:
68         for (i = 0; i < nb_words; i++, src += 2, dst += 2)
69             AV_WB16(dst, AV_RB16(src) ^ key);
70         break;
71     case 20:
72         init_put_bits(&pb, s->buffer, sizeof(s->buffer));
73         for (i = 0; i < nb_words; i++, src += 3)
74             put_bits(&pb, 20, AV_RB24(src) >> 4 ^ key);
75         flush_put_bits(&pb);
76         break;
77     case 24:
78         for (i = 0; i < nb_words; i++, src += 3, dst += 3)
79             AV_WB24(dst, AV_RB24(src) ^ key);
80         break;
81     default:
82         av_assert0(0);
83     }
84
85     return init_get_bits(&s->gb, s->buffer, nb_words * s->word_bits);
86 }
87
88 int ff_dolby_e_parse_init(DBEContext *s, const uint8_t *buf, int buf_size)
89 {
90     int hdr;
91
92     if (buf_size < 3)
93         return AVERROR_INVALIDDATA;
94
95     hdr = AV_RB24(buf);
96     if ((hdr & 0xfffffe) == 0x7888e) {
97         s->word_bits = 24;
98     } else if ((hdr & 0xffffe0) == 0x788e0) {
99         s->word_bits = 20;
100     } else if ((hdr & 0xfffe00) == 0x78e00) {
101         s->word_bits = 16;
102     } else {
103         if (s->avctx)
104             av_log(s->avctx, AV_LOG_ERROR, "Invalid frame header\n");
105         return AVERROR_INVALIDDATA;
106     }
107
108     s->word_bytes  = s->word_bits + 7 >> 3;
109     s->input       = buf + s->word_bytes;
110     s->input_size  = buf_size / s->word_bytes - 1;
111     s->key_present = hdr >> 24 - s->word_bits & 1;
112
113     return 0;
114 }
115
116 int ff_dolby_e_parse_header(DBEContext *s, DolbyEHeaderInfo *hdr)
117 {
118     int i, ret, key, mtd_size;
119
120     if ((key = parse_key(s)) < 0)
121         return key;
122     if ((ret = convert_input(s, 1, key)) < 0)
123         return ret;
124
125     skip_bits(&s->gb, 4);
126     mtd_size = get_bits(&s->gb, 10);
127     if (!mtd_size) {
128         if (s->avctx)
129             av_log(s->avctx, AV_LOG_ERROR, "Invalid metadata size\n");
130         return AVERROR_INVALIDDATA;
131     }
132
133     if ((ret = convert_input(s, mtd_size, key)) < 0)
134         return ret;
135
136     skip_bits(&s->gb, 14);
137     hdr->prog_conf = get_bits(&s->gb, 6);
138     if (hdr->prog_conf > MAX_PROG_CONF) {
139         if (s->avctx)
140             av_log(s->avctx, AV_LOG_ERROR, "Invalid program configuration\n");
141         return AVERROR_INVALIDDATA;
142     }
143
144     hdr->nb_channels = nb_channels_tab[hdr->prog_conf];
145     hdr->nb_programs = nb_programs_tab[hdr->prog_conf];
146
147     hdr->fr_code      = get_bits(&s->gb, 4);
148     hdr->fr_code_orig = get_bits(&s->gb, 4);
149     if (!sample_rate_tab[hdr->fr_code] ||
150         !sample_rate_tab[hdr->fr_code_orig]) {
151         if (s->avctx)
152             av_log(s->avctx, AV_LOG_ERROR, "Invalid frame rate code\n");
153         return AVERROR_INVALIDDATA;
154     }
155
156     skip_bits_long(&s->gb, 88);
157     for (i = 0; i < hdr->nb_channels; i++)
158         hdr->ch_size[i] = get_bits(&s->gb, 10);
159     hdr->mtd_ext_size = get_bits(&s->gb, 8);
160     hdr->meter_size   = get_bits(&s->gb, 8);
161
162     skip_bits_long(&s->gb, 10 * hdr->nb_programs);
163     for (i = 0; i < hdr->nb_channels; i++) {
164         hdr->rev_id[i]     = get_bits(&s->gb,  4);
165         skip_bits1(&s->gb);
166         hdr->begin_gain[i] = get_bits(&s->gb, 10);
167         hdr->end_gain[i]   = get_bits(&s->gb, 10);
168     }
169
170     if (get_bits_left(&s->gb) < 0) {
171         if (s->avctx)
172             av_log(s->avctx, AV_LOG_ERROR, "Read past end of metadata\n");
173         return AVERROR_INVALIDDATA;
174     }
175
176     return skip_input(s, mtd_size + 1);
177 }
178 #endif  /* CONFIG_DOLBY_E_PARSER | CONFIG_DOLBY_E_DECODER */
179
180 #if CONFIG_DOLBY_E_PARSER
181
182 static int dolby_e_parse(AVCodecParserContext *s2, AVCodecContext *avctx,
183                         const uint8_t **poutbuf, int *poutbuf_size,
184                         const uint8_t *buf, int buf_size)
185 {
186     DBEParseContext *s1 = s2->priv_data;
187     DBEContext *s = &s1->dectx;
188     int ret;
189
190     if ((ret = ff_dolby_e_parse_init(s, buf, buf_size)) < 0)
191         goto end;
192
193     if ((ret = ff_dolby_e_parse_header(s, &s1->metadata)) < 0)
194         goto end;
195
196     s2->duration = FRAME_SAMPLES;
197     switch (s1->metadata.nb_channels) {
198     case 4:
199         avctx->channel_layout = AV_CH_LAYOUT_4POINT0;
200         break;
201     case 6:
202         avctx->channel_layout = AV_CH_LAYOUT_5POINT1;
203         break;
204     case 8:
205         avctx->channel_layout = AV_CH_LAYOUT_7POINT1;
206         break;
207     }
208
209     avctx->channels    = s1->metadata.nb_channels;
210     avctx->sample_rate = sample_rate_tab[s1->metadata.fr_code];
211     avctx->sample_fmt  = AV_SAMPLE_FMT_FLTP;
212
213 end:
214     /* always return the full packet. this parser isn't doing any splitting or
215        combining, only packet analysis */
216     *poutbuf      = buf;
217     *poutbuf_size = buf_size;
218     return buf_size;
219 }
220
221 AVCodecParser ff_dolby_e_parser = {
222     .codec_ids      = { AV_CODEC_ID_DOLBY_E },
223     .priv_data_size = sizeof(DBEParseContext),
224     .parser_parse   = dolby_e_parse,
225     .parser_close   = ff_parse_close,
226 };
227 #endif /* CONFIG_DOLBY_E_PARSER */