#ifdef CONFIG_FFSERVER
#include <unistd.h>
-offset_t ffm_read_write_index(int fd)
+int64_t ffm_read_write_index(int fd)
{
uint8_t buf[8];
return AV_RB64(buf);
}
-void ffm_write_write_index(int fd, offset_t pos)
+void ffm_write_write_index(int fd, int64_t pos)
{
uint8_t buf[8];
int i;
write(fd, buf, 8);
}
-void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
+void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size)
{
FFMContext *ffm = s->priv_data;
ffm->write_index = pos;
static int ffm_is_avail_data(AVFormatContext *s, int size)
{
FFMContext *ffm = s->priv_data;
- offset_t pos, avail_size;
+ int64_t pos, avail_size;
int len;
len = ffm->packet_end - ffm->packet_ptr;
retry_read:
get_be16(pb); /* PACKET_ID */
fill_size = get_be16(pb);
- ffm->pts = get_be64(pb);
- ffm->first_frame_in_packet = 1;
+ ffm->dts = get_be64(pb);
frame_offset = get_be16(pb);
get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
/* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
by the write position inside this function */
-static void ffm_seek1(AVFormatContext *s, offset_t pos1)
+static void ffm_seek1(AVFormatContext *s, int64_t pos1)
{
FFMContext *ffm = s->priv_data;
ByteIOContext *pb = s->pb;
- offset_t pos;
+ int64_t pos;
pos = pos1 + ffm->write_index;
if (pos >= ffm->file_size)
url_fseek(pb, pos, SEEK_SET);
}
-static int64_t get_pts(AVFormatContext *s, offset_t pos)
+static int64_t get_dts(AVFormatContext *s, int64_t pos)
{
ByteIOContext *pb = s->pb;
- int64_t pts;
+ int64_t dts;
ffm_seek1(s, pos);
url_fskip(pb, 4);
- pts = get_be64(pb);
+ dts = get_be64(pb);
#ifdef DEBUG_SEEK
av_log(s, AV_LOG_DEBUG, "pts=%0.6f\n", pts / 1000000.0);
#endif
- return pts;
+ return dts;
}
static void adjust_write_index(AVFormatContext *s)
FFMContext *ffm = s->priv_data;
ByteIOContext *pb = s->pb;
int64_t pts;
- //offset_t orig_write_index = ffm->write_index;
- offset_t pos_min, pos_max;
+ //int64_t orig_write_index = ffm->write_index;
+ int64_t pos_min, pos_max;
int64_t pts_start;
- offset_t ptr = url_ftell(pb);
+ int64_t ptr = url_ftell(pb);
pos_min = 0;
pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
- pts_start = get_pts(s, pos_min);
+ pts_start = get_dts(s, pos_min);
- pts = get_pts(s, pos_max);
+ pts = get_dts(s, pos_max);
if (pts - 100000 > pts_start)
goto end;
ffm->write_index = FFM_PACKET_SIZE;
- pts_start = get_pts(s, pos_min);
+ pts_start = get_dts(s, pos_min);
- pts = get_pts(s, pos_max);
+ pts = get_dts(s, pos_max);
if (pts - 100000 <= pts_start) {
while (1) {
- offset_t newpos;
+ int64_t newpos;
int64_t newpts;
newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
if (newpos == pos_min)
break;
- newpts = get_pts(s, newpos);
+ newpts = get_dts(s, newpos);
if (newpts - 100000 <= pts) {
pos_max = newpos;
}
//printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
- //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
+ //printf("pts range %0.6f - %0.6f\n", get_dts(s, 0) / 1000000. , get_dts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
end:
url_fseek(pb, ptr, SEEK_SET);
codec->frame_skip_cmp = get_be32(pb);
codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
codec->codec_tag = get_be32(pb);
+ codec->thread_count = get_byte(pb);
break;
case CODEC_TYPE_AUDIO:
codec->sample_rate = get_be32(pb);
default:
goto fail;
}
-
+ if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
+ codec->extradata_size = get_be32(pb);
+ codec->extradata = av_malloc(codec->extradata_size);
+ if (!codec->extradata)
+ return AVERROR(ENOMEM);
+ get_buffer(pb, codec->extradata, codec->extradata_size);
+ }
}
/* get until end of block reached */
ffm->packet_ptr = ffm->packet;
ffm->packet_end = ffm->packet;
ffm->frame_offset = 0;
- ffm->pts = 0;
+ ffm->dts = 0;
ffm->read_state = READ_HEADER;
ffm->first_packet = 1;
return 0;
for(i=0;i<s->nb_streams;i++) {
st = s->streams[i];
if (st) {
- av_freep(&st->priv_data);
av_free(st);
}
}
switch(ffm->read_state) {
case READ_HEADER:
- if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
+ if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) {
return AVERROR(EAGAIN);
}
dprintf(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
- url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
+ url_ftell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
FRAME_HEADER_SIZE)
return AVERROR(EAGAIN);
+ if (ffm->header[1] & FLAG_DTS)
+ if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
+ return AVERROR(EAGAIN);
#if 0
av_hexdump_log(s, AV_LOG_DEBUG, ffm->header, FRAME_HEADER_SIZE);
#endif
av_free_packet(pkt);
return AVERROR(EAGAIN);
}
- if (ffm->first_frame_in_packet)
- {
- pkt->pts = ffm->pts;
- ffm->first_frame_in_packet = 0;
- }
+ pkt->pts = AV_RB64(ffm->header+8);
+ if (ffm->header[1] & FLAG_DTS)
+ pkt->dts = pkt->pts - AV_RB32(ffm->header+16);
+ else
+ pkt->dts = pkt->pts;
pkt->duration = duration;
break;
}
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
{
FFMContext *ffm = s->priv_data;
- offset_t pos_min, pos_max, pos;
+ int64_t pos_min, pos_max, pos;
int64_t pts_min, pts_max, pts;
double pos1;
pos_min = 0;
pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
while (pos_min <= pos_max) {
- pts_min = get_pts(s, pos_min);
- pts_max = get_pts(s, pos_max);
+ pts_min = get_dts(s, pos_min);
+ pts_max = get_dts(s, pos_max);
/* linear interpolation */
pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
(double)(pts_max - pts_min);
pos = pos_min;
else if (pos >= pos_max)
pos = pos_max;
- pts = get_pts(s, pos);
+ pts = get_dts(s, pos);
/* check if we are lucky */
if (pts == wanted_pts) {
goto found;
return 0;
}
-static int ffm_read_close(AVFormatContext *s)
-{
- AVStream *st;
- int i;
-
- for(i=0;i<s->nb_streams;i++) {
- st = s->streams[i];
- av_freep(&st->priv_data);
- }
- return 0;
-}
-
static int ffm_probe(AVProbeData *p)
{
if (
AVInputFormat ffm_demuxer = {
"ffm",
- NULL_IF_CONFIG_SMALL("ffm format"),
+ NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
sizeof(FFMContext),
ffm_probe,
ffm_read_header,
ffm_read_packet,
- ffm_read_close,
+ NULL,
ffm_seek,
};