]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca_parser.c
indeo: check that band output buffer exists
[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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "parser.h"
26 #include "dca.h"
27 #include "dcadata.h"
28 #include "dca_parser.h"
29 #include "get_bits.h"
30 #include "put_bits.h"
31
32 typedef struct DCAParseContext {
33     ParseContext pc;
34     uint32_t lastmarker;
35     int size;
36     int framesize;
37     int hd_pos;
38 } DCAParseContext;
39
40 #define IS_MARKER(state, i, buf, buf_size) \
41  ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
42  || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
43  || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
44
45 /**
46  * Find the end of the current frame in the bitstream.
47  * @return the position of the first byte of the next frame, or -1
48  */
49 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
50                               int buf_size)
51 {
52     int start_found, i;
53     uint32_t state;
54     ParseContext *pc = &pc1->pc;
55
56     start_found = pc->frame_start_found;
57     state = pc->state;
58
59     i = 0;
60     if (!start_found) {
61         for (i = 0; i < buf_size; i++) {
62             state = (state << 8) | buf[i];
63             if (IS_MARKER(state, i, buf, buf_size)) {
64                 if (pc1->lastmarker && state == pc1->lastmarker) {
65                     start_found = 1;
66                     break;
67                 } else if (!pc1->lastmarker) {
68                     start_found = 1;
69                     pc1->lastmarker = state;
70                     break;
71                 }
72             }
73         }
74     }
75     if (start_found) {
76         for (; i < buf_size; i++) {
77             pc1->size++;
78             state = (state << 8) | buf[i];
79             if (state == DCA_HD_MARKER && !pc1->hd_pos)
80                 pc1->hd_pos = pc1->size;
81             if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
82                 if(pc1->framesize > pc1->size)
83                     continue;
84                 if(!pc1->framesize){
85                     pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
86                 }
87                 pc->frame_start_found = 0;
88                 pc->state = -1;
89                 pc1->size = 0;
90                 return i - 3;
91             }
92         }
93     }
94     pc->frame_start_found = start_found;
95     pc->state = state;
96     return END_NOT_FOUND;
97 }
98
99 static av_cold int dca_parse_init(AVCodecParserContext * s)
100 {
101     DCAParseContext *pc1 = s->priv_data;
102
103     pc1->lastmarker = 0;
104     return 0;
105 }
106
107 int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
108                              int max_size)
109 {
110     uint32_t mrk;
111     int i, tmp;
112     const uint16_t *ssrc = (const uint16_t *) src;
113     uint16_t *sdst = (uint16_t *) dst;
114     PutBitContext pb;
115
116     if ((unsigned) src_size > (unsigned) max_size)
117         src_size = max_size;
118
119     mrk = AV_RB32(src);
120     switch (mrk) {
121     case DCA_MARKER_RAW_BE:
122         memcpy(dst, src, src_size);
123         return src_size;
124     case DCA_MARKER_RAW_LE:
125         for (i = 0; i < (src_size + 1) >> 1; i++)
126             *sdst++ = av_bswap16(*ssrc++);
127         return src_size;
128     case DCA_MARKER_14B_BE:
129     case DCA_MARKER_14B_LE:
130         init_put_bits(&pb, dst, max_size);
131         for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
132             tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
133             put_bits(&pb, 14, tmp);
134         }
135         flush_put_bits(&pb);
136         return (put_bits_count(&pb) + 7) >> 3;
137     default:
138         return AVERROR_INVALIDDATA;
139     }
140 }
141
142 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
143                             int *sample_rate)
144 {
145     GetBitContext gb;
146     uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
147     int ret, sample_blocks, sr_code;
148
149     if (buf_size < 12)
150         return AVERROR_INVALIDDATA;
151
152     if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
153         return ret;
154
155     init_get_bits(&gb, hdr, 96);
156
157     skip_bits_long(&gb, 39);
158     sample_blocks = get_bits(&gb, 7) + 1;
159     if (sample_blocks < 8)
160         return AVERROR_INVALIDDATA;
161     *duration = 256 * (sample_blocks / 8);
162
163     skip_bits(&gb, 20);
164     sr_code = get_bits(&gb, 4);
165     *sample_rate = dca_sample_rates[sr_code];
166     if (*sample_rate == 0)
167         return AVERROR_INVALIDDATA;
168
169     return 0;
170 }
171
172 static int dca_parse(AVCodecParserContext * s,
173                      AVCodecContext * avctx,
174                      const uint8_t ** poutbuf, int *poutbuf_size,
175                      const uint8_t * buf, int buf_size)
176 {
177     DCAParseContext *pc1 = s->priv_data;
178     ParseContext *pc = &pc1->pc;
179     int next, duration, sample_rate;
180
181     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
182         next = buf_size;
183     } else {
184         next = dca_find_frame_end(pc1, buf, buf_size);
185
186         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
187             *poutbuf = NULL;
188             *poutbuf_size = 0;
189             return buf_size;
190         }
191     }
192
193     /* read the duration and sample rate from the frame header */
194     if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
195         s->duration = duration;
196         if (!avctx->sample_rate)
197             avctx->sample_rate = sample_rate;
198     } else
199         s->duration = 0;
200
201     *poutbuf = buf;
202     *poutbuf_size = buf_size;
203     return next;
204 }
205
206 AVCodecParser ff_dca_parser = {
207     .codec_ids      = { CODEC_ID_DTS },
208     .priv_data_size = sizeof(DCAParseContext),
209     .parser_init    = dca_parse_init,
210     .parser_parse   = dca_parse,
211     .parser_close   = ff_parse_close,
212 };