4 * Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org>
5 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com>
7 * This file is part of FFmpeg.
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.
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.
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
27 * @author Marco Gerards <marco@gnu.org>
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem.h"
37 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
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
43 typedef struct DiracParseContext {
47 int header_bytes_needed;
56 static int find_frame_end(DiracParseContext *pc,
57 const uint8_t *buf, int buf_size)
59 uint32_t state = pc->state;
63 for (i = 0; i < buf_size; i++) {
64 state = (state << 8) | buf[i];
65 if (state == DIRAC_PARSE_INFO_PREFIX) {
68 pc->header_bytes_needed = 9;
77 for (; i < buf_size; i++) {
78 if (state == DIRAC_PARSE_INFO_PREFIX) {
79 if ((buf_size - i) >= pc->header_bytes_needed) {
81 return i + pc->header_bytes_needed;
83 pc->header_bytes_needed = 9 - (buf_size - i);
87 state = (state << 8) | buf[i];
94 typedef struct DiracParseUnit {
100 static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
105 if (offset < 0 || pc->index - 13 < offset)
108 start = pc->buffer + offset;
109 pu->pu_type = start[4];
111 pu->next_pu_offset = AV_RB32(start + 5);
112 pu->prev_pu_offset = AV_RB32(start + 9);
114 if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
115 pu->next_pu_offset = 13;
117 if (pu->next_pu_offset && pu->next_pu_offset < 13) {
118 av_log(NULL, AV_LOG_ERROR, "next_pu_offset %d is invalid\n", pu->next_pu_offset);
121 if (pu->prev_pu_offset && pu->prev_pu_offset < 13) {
122 av_log(NULL, AV_LOG_ERROR, "prev_pu_offset %d is invalid\n", pu->prev_pu_offset);
129 static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
130 int next, const uint8_t **buf, int *buf_size)
132 int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
133 s->dts == AV_NOPTS_VALUE);
134 DiracParseContext *pc = s->priv_data;
136 if (pc->overread_index) {
137 memmove(pc->buffer, pc->buffer + pc->overread_index,
138 pc->index - pc->overread_index);
139 pc->index -= pc->overread_index;
140 pc->overread_index = 0;
141 if (*buf_size == 0 && pc->buffer[4] == 0x10) {
143 *buf_size = pc->index;
149 /* Found a possible frame start but not a frame end */
151 av_fast_realloc(pc->buffer, &pc->buffer_size,
152 pc->index + (*buf_size - pc->sync_offset));
154 return AVERROR(ENOMEM);
155 pc->buffer = new_buffer;
156 memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
157 *buf_size - pc->sync_offset);
158 pc->index += *buf_size - pc->sync_offset;
161 /* Found a possible frame start and a possible frame end */
162 DiracParseUnit pu1, pu;
163 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
166 return AVERROR(ENOMEM);
167 pc->buffer = new_buffer;
168 memcpy(pc->buffer + pc->index, *buf, next);
171 /* Need to check if we have a valid Parse Unit. We can't go by the
172 * sync pattern 'BBCD' alone because arithmetic coding of the residual
173 * and motion data can cause the pattern triggering a false start of
174 * frame. So check if the previous parse offset of the next parse unit
175 * is equal to the next parse offset of the current parse unit then
176 * we can be pretty sure that we have a valid parse unit */
177 if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
178 !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
179 pu.next_pu_offset != pu1.prev_pu_offset ||
180 pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
183 *buf_size = next - 9;
184 pc->header_bytes_needed = 9;
188 /* All non-frame data must be accompanied by frame data. This is to
189 * ensure that pts is set correctly. So if the current parse unit is
190 * not frame data, wait for frame data to come along */
192 pc->dirac_unit = pc->buffer + pc->index - 13 -
193 pu1.prev_pu_offset - pc->dirac_unit_size;
195 pc->dirac_unit_size += pu.next_pu_offset;
197 if ((pu.pu_type & 0x08) != 0x08) {
198 pc->header_bytes_needed = 9;
203 /* Get the picture number to set the pts and dts*/
204 if (parse_timing_info && pu1.prev_pu_offset >= 13) {
205 uint8_t *cur_pu = pc->buffer +
206 pc->index - 13 - pu1.prev_pu_offset;
207 int pts = AV_RB32(cur_pu + 13);
208 if (s->last_pts == 0 && s->last_dts == 0)
211 s->dts = s->last_dts + 1;
213 if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
214 avctx->has_b_frames = 1;
216 if (avctx->has_b_frames && s->pts == s->dts)
217 s->pict_type = AV_PICTURE_TYPE_B;
219 /* Finally have a complete Dirac data unit */
220 *buf = pc->dirac_unit;
221 *buf_size = pc->dirac_unit_size;
223 pc->dirac_unit_size = 0;
224 pc->overread_index = pc->index - 13;
225 pc->header_bytes_needed = 9;
230 static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
231 const uint8_t **poutbuf, int *poutbuf_size,
232 const uint8_t *buf, int buf_size)
234 DiracParseContext *pc = s->priv_data;
240 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
243 *poutbuf_size = buf_size;
244 /* Assume that data has been packetized into an encapsulation unit. */
246 next = find_frame_end(pc, buf, buf_size);
247 if (!pc->is_synced && next == -1)
248 /* No frame start found yet. So throw away the entire buffer. */
251 if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
256 *poutbuf_size = buf_size;
260 static void dirac_parse_close(AVCodecParserContext *s)
262 DiracParseContext *pc = s->priv_data;
264 if (pc->buffer_size > 0)
265 av_freep(&pc->buffer);
268 AVCodecParser ff_dirac_parser = {
269 .codec_ids = { AV_CODEC_ID_DIRAC },
270 .priv_data_size = sizeof(DiracParseContext),
271 .parser_parse = dirac_parse,
272 .parser_close = dirac_parse_close,