yading@10
|
1 /*
|
yading@10
|
2 * Dirac parser
|
yading@10
|
3 *
|
yading@10
|
4 * Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org>
|
yading@10
|
5 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com>
|
yading@10
|
6 *
|
yading@10
|
7 * This file is part of FFmpeg.
|
yading@10
|
8 *
|
yading@10
|
9 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
10 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
11 * License as published by the Free Software Foundation; either
|
yading@10
|
12 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
13 *
|
yading@10
|
14 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
17 * Lesser General Public License for more details.
|
yading@10
|
18 *
|
yading@10
|
19 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
20 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
22 */
|
yading@10
|
23
|
yading@10
|
24 /**
|
yading@10
|
25 * @file
|
yading@10
|
26 * Dirac Parser
|
yading@10
|
27 * @author Marco Gerards <marco@gnu.org>
|
yading@10
|
28 */
|
yading@10
|
29
|
yading@10
|
30 #include <string.h>
|
yading@10
|
31
|
yading@10
|
32 #include "libavutil/intreadwrite.h"
|
yading@10
|
33 #include "libavutil/mem.h"
|
yading@10
|
34 #include "parser.h"
|
yading@10
|
35
|
yading@10
|
36 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
|
yading@10
|
37
|
yading@10
|
38 /**
|
yading@10
|
39 * Find the end of the current frame in the bitstream.
|
yading@10
|
40 * @return the position of the first byte of the next frame or -1
|
yading@10
|
41 */
|
yading@10
|
42 typedef struct DiracParseContext {
|
yading@10
|
43 int state;
|
yading@10
|
44 int is_synced;
|
yading@10
|
45 int sync_offset;
|
yading@10
|
46 int header_bytes_needed;
|
yading@10
|
47 int overread_index;
|
yading@10
|
48 int buffer_size;
|
yading@10
|
49 int index;
|
yading@10
|
50 uint8_t *buffer;
|
yading@10
|
51 int dirac_unit_size;
|
yading@10
|
52 uint8_t *dirac_unit;
|
yading@10
|
53 } DiracParseContext;
|
yading@10
|
54
|
yading@10
|
55 static int find_frame_end(DiracParseContext *pc,
|
yading@10
|
56 const uint8_t *buf, int buf_size)
|
yading@10
|
57 {
|
yading@10
|
58 uint32_t state = pc->state;
|
yading@10
|
59 int i = 0;
|
yading@10
|
60
|
yading@10
|
61 if (!pc->is_synced) {
|
yading@10
|
62 for (i = 0; i < buf_size; i++) {
|
yading@10
|
63 state = (state << 8) | buf[i];
|
yading@10
|
64 if (state == DIRAC_PARSE_INFO_PREFIX) {
|
yading@10
|
65 state = -1;
|
yading@10
|
66 pc->is_synced = 1;
|
yading@10
|
67 pc->header_bytes_needed = 9;
|
yading@10
|
68 pc->sync_offset = i;
|
yading@10
|
69 break;
|
yading@10
|
70 }
|
yading@10
|
71 }
|
yading@10
|
72 }
|
yading@10
|
73
|
yading@10
|
74 if (pc->is_synced) {
|
yading@10
|
75 pc->sync_offset = 0;
|
yading@10
|
76 for (; i < buf_size; i++) {
|
yading@10
|
77 if (state == DIRAC_PARSE_INFO_PREFIX) {
|
yading@10
|
78 if ((buf_size-i) >= pc->header_bytes_needed) {
|
yading@10
|
79 pc->state = -1;
|
yading@10
|
80 return i + pc->header_bytes_needed;
|
yading@10
|
81 } else {
|
yading@10
|
82 pc->header_bytes_needed = 9-(buf_size-i);
|
yading@10
|
83 break;
|
yading@10
|
84 }
|
yading@10
|
85 } else
|
yading@10
|
86 state = (state << 8) | buf[i];
|
yading@10
|
87 }
|
yading@10
|
88 }
|
yading@10
|
89 pc->state = state;
|
yading@10
|
90 return -1;
|
yading@10
|
91 }
|
yading@10
|
92
|
yading@10
|
93 typedef struct DiracParseUnit
|
yading@10
|
94 {
|
yading@10
|
95 int next_pu_offset;
|
yading@10
|
96 int prev_pu_offset;
|
yading@10
|
97 uint8_t pu_type;
|
yading@10
|
98 } DiracParseUnit;
|
yading@10
|
99
|
yading@10
|
100 static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
|
yading@10
|
101 int offset)
|
yading@10
|
102 {
|
yading@10
|
103 uint8_t *start = pc->buffer + offset;
|
yading@10
|
104 uint8_t *end = pc->buffer + pc->index;
|
yading@10
|
105 if (start < pc->buffer || (start+13 > end))
|
yading@10
|
106 return 0;
|
yading@10
|
107 pu->pu_type = start[4];
|
yading@10
|
108
|
yading@10
|
109 pu->next_pu_offset = AV_RB32(start+5);
|
yading@10
|
110 pu->prev_pu_offset = AV_RB32(start+9);
|
yading@10
|
111
|
yading@10
|
112 if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
|
yading@10
|
113 pu->next_pu_offset = 13;
|
yading@10
|
114
|
yading@10
|
115 return 1;
|
yading@10
|
116 }
|
yading@10
|
117
|
yading@10
|
118 static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
|
yading@10
|
119 int next, const uint8_t **buf, int *buf_size)
|
yading@10
|
120 {
|
yading@10
|
121 int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
|
yading@10
|
122 s->dts == AV_NOPTS_VALUE);
|
yading@10
|
123 DiracParseContext *pc = s->priv_data;
|
yading@10
|
124
|
yading@10
|
125 if (pc->overread_index) {
|
yading@10
|
126 memcpy(pc->buffer, pc->buffer + pc->overread_index,
|
yading@10
|
127 pc->index - pc->overread_index);
|
yading@10
|
128 pc->index -= pc->overread_index;
|
yading@10
|
129 pc->overread_index = 0;
|
yading@10
|
130 if (*buf_size == 0 && pc->buffer[4] == 0x10) {
|
yading@10
|
131 *buf = pc->buffer;
|
yading@10
|
132 *buf_size = pc->index;
|
yading@10
|
133 return 0;
|
yading@10
|
134 }
|
yading@10
|
135 }
|
yading@10
|
136
|
yading@10
|
137 if ( next == -1) {
|
yading@10
|
138 /* Found a possible frame start but not a frame end */
|
yading@10
|
139 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
|
yading@10
|
140 pc->index + (*buf_size -
|
yading@10
|
141 pc->sync_offset));
|
yading@10
|
142 pc->buffer = new_buffer;
|
yading@10
|
143 memcpy(pc->buffer+pc->index, (*buf + pc->sync_offset),
|
yading@10
|
144 *buf_size - pc->sync_offset);
|
yading@10
|
145 pc->index += *buf_size - pc->sync_offset;
|
yading@10
|
146 return -1;
|
yading@10
|
147 } else {
|
yading@10
|
148 /* Found a possible frame start and a possible frame end */
|
yading@10
|
149 DiracParseUnit pu1, pu;
|
yading@10
|
150 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
|
yading@10
|
151 pc->index + next);
|
yading@10
|
152 pc->buffer = new_buffer;
|
yading@10
|
153 memcpy(pc->buffer + pc->index, *buf, next);
|
yading@10
|
154 pc->index += next;
|
yading@10
|
155
|
yading@10
|
156 /* Need to check if we have a valid Parse Unit. We can't go by the
|
yading@10
|
157 * sync pattern 'BBCD' alone because arithmetic coding of the residual
|
yading@10
|
158 * and motion data can cause the pattern triggering a false start of
|
yading@10
|
159 * frame. So check if the previous parse offset of the next parse unit
|
yading@10
|
160 * is equal to the next parse offset of the current parse unit then
|
yading@10
|
161 * we can be pretty sure that we have a valid parse unit */
|
yading@10
|
162 if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
|
yading@10
|
163 !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
|
yading@10
|
164 pu.next_pu_offset != pu1.prev_pu_offset ||
|
yading@10
|
165 pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
|
yading@10
|
166 ) {
|
yading@10
|
167 pc->index -= 9;
|
yading@10
|
168 *buf_size = next-9;
|
yading@10
|
169 pc->header_bytes_needed = 9;
|
yading@10
|
170 return -1;
|
yading@10
|
171 }
|
yading@10
|
172
|
yading@10
|
173 /* All non-frame data must be accompanied by frame data. This is to
|
yading@10
|
174 * ensure that pts is set correctly. So if the current parse unit is
|
yading@10
|
175 * not frame data, wait for frame data to come along */
|
yading@10
|
176
|
yading@10
|
177 pc->dirac_unit = pc->buffer + pc->index - 13 -
|
yading@10
|
178 pu1.prev_pu_offset - pc->dirac_unit_size;
|
yading@10
|
179
|
yading@10
|
180 pc->dirac_unit_size += pu.next_pu_offset;
|
yading@10
|
181
|
yading@10
|
182 if ((pu.pu_type&0x08) != 0x08) {
|
yading@10
|
183 pc->header_bytes_needed = 9;
|
yading@10
|
184 *buf_size = next;
|
yading@10
|
185 return -1;
|
yading@10
|
186 }
|
yading@10
|
187
|
yading@10
|
188 /* Get the picture number to set the pts and dts*/
|
yading@10
|
189 if (parse_timing_info) {
|
yading@10
|
190 uint8_t *cur_pu = pc->buffer +
|
yading@10
|
191 pc->index - 13 - pu1.prev_pu_offset;
|
yading@10
|
192 int pts = AV_RB32(cur_pu + 13);
|
yading@10
|
193 if (s->last_pts == 0 && s->last_dts == 0)
|
yading@10
|
194 s->dts = pts - 1;
|
yading@10
|
195 else
|
yading@10
|
196 s->dts = s->last_dts+1;
|
yading@10
|
197 s->pts = pts;
|
yading@10
|
198 if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
|
yading@10
|
199 avctx->has_b_frames = 1;
|
yading@10
|
200 }
|
yading@10
|
201 if (avctx->has_b_frames && s->pts == s->dts)
|
yading@10
|
202 s->pict_type = AV_PICTURE_TYPE_B;
|
yading@10
|
203
|
yading@10
|
204 /* Finally have a complete Dirac data unit */
|
yading@10
|
205 *buf = pc->dirac_unit;
|
yading@10
|
206 *buf_size = pc->dirac_unit_size;
|
yading@10
|
207
|
yading@10
|
208 pc->dirac_unit_size = 0;
|
yading@10
|
209 pc->overread_index = pc->index-13;
|
yading@10
|
210 pc->header_bytes_needed = 9;
|
yading@10
|
211 }
|
yading@10
|
212 return next;
|
yading@10
|
213 }
|
yading@10
|
214
|
yading@10
|
215 static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
|
yading@10
|
216 const uint8_t **poutbuf, int *poutbuf_size,
|
yading@10
|
217 const uint8_t *buf, int buf_size)
|
yading@10
|
218 {
|
yading@10
|
219 DiracParseContext *pc = s->priv_data;
|
yading@10
|
220 int next;
|
yading@10
|
221
|
yading@10
|
222 *poutbuf = NULL;
|
yading@10
|
223 *poutbuf_size = 0;
|
yading@10
|
224
|
yading@10
|
225 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
|
yading@10
|
226 next = buf_size;
|
yading@10
|
227 *poutbuf = buf;
|
yading@10
|
228 *poutbuf_size = buf_size;
|
yading@10
|
229 /* Assume that data has been packetized into an encapsulation unit. */
|
yading@10
|
230 } else {
|
yading@10
|
231 next = find_frame_end(pc, buf, buf_size);
|
yading@10
|
232 if (!pc->is_synced && next == -1) {
|
yading@10
|
233 /* No frame start found yet. So throw away the entire buffer. */
|
yading@10
|
234 return buf_size;
|
yading@10
|
235 }
|
yading@10
|
236
|
yading@10
|
237 if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0) {
|
yading@10
|
238 return buf_size;
|
yading@10
|
239 }
|
yading@10
|
240 }
|
yading@10
|
241
|
yading@10
|
242 *poutbuf = buf;
|
yading@10
|
243 *poutbuf_size = buf_size;
|
yading@10
|
244 return next;
|
yading@10
|
245 }
|
yading@10
|
246
|
yading@10
|
247 static void dirac_parse_close(AVCodecParserContext *s)
|
yading@10
|
248 {
|
yading@10
|
249 DiracParseContext *pc = s->priv_data;
|
yading@10
|
250
|
yading@10
|
251 if (pc->buffer_size > 0)
|
yading@10
|
252 av_free(pc->buffer);
|
yading@10
|
253 }
|
yading@10
|
254
|
yading@10
|
255 AVCodecParser ff_dirac_parser = {
|
yading@10
|
256 .codec_ids = { AV_CODEC_ID_DIRAC },
|
yading@10
|
257 .priv_data_size = sizeof(DiracParseContext),
|
yading@10
|
258 .parser_parse = dirac_parse,
|
yading@10
|
259 .parser_close = dirac_parse_close,
|
yading@10
|
260 };
|