8424cf50 |
/*
* Audio and Video frame extraction |
406792e7 |
* Copyright (c) 2003 Fabrice Bellard
* Copyright (c) 2003 Michael Niedermayer |
8424cf50 |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
8424cf50 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
8424cf50 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
8424cf50 |
* 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 |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
8424cf50 |
*/ |
1fddeecd |
|
8f8bc923 |
#include <stdint.h> |
1d9c2dc8 |
#include <string.h>
|
08f6fdc3 |
#include "libavutil/atomic.h" |
1d9c2dc8 |
#include "libavutil/mem.h" |
8424cf50 |
|
a1c69965 |
#include "parser.h"
|
e6df765e |
static AVCodecParser *av_first_parser = NULL; |
8424cf50 |
|
53abe324 |
AVCodecParser *av_parser_next(const AVCodecParser *p) |
a1c69965 |
{
if (p)
return p->next;
else
return av_first_parser; |
55b9e69a |
}
|
8424cf50 |
void av_register_codec_parser(AVCodecParser *parser)
{ |
08f6fdc3 |
do {
parser->next = av_first_parser;
} while (parser->next != avpriv_atomic_ptr_cas((void * volatile *)&av_first_parser, parser->next, parser)); |
8424cf50 |
}
AVCodecParserContext *av_parser_init(int codec_id)
{ |
92947c6d |
AVCodecParserContext *s = NULL; |
8424cf50 |
AVCodecParser *parser;
int ret; |
115329f1 |
|
a1c69965 |
if (codec_id == AV_CODEC_ID_NONE) |
8845e427 |
return NULL; |
8424cf50 |
|
81a663f4 |
for (parser = av_first_parser; parser; parser = parser->next) { |
8424cf50 |
if (parser->codec_ids[0] == codec_id ||
parser->codec_ids[1] == codec_id || |
99f06236 |
parser->codec_ids[2] == codec_id ||
parser->codec_ids[3] == codec_id ||
parser->codec_ids[4] == codec_id) |
8424cf50 |
goto found;
}
return NULL; |
a1c69965 |
found: |
8424cf50 |
s = av_mallocz(sizeof(AVCodecParserContext));
if (!s) |
92947c6d |
goto err_out; |
8424cf50 |
s->parser = parser; |
85ff3394 |
s->priv_data = av_mallocz(parser->priv_data_size); |
92947c6d |
if (!s->priv_data)
goto err_out; |
f8353d5f |
s->fetch_timestamp=1;
s->pict_type = AV_PICTURE_TYPE_I; |
8424cf50 |
if (parser->parser_init) {
ret = parser->parser_init(s); |
92947c6d |
if (ret != 0)
goto err_out; |
8424cf50 |
} |
a1c69965 |
s->key_frame = -1; |
c01694c8 |
s->convergence_duration = 0; |
27ca0a79 |
s->dts_sync_point = INT_MIN;
s->dts_ref_dts_delta = INT_MIN;
s->pts_dts_delta = INT_MIN; |
8424cf50 |
return s; |
92947c6d |
err_out:
if (s)
av_freep(&s->priv_data);
av_free(s);
return NULL; |
8424cf50 |
}
|
a1c69965 |
void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove)
{ |
cd614494 |
int i; |
ebdd7377 |
|
a1c69965 |
s->dts =
s->pts = AV_NOPTS_VALUE;
s->pos = -1;
s->offset = 0;
for (i = 0; i < AV_PARSER_PTS_NB; i++) {
if (s->cur_offset + off >= s->cur_frame_offset[i] &&
(s->frame_offset < s->cur_frame_offset[i] || |
bdadf05e |
(!s->frame_offset && !s->next_frame_offset)) && // first field/frame
// check disabled since MPEG-TS does not send complete PES packets
/*s->next_frame_offset + off <*/ s->cur_frame_end[i]){
|
a1c69965 |
s->dts = s->cur_frame_dts[i];
s->pts = s->cur_frame_pts[i];
s->pos = s->cur_frame_pos[i]; |
cd614494 |
s->offset = s->next_frame_offset - s->cur_frame_offset[i]; |
a1c69965 |
if (remove)
s->cur_frame_offset[i] = INT64_MAX;
if (s->cur_offset + off < s->cur_frame_end[i]) |
2cc30421 |
break; |
cd614494 |
}
}
}
|
a1c69965 |
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, |
b283ba26 |
uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size, |
a1c69965 |
int64_t pts, int64_t dts, int64_t pos) |
b283ba26 |
{ |
47917785 |
int index, i; |
6e45e928 |
uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; |
115329f1 |
|
a1c69965 |
if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) { |
59cb40b9 |
s->next_frame_offset =
s->cur_offset = pos; |
a1c69965 |
s->flags |= PARSER_FLAG_FETCHED_OFFSET; |
59cb40b9 |
}
|
6e45e928 |
if (buf_size == 0) {
/* padding is always necessary even if EOF, so we add it here */
memset(dummy_buf, 0, sizeof(dummy_buf));
buf = dummy_buf; |
a1c69965 |
} else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */ |
b84f2a35 |
/* add a new packet descriptor */ |
a1c69965 |
i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
s->cur_frame_start_index = i;
s->cur_frame_offset[i] = s->cur_offset;
s->cur_frame_end[i] = s->cur_offset + buf_size;
s->cur_frame_pts[i] = pts;
s->cur_frame_dts[i] = dts;
s->cur_frame_pos[i] = pos; |
cb63a4b3 |
} |
b84f2a35 |
|
a1c69965 |
if (s->fetch_timestamp) {
s->fetch_timestamp = 0;
s->last_pts = s->pts;
s->last_dts = s->dts;
s->last_pos = s->pos; |
ec0c5d48 |
ff_fetch_timestamp(s, 0, 0); |
6e45e928 |
} |
8424cf50 |
/* WARNING: the returned index can be negative */ |
a1c69965 |
index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
poutbuf_size, buf, buf_size); |
8424cf50 |
/* update the file pointer */
if (*poutbuf_size) { |
b84f2a35 |
/* fill the data for the current frame */ |
26f23725 |
s->frame_offset = s->next_frame_offset; |
115329f1 |
|
b84f2a35 |
/* offset of the next frame */ |
26f23725 |
s->next_frame_offset = s->cur_offset + index; |
a1c69965 |
s->fetch_timestamp = 1; |
8424cf50 |
}
if (index < 0)
index = 0;
s->cur_offset += index;
return index;
}
|
a1c69965 |
int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx, |
115329f1 |
uint8_t **poutbuf, int *poutbuf_size, |
a1c69965 |
const uint8_t *buf, int buf_size, int keyframe)
{
if (s && s->parser->split) { |
4ec33648 |
if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER ||
avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) { |
a1c69965 |
int i = s->parser->split(avctx, buf, buf_size);
buf += i; |
90ad92b3 |
buf_size -= i;
}
}
|
79396ac6 |
/* cast to avoid warning about discarding qualifiers */ |
a1c69965 |
*poutbuf = (uint8_t *) buf;
*poutbuf_size = buf_size;
if (avctx->extradata) { |
4ec33648 |
if (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) { |
a1c69965 |
int size = buf_size + avctx->extradata_size;
*poutbuf_size = size;
*poutbuf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); |
115329f1 |
|
90ad92b3 |
memcpy(*poutbuf, avctx->extradata, avctx->extradata_size); |
4ec33648 |
memcpy(*poutbuf + avctx->extradata_size, buf, |
a1c69965 |
buf_size + FF_INPUT_BUFFER_PADDING_SIZE); |
90ad92b3 |
return 1;
}
}
return 0;
}
|
8424cf50 |
void av_parser_close(AVCodecParserContext *s)
{ |
a1c69965 |
if (s) { |
0393cf15 |
if (s->parser->parser_close) |
87b6ea84 |
s->parser->parser_close(s); |
cc769931 |
av_freep(&s->priv_data); |
87b6ea84 |
av_free(s); |
99c1c388 |
} |
8424cf50 |
}
|
a1c69965 |
int ff_combine_frame(ParseContext *pc, int next,
const uint8_t **buf, int *buf_size) |
8424cf50 |
{ |
a1c69965 |
if (pc->overread) { |
94bf9ac4 |
av_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n", |
aebb56e1 |
pc->overread, pc->state, next, pc->index, pc->overread_index); |
a1c69965 |
av_dlog(NULL, "%X %X %X %X\n",
(*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]); |
8424cf50 |
}
|
e42dba48 |
/* Copy overread bytes from last frame into buffer. */ |
a1c69965 |
for (; pc->overread > 0; pc->overread--)
pc->buffer[pc->index++] = pc->buffer[pc->overread_index++]; |
f48c0551 |
/* flush remaining if EOF */ |
a1c69965 |
if (!*buf_size && next == END_NOT_FOUND)
next = 0; |
f48c0551 |
|
a1c69965 |
pc->last_index = pc->index; |
8424cf50 |
/* copy into buffer end return */ |
a1c69965 |
if (next == END_NOT_FOUND) {
void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, |
4ec33648 |
*buf_size + pc->index + |
a1c69965 |
FF_INPUT_BUFFER_PADDING_SIZE); |
8424cf50 |
|
72d580f8 |
if (!new_buffer) { |
f31011e9 |
pc->index = 0; |
1f96bafb |
return AVERROR(ENOMEM); |
f31011e9 |
} |
1f96bafb |
pc->buffer = new_buffer; |
8424cf50 |
memcpy(&pc->buffer[pc->index], *buf, *buf_size);
pc->index += *buf_size;
return -1;
}
|
a1c69965 |
*buf_size =
pc->overread_index = pc->index + next; |
115329f1 |
|
8424cf50 |
/* append to buffer */ |
a1c69965 |
if (pc->index) {
void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
next + pc->index +
FF_INPUT_BUFFER_PADDING_SIZE); |
72d580f8 |
if (!new_buffer) { |
f31011e9 |
pc->overread_index =
pc->index = 0; |
1f96bafb |
return AVERROR(ENOMEM); |
f31011e9 |
} |
1f96bafb |
pc->buffer = new_buffer; |
096abfa1 |
if (next > -FF_INPUT_BUFFER_PADDING_SIZE)
memcpy(&pc->buffer[pc->index], *buf,
next + FF_INPUT_BUFFER_PADDING_SIZE); |
8424cf50 |
pc->index = 0; |
a1c69965 |
*buf = pc->buffer; |
8424cf50 |
}
/* store overread bytes */ |
a1c69965 |
for (; next < 0; next++) { |
4ec33648 |
pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next]; |
8424cf50 |
pc->overread++;
}
|
a1c69965 |
if (pc->overread) { |
94bf9ac4 |
av_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n", |
09cbf60f |
pc->overread, pc->state, next, pc->index, pc->overread_index); |
a1c69965 |
av_dlog(NULL, "%X %X %X %X\n",
(*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]); |
8424cf50 |
}
return 0;
}
|
e4cb187d |
void ff_parse_close(AVCodecParserContext *s) |
8424cf50 |
{ |
e4cb187d |
ParseContext *pc = s->priv_data; |
8424cf50 |
|
c8714ea1 |
av_freep(&pc->buffer); |
8424cf50 |
}
|
a1c69965 |
int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size) |
90ad92b3 |
{
int i; |
a1c69965 |
uint32_t state = -1; |
115329f1 |
|
a1c69965 |
for (i = 0; i < buf_size; i++) { |
4ec33648 |
state = state << 8 | buf[i]; |
a1c69965 |
if (state == 0x1B3 || state == 0x1B6)
return i - 3; |
90ad92b3 |
}
return 0;
} |