]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca_parser.c
Merge remote-tracking branch 'qatar/master'
[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 "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 || state == DCA_HD_MARKER)
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 || pc1->lastmarker == DCA_HD_MARKER) {
65                     start_found = 1;
66                     pc1->lastmarker = state;
67                     break;
68                 }
69             }
70         }
71     }
72     if (start_found) {
73         for (; i < buf_size; i++) {
74             pc1->size++;
75             state = (state << 8) | buf[i];
76             if (state == DCA_HD_MARKER && !pc1->hd_pos)
77                 pc1->hd_pos = pc1->size;
78             if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
79                 if(pc1->framesize > pc1->size)
80                     continue;
81                 // We have to check that we really read a full frame here, and that it isn't a pure HD frame, because their size is not constant.
82                 if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){
83                     pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
84                 }
85                 pc->frame_start_found = 0;
86                 pc->state = -1;
87                 pc1->size = 0;
88                 return i - 3;
89             }
90         }
91     }
92     pc->frame_start_found = start_found;
93     pc->state = state;
94     return END_NOT_FOUND;
95 }
96
97 static av_cold int dca_parse_init(AVCodecParserContext * s)
98 {
99     DCAParseContext *pc1 = s->priv_data;
100
101     pc1->lastmarker = 0;
102     return 0;
103 }
104
105 int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
106                              int max_size)
107 {
108     uint32_t mrk;
109     int i, tmp;
110     const uint16_t *ssrc = (const uint16_t *) src;
111     uint16_t *sdst = (uint16_t *) dst;
112     PutBitContext pb;
113
114     if ((unsigned) src_size > (unsigned) max_size)
115         src_size = max_size;
116
117     mrk = AV_RB32(src);
118     switch (mrk) {
119     case DCA_MARKER_RAW_BE:
120         memcpy(dst, src, src_size);
121         return src_size;
122     case DCA_MARKER_RAW_LE:
123         for (i = 0; i < (src_size + 1) >> 1; i++)
124             *sdst++ = av_bswap16(*ssrc++);
125         return src_size;
126     case DCA_MARKER_14B_BE:
127     case DCA_MARKER_14B_LE:
128         init_put_bits(&pb, dst, max_size);
129         for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
130             tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
131             put_bits(&pb, 14, tmp);
132         }
133         flush_put_bits(&pb);
134         return (put_bits_count(&pb) + 7) >> 3;
135     default:
136         return AVERROR_INVALIDDATA;
137     }
138 }
139
140 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
141                             int *sample_rate)
142 {
143     GetBitContext gb;
144     uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
145     int ret, sample_blocks, sr_code;
146
147     if (buf_size < 12)
148         return AVERROR_INVALIDDATA;
149
150     if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
151         return ret;
152
153     init_get_bits(&gb, hdr, 96);
154
155     skip_bits_long(&gb, 39);
156     sample_blocks = get_bits(&gb, 7) + 1;
157     if (sample_blocks < 8)
158         return AVERROR_INVALIDDATA;
159     *duration = 256 * (sample_blocks / 8);
160
161     skip_bits(&gb, 20);
162     sr_code = get_bits(&gb, 4);
163     *sample_rate = dca_sample_rates[sr_code];
164     if (*sample_rate == 0)
165         return AVERROR_INVALIDDATA;
166
167     return 0;
168 }
169
170 static int dca_parse(AVCodecParserContext * s,
171                      AVCodecContext * avctx,
172                      const uint8_t ** poutbuf, int *poutbuf_size,
173                      const uint8_t * buf, int buf_size)
174 {
175     DCAParseContext *pc1 = s->priv_data;
176     ParseContext *pc = &pc1->pc;
177     int next, duration, sample_rate;
178
179     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
180         next = buf_size;
181     } else {
182         next = dca_find_frame_end(pc1, buf, buf_size);
183
184         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
185             *poutbuf = NULL;
186             *poutbuf_size = 0;
187             return buf_size;
188         }
189     }
190
191     /* read the duration and sample rate from the frame header */
192     if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
193         s->duration = duration;
194         if (!avctx->sample_rate)
195             avctx->sample_rate = sample_rate;
196     } else
197         s->duration = 0;
198
199     *poutbuf = buf;
200     *poutbuf_size = buf_size;
201     return next;
202 }
203
204 AVCodecParser ff_dca_parser = {
205     .codec_ids      = { CODEC_ID_DTS },
206     .priv_data_size = sizeof(DCAParseContext),
207     .parser_init    = dca_parse_init,
208     .parser_parse   = dca_parse,
209     .parser_close   = ff_parse_close,
210 };