2008-05-03 03:33:21 +00:00
|
|
|
/*
|
|
|
|
* Dirac parser
|
|
|
|
*
|
2008-12-22 00:01:39 +00:00
|
|
|
* Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org>
|
|
|
|
* Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com>
|
2008-05-03 03:33:21 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2008-05-03 03:33:21 +00:00
|
|
|
* Dirac Parser
|
|
|
|
* @author Marco Gerards <marco@gnu.org>
|
|
|
|
*/
|
|
|
|
|
2012-08-06 13:49:32 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-01-11 22:19:48 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/mem.h"
|
2014-09-08 22:32:34 +00:00
|
|
|
|
2008-05-03 03:33:21 +00:00
|
|
|
#include "parser.h"
|
|
|
|
|
|
|
|
#define DIRAC_PARSE_INFO_PREFIX 0x42424344
|
|
|
|
|
|
|
|
/**
|
2010-06-30 15:38:06 +00:00
|
|
|
* Find the end of the current frame in the bitstream.
|
2008-05-03 03:33:21 +00:00
|
|
|
* @return the position of the first byte of the next frame or -1
|
|
|
|
*/
|
2008-12-22 00:01:39 +00:00
|
|
|
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)
|
2008-05-03 03:33:21 +00:00
|
|
|
{
|
|
|
|
uint32_t state = pc->state;
|
2008-12-22 00:01:39 +00:00
|
|
|
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;
|
2008-05-03 03:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-22 00:01:39 +00:00
|
|
|
if (pc->is_synced) {
|
|
|
|
pc->sync_offset = 0;
|
|
|
|
for (; i < buf_size; i++) {
|
|
|
|
if (state == DIRAC_PARSE_INFO_PREFIX) {
|
2014-09-08 22:32:34 +00:00
|
|
|
if ((buf_size - i) >= pc->header_bytes_needed) {
|
2008-12-22 00:01:39 +00:00
|
|
|
pc->state = -1;
|
|
|
|
return i + pc->header_bytes_needed;
|
|
|
|
} else {
|
2014-09-08 22:32:34 +00:00
|
|
|
pc->header_bytes_needed = 9 - (buf_size - i);
|
2008-12-22 00:01:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2014-09-08 22:32:34 +00:00
|
|
|
state = (state << 8) | buf[i];
|
2008-12-22 00:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-03 03:33:21 +00:00
|
|
|
pc->state = state;
|
2008-12-22 00:01:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-08 22:32:34 +00:00
|
|
|
typedef struct DiracParseUnit {
|
2008-12-22 00:01:39 +00:00
|
|
|
int next_pu_offset;
|
|
|
|
int prev_pu_offset;
|
|
|
|
uint8_t pu_type;
|
|
|
|
} DiracParseUnit;
|
|
|
|
|
|
|
|
static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
|
|
|
|
int offset)
|
|
|
|
{
|
2016-01-21 15:14:10 +00:00
|
|
|
int i;
|
2015-12-05 16:11:54 +00:00
|
|
|
int8_t *start;
|
2016-01-21 15:14:10 +00:00
|
|
|
static const uint8_t valid_pu_types[] = {
|
2016-01-21 16:43:58 +00:00
|
|
|
0x00, 0x10, 0x20, 0x30, 0x08, 0x48, 0xC8, 0xE8, 0x0A, 0x0C, 0x0D, 0x0E,
|
|
|
|
0x4C, 0x09, 0xCC, 0x88, 0xCB
|
2016-01-21 15:14:10 +00:00
|
|
|
};
|
2015-12-05 16:11:54 +00:00
|
|
|
|
|
|
|
if (offset < 0 || pc->index - 13 < offset)
|
2008-12-22 00:01:39 +00:00
|
|
|
return 0;
|
2015-12-05 16:11:54 +00:00
|
|
|
|
|
|
|
start = pc->buffer + offset;
|
2008-12-22 00:01:39 +00:00
|
|
|
pu->pu_type = start[4];
|
|
|
|
|
2014-09-08 22:32:34 +00:00
|
|
|
pu->next_pu_offset = AV_RB32(start + 5);
|
|
|
|
pu->prev_pu_offset = AV_RB32(start + 9);
|
2008-12-22 00:01:39 +00:00
|
|
|
|
2016-01-21 15:14:10 +00:00
|
|
|
/* Check for valid parse code */
|
2016-01-21 16:43:58 +00:00
|
|
|
for (i = 0; i < 17; i++)
|
2016-01-21 15:14:10 +00:00
|
|
|
if (valid_pu_types[i] == pu->pu_type)
|
|
|
|
break;
|
2016-01-21 16:43:58 +00:00
|
|
|
if (i == 17)
|
2016-01-21 15:14:10 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-01-21 16:43:58 +00:00
|
|
|
if (pu->pu_type == 0x10 && pu->next_pu_offset == 0x00)
|
2016-01-21 15:14:10 +00:00
|
|
|
pu->next_pu_offset = 13; /* The length of a parse info header */
|
2008-12-22 00:01:39 +00:00
|
|
|
|
2016-01-21 15:14:10 +00:00
|
|
|
/* Check if the parse offsets are somewhat sane */
|
|
|
|
if ((pu->next_pu_offset && pu->next_pu_offset < 13) ||
|
|
|
|
(pu->prev_pu_offset && pu->prev_pu_offset < 13))
|
2015-12-05 16:14:36 +00:00
|
|
|
return 0;
|
|
|
|
|
2008-12-22 00:01:39 +00:00
|
|
|
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) {
|
2015-11-06 20:58:42 +00:00
|
|
|
memmove(pc->buffer, pc->buffer + pc->overread_index,
|
2008-12-22 00:01:39 +00:00
|
|
|
pc->index - pc->overread_index);
|
2014-09-08 22:32:34 +00:00
|
|
|
pc->index -= pc->overread_index;
|
2008-12-22 00:01:39 +00:00
|
|
|
pc->overread_index = 0;
|
|
|
|
if (*buf_size == 0 && pc->buffer[4] == 0x10) {
|
|
|
|
*buf = pc->buffer;
|
|
|
|
*buf_size = pc->index;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-08 22:32:34 +00:00
|
|
|
if (next == -1) {
|
2008-12-22 00:01:39 +00:00
|
|
|
/* Found a possible frame start but not a frame end */
|
2014-09-08 22:32:34 +00:00
|
|
|
void *new_buffer =
|
|
|
|
av_fast_realloc(pc->buffer, &pc->buffer_size,
|
|
|
|
pc->index + (*buf_size - pc->sync_offset));
|
2014-10-02 07:31:41 +00:00
|
|
|
if (!new_buffer)
|
|
|
|
return AVERROR(ENOMEM);
|
2008-12-22 00:01:39 +00:00
|
|
|
pc->buffer = new_buffer;
|
2014-09-08 22:32:34 +00:00
|
|
|
memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
|
2008-12-22 00:01:39 +00:00
|
|
|
*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);
|
2014-10-02 07:31:41 +00:00
|
|
|
if (!new_buffer)
|
|
|
|
return AVERROR(ENOMEM);
|
2008-12-22 00:01:39 +00:00
|
|
|
pc->buffer = new_buffer;
|
|
|
|
memcpy(pc->buffer + pc->index, *buf, next);
|
|
|
|
pc->index += next;
|
2008-05-03 03:33:21 +00:00
|
|
|
|
2008-12-22 00:01:39 +00:00
|
|
|
/* 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) ||
|
2012-12-15 15:03:19 +00:00
|
|
|
pu.next_pu_offset != pu1.prev_pu_offset ||
|
|
|
|
pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
|
|
|
|
) {
|
2014-09-08 22:32:34 +00:00
|
|
|
pc->index -= 9;
|
|
|
|
*buf_size = next - 9;
|
2008-12-22 00:01:39 +00:00
|
|
|
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;
|
|
|
|
|
2014-09-08 22:32:34 +00:00
|
|
|
if ((pu.pu_type & 0x08) != 0x08) {
|
2008-12-22 00:01:39 +00:00
|
|
|
pc->header_bytes_needed = 9;
|
2014-09-08 22:32:34 +00:00
|
|
|
*buf_size = next;
|
2008-12-22 00:01:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the picture number to set the pts and dts*/
|
2015-12-05 16:15:38 +00:00
|
|
|
if (parse_timing_info && pu1.prev_pu_offset >= 13) {
|
2008-12-22 00:01:39 +00:00
|
|
|
uint8_t *cur_pu = pc->buffer +
|
|
|
|
pc->index - 13 - pu1.prev_pu_offset;
|
2019-07-11 21:23:07 +00:00
|
|
|
int64_t pts = AV_RB32(cur_pu + 13);
|
2008-12-22 00:01:39 +00:00
|
|
|
if (s->last_pts == 0 && s->last_dts == 0)
|
|
|
|
s->dts = pts - 1;
|
2020-10-23 18:29:50 +00:00
|
|
|
else if (s->last_dts != AV_NOPTS_VALUE)
|
2014-09-08 22:32:34 +00:00
|
|
|
s->dts = s->last_dts + 1;
|
2008-12-22 00:01:39 +00:00
|
|
|
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)
|
2014-09-08 22:32:34 +00:00
|
|
|
s->pict_type = AV_PICTURE_TYPE_B;
|
2008-12-22 00:01:39 +00:00
|
|
|
|
|
|
|
/* Finally have a complete Dirac data unit */
|
|
|
|
*buf = pc->dirac_unit;
|
|
|
|
*buf_size = pc->dirac_unit_size;
|
|
|
|
|
|
|
|
pc->dirac_unit_size = 0;
|
2014-09-08 22:32:34 +00:00
|
|
|
pc->overread_index = pc->index - 13;
|
2008-12-22 00:01:39 +00:00
|
|
|
pc->header_bytes_needed = 9;
|
|
|
|
}
|
|
|
|
return next;
|
2008-05-03 03:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
|
|
|
|
const uint8_t **poutbuf, int *poutbuf_size,
|
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
2008-12-22 00:01:39 +00:00
|
|
|
DiracParseContext *pc = s->priv_data;
|
2008-05-03 03:33:21 +00:00
|
|
|
int next;
|
|
|
|
|
2014-09-08 22:32:34 +00:00
|
|
|
*poutbuf = NULL;
|
2008-12-22 00:01:39 +00:00
|
|
|
*poutbuf_size = 0;
|
|
|
|
|
2008-05-03 03:33:21 +00:00
|
|
|
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
|
2014-09-08 22:32:34 +00:00
|
|
|
next = buf_size;
|
|
|
|
*poutbuf = buf;
|
2008-12-22 00:01:39 +00:00
|
|
|
*poutbuf_size = buf_size;
|
|
|
|
/* Assume that data has been packetized into an encapsulation unit. */
|
|
|
|
} else {
|
2008-05-03 03:33:21 +00:00
|
|
|
next = find_frame_end(pc, buf, buf_size);
|
2014-09-08 22:32:34 +00:00
|
|
|
if (!pc->is_synced && next == -1)
|
2008-12-22 00:01:39 +00:00
|
|
|
/* No frame start found yet. So throw away the entire buffer. */
|
|
|
|
return buf_size;
|
2008-05-03 03:33:21 +00:00
|
|
|
|
2014-09-08 22:32:34 +00:00
|
|
|
if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
|
2008-05-03 03:33:21 +00:00
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2014-09-08 22:32:34 +00:00
|
|
|
*poutbuf = buf;
|
2008-05-03 03:33:21 +00:00
|
|
|
*poutbuf_size = buf_size;
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2008-12-22 00:01:39 +00:00
|
|
|
static void dirac_parse_close(AVCodecParserContext *s)
|
|
|
|
{
|
|
|
|
DiracParseContext *pc = s->priv_data;
|
|
|
|
|
|
|
|
if (pc->buffer_size > 0)
|
2014-10-11 20:50:51 +00:00
|
|
|
av_freep(&pc->buffer);
|
2008-12-22 00:01:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 23:20:43 +00:00
|
|
|
const AVCodecParser ff_dirac_parser = {
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_ids = { AV_CODEC_ID_DIRAC },
|
2011-11-02 08:34:41 +00:00
|
|
|
.priv_data_size = sizeof(DiracParseContext),
|
|
|
|
.parser_parse = dirac_parse,
|
|
|
|
.parser_close = dirac_parse_close,
|
2008-05-03 03:33:21 +00:00
|
|
|
};
|