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