aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnuradha Suraparaju <anuradha@rd.bbc.co.uk>2008-12-22 00:01:39 +0000
committerDiego Biurrun <diego@biurrun.de>2008-12-22 00:01:39 +0000
commit64bfc5845bd4361d91f5bb7a7d76f416ee357ab6 (patch)
tree0a92947f69d310a3546c47efcc324c1ecfd6d13d
parentc4ff7c53e0135c3e0e65506f2ec95e1eeac6f069 (diff)
downloadffmpeg-64bfc5845bd4361d91f5bb7a7d76f416ee357ab6.tar.gz
Fix incorrectly constructed Dirac parse units that caused A/V sync loss.
Fixes issue 694. patch by Anuradha Suraparaju, anuradha rd.bbc.co uk Originally committed as revision 16261 to svn://svn.ffmpeg.org/ffmpeg/trunk
-rw-r--r--libavcodec/dirac_parser.c205
-rw-r--r--libavcodec/libdiracdec.c6
-rw-r--r--libavcodec/libschroedingerdec.c3
3 files changed, 193 insertions, 21 deletions
diff --git a/libavcodec/dirac_parser.c b/libavcodec/dirac_parser.c
index 199354a9c6..ac82dcabf1 100644
--- a/libavcodec/dirac_parser.c
+++ b/libavcodec/dirac_parser.c
@@ -1,7 +1,8 @@
/*
* Dirac parser
*
- * Copyright (c) 2007 Marco Gerards <marco@gnu.org>
+ * Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org>
+ * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com>
*
* This file is part of FFmpeg.
*
@@ -34,42 +35,200 @@
* Finds the end of the current frame in the bitstream.
* @return the position of the first byte of the next frame or -1
*/
-static int find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
+typedef struct DiracParseContext {
+ int state;
+ int is_synced;
+ int sync_offset;
+ int header_bytes_needed;
+ int overread_index;
+ int buffer_size;
+ int index;
+ uint8_t *buffer;
+ int dirac_unit_size;
+ uint8_t *dirac_unit;
+} DiracParseContext;
+
+static int find_frame_end(DiracParseContext *pc,
+ const uint8_t *buf, int buf_size)
{
uint32_t state = pc->state;
- int i;
-
- for (i = 0; i < buf_size; i++) {
- state = (state << 8) | buf[i];
- if (state == DIRAC_PARSE_INFO_PREFIX) {
- pc->frame_start_found ^= 1;
- if (!pc->frame_start_found) {
- pc->state = -1;
- return i - 3;
+ int i = 0;
+
+ if (!pc->is_synced) {
+ for (i = 0; i < buf_size; i++) {
+ state = (state << 8) | buf[i];
+ if (state == DIRAC_PARSE_INFO_PREFIX) {
+ state = -1;
+ pc->is_synced = 1;
+ pc->header_bytes_needed = 9;
+ pc->sync_offset = i;
+ break;
}
}
}
+ if (pc->is_synced) {
+ pc->sync_offset = 0;
+ for (; i < buf_size; i++) {
+ if (state == DIRAC_PARSE_INFO_PREFIX) {
+ if ((buf_size-i) >= pc->header_bytes_needed) {
+ pc->state = -1;
+ return i + pc->header_bytes_needed;
+ } else {
+ pc->header_bytes_needed = 9-(buf_size-i);
+ break;
+ }
+ } else
+ state = (state << 8) | buf[i];
+ }
+ }
pc->state = state;
+ return -1;
+}
+
+typedef struct DiracParseUnit
+{
+ int next_pu_offset;
+ int prev_pu_offset;
+ uint8_t pu_type;
+} DiracParseUnit;
+
+static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
+ int offset)
+{
+ uint8_t *start = pc->buffer + offset;
+ uint8_t *end = pc->buffer + pc->index;
+ if (start < pc->buffer || (start+13 > end))
+ return 0;
+ pu->pu_type = start[4];
+
+ pu->next_pu_offset = AV_RB32(start+5);
+ pu->prev_pu_offset = AV_RB32(start+9);
+
+ if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
+ pu->next_pu_offset = 13;
+
+ return 1;
+}
+
+static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
+ int next, const uint8_t **buf, int *buf_size)
+{
+ int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
+ s->dts == AV_NOPTS_VALUE);
+ DiracParseContext *pc = s->priv_data;
+
+ if (pc->overread_index) {
+ memcpy(pc->buffer, pc->buffer + pc->overread_index,
+ pc->index - pc->overread_index);
+ pc->index -= pc->overread_index;
+ pc->overread_index = 0;
+ if (*buf_size == 0 && pc->buffer[4] == 0x10) {
+ *buf = pc->buffer;
+ *buf_size = pc->index;
+ return 0;
+ }
+ }
+
+ if ( next == -1) {
+ /* Found a possible frame start but not a frame end */
+ void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
+ pc->index + (*buf_size -
+ pc->sync_offset));
+ pc->buffer = new_buffer;
+ memcpy(pc->buffer+pc->index, (*buf + pc->sync_offset),
+ *buf_size - pc->sync_offset);
+ pc->index += *buf_size - pc->sync_offset;
+ return -1;
+ } else {
+ /* Found a possible frame start and a possible frame end */
+ DiracParseUnit pu1, pu;
+ void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
+ pc->index + next);
+ pc->buffer = new_buffer;
+ memcpy(pc->buffer + pc->index, *buf, next);
+ pc->index += next;
- return END_NOT_FOUND;
+ /* Need to check if we have a valid Parse Unit. We can't go by the
+ * sync pattern 'BBCD' alone because arithmetic coding of the residual
+ * and motion data can cause the pattern triggering a false start of
+ * frame. So check if the previous parse offset of the next parse unit
+ * is equal to the next parse offset of the current parse unit then
+ * we can be pretty sure that we have a valid parse unit */
+ if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
+ !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
+ pu.next_pu_offset != pu1.prev_pu_offset) {
+ pc->index -= 9;
+ *buf_size = next-9;
+ pc->header_bytes_needed = 9;
+ return -1;
+ }
+
+ /* All non-frame data must be accompanied by frame data. This is to
+ * ensure that pts is set correctly. So if the current parse unit is
+ * not frame data, wait for frame data to come along */
+
+ pc->dirac_unit = pc->buffer + pc->index - 13 -
+ pu1.prev_pu_offset - pc->dirac_unit_size;
+
+ pc->dirac_unit_size += pu.next_pu_offset;
+
+ if ((pu.pu_type&0x08) != 0x08) {
+ pc->header_bytes_needed = 9;
+ *buf_size = next;
+ return -1;
+ }
+
+ /* Get the picture number to set the pts and dts*/
+ if (parse_timing_info) {
+ uint8_t *cur_pu = pc->buffer +
+ pc->index - 13 - pu1.prev_pu_offset;
+ int pts = AV_RB32(cur_pu + 13);
+ if (s->last_pts == 0 && s->last_dts == 0)
+ s->dts = pts - 1;
+ else
+ s->dts = s->last_dts+1;
+ s->pts = pts;
+ if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
+ avctx->has_b_frames = 1;
+ }
+ if (avctx->has_b_frames && s->pts == s->dts)
+ s->pict_type = FF_B_TYPE;
+
+ /* Finally have a complete Dirac data unit */
+ *buf = pc->dirac_unit;
+ *buf_size = pc->dirac_unit_size;
+
+ pc->dirac_unit_size = 0;
+ pc->overread_index = pc->index-13;
+ pc->header_bytes_needed = 9;
+ }
+ return next;
}
static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
- ParseContext *pc = s->priv_data;
+ DiracParseContext *pc = s->priv_data;
int next;
+ *poutbuf = NULL;
+ *poutbuf_size = 0;
+
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
next = buf_size;
- }else{
+ *poutbuf = buf;
+ *poutbuf_size = buf_size;
+ /* Assume that data has been packetized into an encapsulation unit. */
+ } else {
next = find_frame_end(pc, buf, buf_size);
+ if (!pc->is_synced && next == -1) {
+ /* No frame start found yet. So throw away the entire buffer. */
+ return buf_size;
+ }
- if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
- *poutbuf = NULL;
- *poutbuf_size = 0;
+ if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0) {
return buf_size;
}
}
@@ -79,10 +238,18 @@ static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
return next;
}
+static void dirac_parse_close(AVCodecParserContext *s)
+{
+ DiracParseContext *pc = s->priv_data;
+
+ if (pc->buffer_size > 0)
+ av_free(pc->buffer);
+}
+
AVCodecParser dirac_parser = {
{ CODEC_ID_DIRAC },
- sizeof(ParseContext),
+ sizeof(DiracParseContext),
NULL,
dirac_parse,
- ff_parse_close,
+ dirac_parse_close,
};
diff --git a/libavcodec/libdiracdec.c b/libavcodec/libdiracdec.c
index 06830f76d9..6c6e46708c 100644
--- a/libavcodec/libdiracdec.c
+++ b/libavcodec/libdiracdec.c
@@ -88,10 +88,12 @@ static int libdirac_decode_frame(AVCodecContext *avccontext,
*data_size = 0;
- if (buf_size>0)
+ if (buf_size>0) {
/* set data to decode into buffer */
dirac_buffer (p_dirac_params->p_decoder, buf, buf+buf_size);
-
+ if ((buf[4] &0x08) == 0x08 && (buf[4] & 0x03))
+ avccontext->has_b_frames = 1;
+ }
while (1) {
/* parse data and process result */
DecoderState state = dirac_parse (p_dirac_params->p_decoder);
diff --git a/libavcodec/libschroedingerdec.c b/libavcodec/libschroedingerdec.c
index f9b4d2198a..36cba01d36 100644
--- a/libavcodec/libschroedingerdec.c
+++ b/libavcodec/libschroedingerdec.c
@@ -235,6 +235,9 @@ static int libschroedinger_decode_frame(AVCodecContext *avccontext,
do {
if ((enc_buf = FfmpegFindNextSchroParseUnit(&parse_ctx))) {
/* Push buffer into decoder. */
+ if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
+ SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
+ avccontext->has_b_frames = 1;
state = schro_decoder_push (decoder, enc_buf);
if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
libschroedinger_handle_first_access_unit(avccontext);