1 /*
2 * Dirac parser
3 *
4 * Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org>
5 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * Dirac Parser
27 * @author Marco Gerards <marco@gnu.org>
28 */
29
30 #include <string.h>
31
34
36
37 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
38
39 /**
40 * Find the end of the current frame in the bitstream.
41 * @return the position of the first byte of the next frame or -1
42 */
55
57 const uint8_t *buf, int buf_size)
58 {
61
63 for (
i = 0;
i < buf_size;
i++) {
70 break;
71 }
72 }
73 }
74
77 for (;
i < buf_size;
i++) {
82 } else {
84 break;
85 }
86 } else
88 }
89 }
91 return -1;
92 }
93
99
102 {
104 int8_t *start;
105 static const uint8_t valid_pu_types[] = {
106 0x00, 0x10, 0x20, 0x30, 0x08, 0x48, 0xC8, 0xE8, 0x0A, 0x0C, 0x0D, 0x0E,
107 0x4C, 0x09, 0xCC, 0x88, 0xCB
108 };
109
111 return 0;
112
115
118
119 /* Check for valid parse code */
120 for (
i = 0;
i < 17;
i++)
121 if (valid_pu_types[
i] == pu->
pu_type)
122 break;
124 return 0;
125
128
129 /* Check if the parse offsets are somewhat sane */
132 return 0;
133
134 return 1;
135 }
136
138 int next, const uint8_t **buf, int *buf_size)
139 {
143
149 if (*buf_size == 0 && pc->
buffer[4] == 0x10) {
151 *buf_size = pc->
index;
152 return 0;
153 }
154 }
155
156 if (next == -1) {
157 /* Found a possible frame start but not a frame end */
158 void *new_buffer =
161 if (!new_buffer)
167 return -1;
168 } else {
169 /* Found a possible frame start and a possible frame end */
173 if (!new_buffer)
178
179 /* Need to check if we have a valid Parse Unit. We can't go by the
180 * sync pattern 'BBCD' alone because arithmetic coding of the residual
181 * and motion data can cause the pattern triggering a false start of
182 * frame. So check if the previous parse offset of the next parse unit
183 * is equal to the next parse offset of the current parse unit then
184 * we can be pretty sure that we have a valid parse unit */
189 ) {
191 *buf_size = next - 9;
193 return -1;
194 }
195
196 /* All non-frame data must be accompanied by frame data. This is to
197 * ensure that pts is set correctly. So if the current parse unit is
198 * not frame data, wait for frame data to come along */
199
202
204
205 if ((pu.
pu_type & 0x08) != 0x08) {
207 *buf_size = next;
208 return -1;
209 }
210
211 /* Get the picture number to set the pts and dts*/
213 uint8_t *cur_pu = pc->
buffer +
216 if (
s->last_pts == 0 &&
s->last_dts == 0)
219 s->dts =
s->last_dts + 1;
223 }
226
227 /* Finally have a complete Dirac data unit */
230
234 }
235 return next;
236 }
237
239 const uint8_t **poutbuf, int *poutbuf_size,
240 const uint8_t *buf, int buf_size)
241 {
243 int next;
244
246 *poutbuf_size = 0;
247
249 next = buf_size;
250 *poutbuf = buf;
251 *poutbuf_size = buf_size;
252 /* Assume that data has been packetized into an encapsulation unit. */
253 } else {
256 /* No frame start found yet. So throw away the entire buffer. */
257 return buf_size;
258
260 return buf_size;
261 }
262
263 *poutbuf = buf;
264 *poutbuf_size = buf_size;
265 return next;
266 }
267
269 {
271
274 }
275
281 };