]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca_parser.c
Merge commit '21180b73239c6360aa28496d4879713b7ba4a8e5'
[ffmpeg] / libavcodec / dca_parser.c
1 /*
2  * DCA parser
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "dca.h"
26 #include "dca_syncwords.h"
27 #include "get_bits.h"
28 #include "parser.h"
29
30 typedef struct DCAParseContext {
31     ParseContext pc;
32     uint32_t lastmarker;
33     int size;
34     int framesize;
35 } DCAParseContext;
36
37 #define IS_CORE_MARKER(state) \
38     (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
39      ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
40      ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE     << 16) | 0x00FC)) || \
41      ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE     << 16) | 0xFC00)))
42
43 #define IS_EXSS_MARKER(state)   ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
44
45 #define IS_MARKER(state)    (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
46
47 #define CORE_MARKER(state)  ((state >> 16) & 0xFFFFFFFF)
48 #define EXSS_MARKER(state)  (state & 0xFFFFFFFF)
49
50 /**
51  * Find the end of the current frame in the bitstream.
52  * @return the position of the first byte of the next frame, or -1
53  */
54 static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
55                               int buf_size)
56 {
57     int start_found, i;
58     uint64_t state;
59     ParseContext *pc = &pc1->pc;
60
61     start_found = pc->frame_start_found;
62     state       = pc->state64;
63
64     i = 0;
65     if (!start_found) {
66         for (i = 0; i < buf_size; i++) {
67             state = (state << 8) | buf[i];
68             if (IS_MARKER(state)) {
69                 if (!pc1->lastmarker || CORE_MARKER(state) == pc1->lastmarker || pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM) {
70                     start_found = 1;
71                     if (IS_EXSS_MARKER(state))
72                         pc1->lastmarker = EXSS_MARKER(state);
73                     else
74                         pc1->lastmarker = CORE_MARKER(state);
75                     i++;
76                     break;
77                 }
78             }
79         }
80     }
81     if (start_found) {
82         for (; i < buf_size; i++) {
83             pc1->size++;
84             state = (state << 8) | buf[i];
85             if (IS_MARKER(state) && (CORE_MARKER(state) == pc1->lastmarker || pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
86                 if (pc1->framesize > pc1->size)
87                     continue;
88                 pc->frame_start_found = 0;
89                 pc->state64           = -1;
90                 pc1->size             = 0;
91                 return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
92             }
93         }
94     }
95     pc->frame_start_found = start_found;
96     pc->state64           = state;
97     return END_NOT_FOUND;
98 }
99
100 static av_cold int dca_parse_init(AVCodecParserContext *s)
101 {
102     DCAParseContext *pc1 = s->priv_data;
103
104     pc1->lastmarker = 0;
105     return 0;
106 }
107
108 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
109                             int *sample_rate, int *framesize)
110 {
111     GetBitContext gb;
112     uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
113     int ret, sample_blocks, sr_code;
114
115     if (buf_size < 12)
116         return AVERROR_INVALIDDATA;
117
118     if ((ret = avpriv_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
119         return ret;
120
121     init_get_bits(&gb, hdr, 96);
122
123     skip_bits_long(&gb, 39);
124     sample_blocks = get_bits(&gb, 7) + 1;
125     if (sample_blocks < 8)
126         return AVERROR_INVALIDDATA;
127     *duration = 256 * (sample_blocks / 8);
128
129     *framesize = get_bits(&gb, 14) + 1;
130     if (*framesize < 95)
131         return AVERROR_INVALIDDATA;
132
133     skip_bits(&gb, 6);
134     sr_code      = get_bits(&gb, 4);
135     *sample_rate = avpriv_dca_sample_rates[sr_code];
136     if (*sample_rate == 0)
137         return AVERROR_INVALIDDATA;
138
139     return 0;
140 }
141
142 static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
143                      const uint8_t **poutbuf, int *poutbuf_size,
144                      const uint8_t *buf, int buf_size)
145 {
146     DCAParseContext *pc1 = s->priv_data;
147     ParseContext *pc = &pc1->pc;
148     int next, duration, sample_rate;
149
150     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
151         next = buf_size;
152     } else {
153         next = dca_find_frame_end(pc1, buf, buf_size);
154
155         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
156             *poutbuf      = NULL;
157             *poutbuf_size = 0;
158             return buf_size;
159         }
160     }
161
162     /* read the duration and sample rate from the frame header */
163     if (!dca_parse_params(buf, buf_size, &duration, &sample_rate, &pc1->framesize)) {
164         s->duration        = duration;
165         avctx->sample_rate = sample_rate;
166     } else
167         s->duration = 0;
168
169     *poutbuf      = buf;
170     *poutbuf_size = buf_size;
171     return next;
172 }
173
174 AVCodecParser ff_dca_parser = {
175     .codec_ids      = { AV_CODEC_ID_DTS },
176     .priv_data_size = sizeof(DCAParseContext),
177     .parser_init    = dca_parse_init,
178     .parser_parse   = dca_parse,
179     .parser_close   = ff_parse_close,
180 };