/src/ffmpeg/libavcodec/dirac_parser.c
Line | Count | Source |
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 | | |
32 | | #include "libavutil/attributes.h" |
33 | | #include "libavutil/intreadwrite.h" |
34 | | #include "libavutil/mem.h" |
35 | | |
36 | | #include "avcodec.h" |
37 | | #include "parser_internal.h" |
38 | | |
39 | 54.8M | #define DIRAC_PARSE_INFO_PREFIX 0x42424344 |
40 | | |
41 | | /** |
42 | | * Find the end of the current frame in the bitstream. |
43 | | * @return the position of the first byte of the next frame or -1 |
44 | | */ |
45 | | typedef struct DiracParseContext { |
46 | | int state; |
47 | | int is_synced; |
48 | | int sync_offset; |
49 | | int header_bytes_needed; |
50 | | int overread_index; |
51 | | int buffer_size; |
52 | | int index; |
53 | | uint8_t *buffer; |
54 | | int dirac_unit_size; |
55 | | uint8_t *dirac_unit; |
56 | | } DiracParseContext; |
57 | | |
58 | | static int find_frame_end(DiracParseContext *pc, |
59 | | const uint8_t *buf, int buf_size) |
60 | 2.55M | { |
61 | 2.55M | uint32_t state = pc->state; |
62 | 2.55M | int i = 0; |
63 | | |
64 | 2.55M | if (!pc->is_synced) { |
65 | 10.1M | for (i = 0; i < buf_size; i++) { |
66 | 10.0M | state = (state << 8) | buf[i]; |
67 | 10.0M | if (state == DIRAC_PARSE_INFO_PREFIX) { |
68 | 3.46k | state = -1; |
69 | 3.46k | pc->is_synced = 1; |
70 | 3.46k | pc->header_bytes_needed = 9; |
71 | 3.46k | pc->sync_offset = i; |
72 | 3.46k | break; |
73 | 3.46k | } |
74 | 10.0M | } |
75 | 73.8k | } |
76 | | |
77 | 2.55M | if (pc->is_synced) { |
78 | 2.48M | pc->sync_offset = 0; |
79 | 44.9M | for (; i < buf_size; i++) { |
80 | 44.8M | if (state == DIRAC_PARSE_INFO_PREFIX) { |
81 | 2.31M | if ((buf_size - i) >= pc->header_bytes_needed) { |
82 | 1.61M | pc->state = -1; |
83 | 1.61M | return i + pc->header_bytes_needed; |
84 | 1.61M | } else { |
85 | 699k | pc->header_bytes_needed = 9 - (buf_size - i); |
86 | 699k | break; |
87 | 699k | } |
88 | 2.31M | } else |
89 | 42.4M | state = (state << 8) | buf[i]; |
90 | 44.8M | } |
91 | 2.48M | } |
92 | 946k | pc->state = state; |
93 | 946k | return -1; |
94 | 2.55M | } |
95 | | |
96 | | typedef struct DiracParseUnit { |
97 | | int next_pu_offset; |
98 | | int prev_pu_offset; |
99 | | uint8_t pu_type; |
100 | | } DiracParseUnit; |
101 | | |
102 | | static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc, |
103 | | int offset) |
104 | 2.98M | { |
105 | 2.98M | int i; |
106 | 2.98M | int8_t *start; |
107 | 2.98M | static const uint8_t valid_pu_types[] = { |
108 | 2.98M | 0x00, 0x10, 0x20, 0x30, 0x08, 0x48, 0xC8, 0xE8, 0x0A, 0x0C, 0x0D, 0x0E, |
109 | 2.98M | 0x4C, 0x09, 0xCC, 0x88, 0xCB |
110 | 2.98M | }; |
111 | | |
112 | 2.98M | if (offset < 0 || pc->index - 13 < offset) |
113 | 141k | return 0; |
114 | | |
115 | 2.84M | start = pc->buffer + offset; |
116 | 2.84M | pu->pu_type = start[4]; |
117 | | |
118 | 2.84M | pu->next_pu_offset = AV_RB32(start + 5); |
119 | 2.84M | pu->prev_pu_offset = AV_RB32(start + 9); |
120 | | |
121 | | /* Check for valid parse code */ |
122 | 15.8M | for (i = 0; i < 17; i++) |
123 | 15.5M | if (valid_pu_types[i] == pu->pu_type) |
124 | 2.52M | break; |
125 | 2.84M | if (i == 17) |
126 | 316k | return 0; |
127 | | |
128 | 2.52M | if (pu->pu_type == 0x10 && pu->next_pu_offset == 0x00) |
129 | 367k | pu->next_pu_offset = 13; /* The length of a parse info header */ |
130 | | |
131 | | /* Check if the parse offsets are somewhat sane */ |
132 | 2.52M | if ((pu->next_pu_offset && pu->next_pu_offset < 13) || |
133 | 2.51M | (pu->prev_pu_offset && pu->prev_pu_offset < 13)) |
134 | 89.8k | return 0; |
135 | | |
136 | 2.43M | return 1; |
137 | 2.52M | } |
138 | | |
139 | | static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx, |
140 | | int next, const uint8_t **buf, int *buf_size) |
141 | 2.48M | { |
142 | 2.48M | int parse_timing_info = (s->pts == AV_NOPTS_VALUE && |
143 | 2.48M | s->dts == AV_NOPTS_VALUE); |
144 | 2.48M | DiracParseContext *pc = s->priv_data; |
145 | | |
146 | 2.48M | if (pc->overread_index) { |
147 | 613k | memmove(pc->buffer, pc->buffer + pc->overread_index, |
148 | 613k | pc->index - pc->overread_index); |
149 | 613k | pc->index -= pc->overread_index; |
150 | 613k | pc->overread_index = 0; |
151 | 613k | if (*buf_size == 0 && pc->buffer[4] == 0x10) { |
152 | 895 | *buf = pc->buffer; |
153 | 895 | *buf_size = pc->index; |
154 | 895 | return 0; |
155 | 895 | } |
156 | 613k | } |
157 | | |
158 | 2.48M | if (next == -1) { |
159 | | /* Found a possible frame start but not a frame end */ |
160 | 875k | void *new_buffer = |
161 | 875k | av_fast_realloc(pc->buffer, &pc->buffer_size, |
162 | 875k | pc->index + (*buf_size - pc->sync_offset)); |
163 | 875k | if (!new_buffer) |
164 | 0 | return AVERROR(ENOMEM); |
165 | 875k | pc->buffer = new_buffer; |
166 | 875k | memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset), |
167 | 875k | *buf_size - pc->sync_offset); |
168 | 875k | pc->index += *buf_size - pc->sync_offset; |
169 | 875k | return -1; |
170 | 1.61M | } else { |
171 | | /* Found a possible frame start and a possible frame end */ |
172 | 1.61M | DiracParseUnit pu1, pu; |
173 | 1.61M | void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, |
174 | 1.61M | pc->index + next); |
175 | 1.61M | if (!new_buffer) |
176 | 0 | return AVERROR(ENOMEM); |
177 | 1.61M | pc->buffer = new_buffer; |
178 | 1.61M | memcpy(pc->buffer + pc->index, *buf, next); |
179 | 1.61M | pc->index += next; |
180 | | |
181 | | /* Need to check if we have a valid Parse Unit. We can't go by the |
182 | | * sync pattern 'BBCD' alone because arithmetic coding of the residual |
183 | | * and motion data can cause the pattern triggering a false start of |
184 | | * frame. So check if the previous parse offset of the next parse unit |
185 | | * is equal to the next parse offset of the current parse unit then |
186 | | * we can be pretty sure that we have a valid parse unit */ |
187 | 1.61M | if (!unpack_parse_unit(&pu1, pc, pc->index - 13) || |
188 | 1.37M | !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) || |
189 | 1.06M | pu.next_pu_offset != pu1.prev_pu_offset || |
190 | 947k | pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset |
191 | 1.61M | ) { |
192 | 667k | pc->index -= 9; |
193 | 667k | *buf_size = next - 9; |
194 | 667k | pc->header_bytes_needed = 9; |
195 | 667k | return -1; |
196 | 667k | } |
197 | | |
198 | | /* All non-frame data must be accompanied by frame data. This is to |
199 | | * ensure that pts is set correctly. So if the current parse unit is |
200 | | * not frame data, wait for frame data to come along */ |
201 | | |
202 | 943k | pc->dirac_unit = pc->buffer + pc->index - 13 - |
203 | 943k | pu1.prev_pu_offset - pc->dirac_unit_size; |
204 | | |
205 | 943k | pc->dirac_unit_size += pu.next_pu_offset; |
206 | | |
207 | 943k | if ((pu.pu_type & 0x08) != 0x08) { |
208 | 329k | pc->header_bytes_needed = 9; |
209 | 329k | *buf_size = next; |
210 | 329k | return -1; |
211 | 329k | } |
212 | | |
213 | | /* Get the picture number to set the pts and dts*/ |
214 | 613k | if (parse_timing_info && pu1.prev_pu_offset >= 13) { |
215 | 434k | uint8_t *cur_pu = pc->buffer + |
216 | 434k | pc->index - 13 - pu1.prev_pu_offset; |
217 | 434k | int64_t pts = AV_RB32(cur_pu + 13); |
218 | 434k | if (s->last_pts == 0 && s->last_dts == 0) |
219 | 16.9k | s->dts = pts - 1; |
220 | 418k | else if (s->last_dts != AV_NOPTS_VALUE) |
221 | 381k | s->dts = s->last_dts + 1; |
222 | 434k | s->pts = pts; |
223 | 434k | if (!avctx->has_b_frames && (cur_pu[4] & 0x03)) |
224 | 1.93k | avctx->has_b_frames = 1; |
225 | 434k | } |
226 | 613k | if (avctx->has_b_frames && s->pts == s->dts) |
227 | 70.3k | s->pict_type = AV_PICTURE_TYPE_B; |
228 | | |
229 | | /* Finally have a complete Dirac data unit */ |
230 | 613k | *buf = pc->dirac_unit; |
231 | 613k | *buf_size = pc->dirac_unit_size; |
232 | | |
233 | 613k | pc->dirac_unit_size = 0; |
234 | 613k | pc->overread_index = pc->index - 13; |
235 | 613k | pc->header_bytes_needed = 9; |
236 | 613k | } |
237 | 613k | return next; |
238 | 2.48M | } |
239 | | |
240 | | static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx, |
241 | | const uint8_t **poutbuf, int *poutbuf_size, |
242 | | const uint8_t *buf, int buf_size) |
243 | 2.55M | { |
244 | 2.55M | DiracParseContext *pc = s->priv_data; |
245 | 2.55M | int next; |
246 | | |
247 | 2.55M | *poutbuf = NULL; |
248 | 2.55M | *poutbuf_size = 0; |
249 | | |
250 | 2.55M | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
251 | 1.85k | next = buf_size; |
252 | 1.85k | *poutbuf = buf; |
253 | 1.85k | *poutbuf_size = buf_size; |
254 | | /* Assume that data has been packetized into an encapsulation unit. */ |
255 | 2.55M | } else { |
256 | 2.55M | next = find_frame_end(pc, buf, buf_size); |
257 | 2.55M | if (!pc->is_synced && next == -1) |
258 | | /* No frame start found yet. So throw away the entire buffer. */ |
259 | 70.3k | return buf_size; |
260 | | |
261 | 2.48M | if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0) |
262 | 1.87M | return buf_size; |
263 | 2.48M | } |
264 | | |
265 | 616k | *poutbuf = buf; |
266 | 616k | *poutbuf_size = buf_size; |
267 | 616k | return next; |
268 | 2.55M | } |
269 | | |
270 | | static av_cold void dirac_parse_close(AVCodecParserContext *s) |
271 | 5.90k | { |
272 | 5.90k | DiracParseContext *pc = s->priv_data; |
273 | | |
274 | 5.90k | if (pc->buffer_size > 0) |
275 | 3.46k | av_freep(&pc->buffer); |
276 | 5.90k | } |
277 | | |
278 | | const FFCodecParser ff_dirac_parser = { |
279 | | PARSER_CODEC_LIST(AV_CODEC_ID_DIRAC), |
280 | | .priv_data_size = sizeof(DiracParseContext), |
281 | | .parse = dirac_parse, |
282 | | .close = dirac_parse_close, |
283 | | }; |