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
25 * @file dirac_parser.c
27 * @author Marco Gerards <marco@gnu.org>
32 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
35 * Finds the end of the current frame in the bitstream.
36 * @return the position of the first byte of the next frame or -1
38 typedef struct DiracParseContext {
42 int header_bytes_needed;
51 static int find_frame_end(DiracParseContext *pc,
52 const uint8_t *buf, int buf_size)
54 uint32_t state = pc->state;
58 for (i = 0; i < buf_size; i++) {
59 state = (state << 8) | buf[i];
60 if (state == DIRAC_PARSE_INFO_PREFIX) {
63 pc->header_bytes_needed = 9;
72 for (; i < buf_size; i++) {
73 if (state == DIRAC_PARSE_INFO_PREFIX) {
74 if ((buf_size-i) >= pc->header_bytes_needed) {
76 return i + pc->header_bytes_needed;
78 pc->header_bytes_needed = 9-(buf_size-i);
82 state = (state << 8) | buf[i];
89 typedef struct DiracParseUnit
96 static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
99 uint8_t *start = pc->buffer + offset;
100 uint8_t *end = pc->buffer + pc->index;
101 if (start < pc->buffer || (start+13 > end))
103 pu->pu_type = start[4];
105 pu->next_pu_offset = AV_RB32(start+5);
106 pu->prev_pu_offset = AV_RB32(start+9);
108 if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
109 pu->next_pu_offset = 13;
114 static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
115 int next, const uint8_t **buf, int *buf_size)
117 int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
118 s->dts == AV_NOPTS_VALUE);
119 DiracParseContext *pc = s->priv_data;
121 if (pc->overread_index) {
122 memcpy(pc->buffer, pc->buffer + pc->overread_index,
123 pc->index - pc->overread_index);
124 pc->index -= pc->overread_index;
125 pc->overread_index = 0;
126 if (*buf_size == 0 && pc->buffer[4] == 0x10) {
128 *buf_size = pc->index;
134 /* Found a possible frame start but not a frame end */
135 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
136 pc->index + (*buf_size -
138 pc->buffer = new_buffer;
139 memcpy(pc->buffer+pc->index, (*buf + pc->sync_offset),
140 *buf_size - pc->sync_offset);
141 pc->index += *buf_size - pc->sync_offset;
144 /* Found a possible frame start and a possible frame end */
145 DiracParseUnit pu1, pu;
146 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
148 pc->buffer = new_buffer;
149 memcpy(pc->buffer + pc->index, *buf, next);
152 /* Need to check if we have a valid Parse Unit. We can't go by the
153 * sync pattern 'BBCD' alone because arithmetic coding of the residual
154 * and motion data can cause the pattern triggering a false start of
155 * frame. So check if the previous parse offset of the next parse unit
156 * is equal to the next parse offset of the current parse unit then
157 * we can be pretty sure that we have a valid parse unit */
158 if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
159 !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
160 pu.next_pu_offset != pu1.prev_pu_offset) {
163 pc->header_bytes_needed = 9;
167 /* All non-frame data must be accompanied by frame data. This is to
168 * ensure that pts is set correctly. So if the current parse unit is
169 * not frame data, wait for frame data to come along */
171 pc->dirac_unit = pc->buffer + pc->index - 13 -
172 pu1.prev_pu_offset - pc->dirac_unit_size;
174 pc->dirac_unit_size += pu.next_pu_offset;
176 if ((pu.pu_type&0x08) != 0x08) {
177 pc->header_bytes_needed = 9;
182 /* Get the picture number to set the pts and dts*/
183 if (parse_timing_info) {
184 uint8_t *cur_pu = pc->buffer +
185 pc->index - 13 - pu1.prev_pu_offset;
186 int pts = AV_RB32(cur_pu + 13);
187 if (s->last_pts == 0 && s->last_dts == 0)
190 s->dts = s->last_dts+1;
192 if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
193 avctx->has_b_frames = 1;
195 if (avctx->has_b_frames && s->pts == s->dts)
196 s->pict_type = FF_B_TYPE;
198 /* Finally have a complete Dirac data unit */
199 *buf = pc->dirac_unit;
200 *buf_size = pc->dirac_unit_size;
202 pc->dirac_unit_size = 0;
203 pc->overread_index = pc->index-13;
204 pc->header_bytes_needed = 9;
209 static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
210 const uint8_t **poutbuf, int *poutbuf_size,
211 const uint8_t *buf, int buf_size)
213 DiracParseContext *pc = s->priv_data;
219 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
222 *poutbuf_size = buf_size;
223 /* Assume that data has been packetized into an encapsulation unit. */
225 next = find_frame_end(pc, buf, buf_size);
226 if (!pc->is_synced && next == -1) {
227 /* No frame start found yet. So throw away the entire buffer. */
231 if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0) {
237 *poutbuf_size = buf_size;
241 static void dirac_parse_close(AVCodecParserContext *s)
243 DiracParseContext *pc = s->priv_data;
245 if (pc->buffer_size > 0)
249 AVCodecParser dirac_parser = {
251 sizeof(DiracParseContext),