2001-07-22 17:18:56 +03:00
|
|
|
/*
|
2008-05-26 06:44:31 +03:00
|
|
|
* FFM (ffserver live feed) demuxer
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2002-05-26 01:31:48 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2001-07-22 17:18:56 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 01:31:48 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2002-05-26 01:31:48 +03:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 17:18:56 +03:00
|
|
|
*/
|
2008-05-26 06:44:31 +03:00
|
|
|
|
2013-11-23 23:32:55 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-08-18 02:45:35 +02:00
|
|
|
#include "libavutil/internal.h"
|
2009-01-12 00:19:48 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-11-27 17:04:16 +03:00
|
|
|
#include "libavutil/intfloat.h"
|
2014-11-11 00:22:59 +02:00
|
|
|
#include "libavutil/opt.h"
|
2014-11-16 22:53:59 +02:00
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2016-04-28 22:50:48 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2001-07-22 17:18:56 +03:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2008-05-26 06:44:31 +03:00
|
|
|
#include "ffm.h"
|
2012-01-28 06:23:26 +03:00
|
|
|
#include "avio_internal.h"
|
2008-05-26 06:52:14 +03:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
static int ffm_is_avail_data(AVFormatContext *s, int size)
|
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
2008-10-03 13:16:29 +03:00
|
|
|
int64_t pos, avail_size;
|
2016-01-02 17:32:09 +02:00
|
|
|
ptrdiff_t len;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
len = ffm->packet_end - ffm->packet_ptr;
|
2008-05-26 06:37:01 +03:00
|
|
|
if (size <= len)
|
|
|
|
return 1;
|
2011-03-03 21:11:45 +02:00
|
|
|
pos = avio_tell(s->pb);
|
2009-03-20 03:23:33 +02:00
|
|
|
if (!ffm->write_index) {
|
2009-03-20 03:39:55 +02:00
|
|
|
if (pos == ffm->file_size)
|
2009-03-20 03:23:33 +02:00
|
|
|
return AVERROR_EOF;
|
|
|
|
avail_size = ffm->file_size - pos;
|
|
|
|
} else {
|
2001-07-22 17:18:56 +03:00
|
|
|
if (pos == ffm->write_index) {
|
|
|
|
/* exactly at the end of stream */
|
2015-11-20 04:10:11 +02:00
|
|
|
if (ffm->server_attached)
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
else
|
|
|
|
return AVERROR_INVALIDDATA;
|
2001-07-22 17:18:56 +03:00
|
|
|
} else if (pos < ffm->write_index) {
|
|
|
|
avail_size = ffm->write_index - pos;
|
|
|
|
} else {
|
|
|
|
avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
|
|
|
|
}
|
2009-03-20 03:23:33 +02:00
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
|
|
|
|
if (size <= avail_size)
|
|
|
|
return 1;
|
2015-11-20 04:10:11 +02:00
|
|
|
else if (ffm->server_attached)
|
2009-03-20 03:23:33 +02:00
|
|
|
return AVERROR(EAGAIN);
|
2015-11-20 04:10:11 +02:00
|
|
|
else
|
|
|
|
return AVERROR_INVALIDDATA;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2016-01-02 17:27:43 +02:00
|
|
|
static int ffm_resync(AVFormatContext *s, uint32_t state)
|
2009-03-20 03:44:45 +02:00
|
|
|
{
|
|
|
|
av_log(s, AV_LOG_ERROR, "resyncing\n");
|
|
|
|
while (state != PACKET_ID) {
|
2014-08-07 23:12:41 +03:00
|
|
|
if (avio_feof(s->pb)) {
|
2009-03-20 03:44:45 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "cannot find FFM syncword\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
state = (state << 8) | avio_r8(s->pb);
|
2009-03-20 03:44:45 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/* first is true if we read the frame header */
|
2002-04-08 23:58:56 +03:00
|
|
|
static int ffm_read_data(AVFormatContext *s,
|
2008-06-06 23:36:16 +03:00
|
|
|
uint8_t *buf, int size, int header)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2016-01-02 17:32:09 +02:00
|
|
|
int fill_size, size1, frame_offset;
|
2016-01-02 17:27:43 +02:00
|
|
|
uint32_t id;
|
2016-01-02 17:32:09 +02:00
|
|
|
ptrdiff_t len;
|
2015-03-09 15:59:44 +02:00
|
|
|
int64_t last_pos = -1;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
size1 = size;
|
|
|
|
while (size > 0) {
|
|
|
|
redo:
|
|
|
|
len = ffm->packet_end - ffm->packet_ptr;
|
2008-05-26 06:53:10 +03:00
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
2001-07-22 17:18:56 +03:00
|
|
|
if (len > size)
|
|
|
|
len = size;
|
|
|
|
if (len == 0) {
|
2016-01-03 16:47:23 +02:00
|
|
|
if (avio_tell(pb) == ffm->file_size) {
|
|
|
|
if (ffm->server_attached) {
|
2015-12-02 23:25:55 +02:00
|
|
|
avio_seek(pb, ffm->packet_size, SEEK_SET);
|
2016-01-03 16:47:23 +02:00
|
|
|
} else
|
2015-12-02 23:25:55 +02:00
|
|
|
return AVERROR_EOF;
|
2016-01-03 16:47:23 +02:00
|
|
|
}
|
2002-05-19 00:30:16 +03:00
|
|
|
retry_read:
|
2011-09-08 21:23:13 +03:00
|
|
|
if (pb->buffer_size != ffm->packet_size) {
|
|
|
|
int64_t tell = avio_tell(pb);
|
2015-06-07 14:55:10 +02:00
|
|
|
int ret = ffio_set_buf_size(pb, ffm->packet_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-09-08 21:23:13 +03:00
|
|
|
avio_seek(pb, tell, SEEK_SET);
|
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
id = avio_rb16(pb); /* PACKET_ID */
|
2015-03-09 15:59:44 +02:00
|
|
|
if (id != PACKET_ID) {
|
2009-03-20 03:44:45 +02:00
|
|
|
if (ffm_resync(s, id) < 0)
|
|
|
|
return -1;
|
2015-03-09 15:59:44 +02:00
|
|
|
last_pos = avio_tell(pb);
|
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
fill_size = avio_rb16(pb);
|
|
|
|
ffm->dts = avio_rb64(pb);
|
|
|
|
frame_offset = avio_rb16(pb);
|
|
|
|
avio_read(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
|
2016-01-02 17:27:02 +02:00
|
|
|
if (ffm->packet_size < FFM_HEADER_SIZE + fill_size || frame_offset < 0) {
|
2004-10-01 16:16:16 +03:00
|
|
|
return -1;
|
2016-01-02 17:27:02 +02:00
|
|
|
}
|
|
|
|
ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
|
2001-07-22 17:18:56 +03:00
|
|
|
/* if first packet or resynchronization packet, we must
|
|
|
|
handle it specifically */
|
|
|
|
if (ffm->first_packet || (frame_offset & 0x8000)) {
|
2002-05-19 00:30:16 +03:00
|
|
|
if (!frame_offset) {
|
|
|
|
/* This packet has no frame headers in it */
|
2012-10-17 21:36:30 +03:00
|
|
|
if (avio_tell(pb) >= ffm->packet_size * 3LL) {
|
2015-03-09 15:59:44 +02:00
|
|
|
int64_t seekback = FFMIN(ffm->packet_size * 2LL, avio_tell(pb) - last_pos);
|
|
|
|
seekback = FFMAX(seekback, 0);
|
|
|
|
avio_seek(pb, -seekback, SEEK_CUR);
|
2002-05-19 00:30:16 +03:00
|
|
|
goto retry_read;
|
|
|
|
}
|
|
|
|
/* This is bad, we cannot find a valid frame header */
|
|
|
|
return 0;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
ffm->first_packet = 0;
|
2016-01-02 17:27:02 +02:00
|
|
|
if ((frame_offset & 0x7fff) < FFM_HEADER_SIZE) {
|
|
|
|
ffm->packet_end = ffm->packet_ptr;
|
2004-10-01 16:16:16 +03:00
|
|
|
return -1;
|
2016-01-02 17:27:02 +02:00
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
|
2008-06-06 23:36:16 +03:00
|
|
|
if (!header)
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ffm->packet_ptr = ffm->packet;
|
|
|
|
}
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
memcpy(buf, ffm->packet_ptr, len);
|
|
|
|
buf += len;
|
|
|
|
ffm->packet_ptr += len;
|
|
|
|
size -= len;
|
2008-06-06 23:36:16 +03:00
|
|
|
header = 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
return size1 - size;
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:08:26 +02:00
|
|
|
/* ensure that actual seeking happens between FFM_PACKET_SIZE
|
2009-03-21 09:35:52 +02:00
|
|
|
and file_size - FFM_PACKET_SIZE */
|
2011-09-14 20:24:55 +03:00
|
|
|
static int64_t ffm_seek1(AVFormatContext *s, int64_t pos1)
|
2008-05-26 06:50:25 +03:00
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2008-10-03 13:16:29 +03:00
|
|
|
int64_t pos;
|
2008-05-26 06:50:25 +03:00
|
|
|
|
2009-03-21 09:35:52 +02:00
|
|
|
pos = FFMIN(pos1, ffm->file_size - FFM_PACKET_SIZE);
|
|
|
|
pos = FFMAX(pos, FFM_PACKET_SIZE);
|
2015-08-18 02:45:35 +02:00
|
|
|
ff_dlog(s, "seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
|
2011-09-14 20:24:55 +03:00
|
|
|
return avio_seek(pb, pos, SEEK_SET);
|
2008-05-26 06:50:25 +03:00
|
|
|
}
|
|
|
|
|
2008-10-03 13:16:29 +03:00
|
|
|
static int64_t get_dts(AVFormatContext *s, int64_t pos)
|
2008-05-26 06:47:07 +03:00
|
|
|
{
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2008-06-22 11:53:44 +03:00
|
|
|
int64_t dts;
|
2008-05-26 06:47:07 +03:00
|
|
|
|
|
|
|
ffm_seek1(s, pos);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 4);
|
2011-02-21 17:43:01 +02:00
|
|
|
dts = avio_rb64(pb);
|
2015-08-18 02:45:35 +02:00
|
|
|
ff_dlog(s, "dts=%0.6f\n", dts / 1000000.0);
|
2008-06-22 11:53:44 +03:00
|
|
|
return dts;
|
2008-05-26 06:47:07 +03:00
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2003-11-19 04:34:40 +02:00
|
|
|
static void adjust_write_index(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2003-11-19 04:34:40 +02:00
|
|
|
int64_t pts;
|
2008-10-03 13:16:29 +03:00
|
|
|
//int64_t orig_write_index = ffm->write_index;
|
|
|
|
int64_t pos_min, pos_max;
|
2003-11-19 04:34:40 +02:00
|
|
|
int64_t pts_start;
|
2011-03-03 21:11:45 +02:00
|
|
|
int64_t ptr = avio_tell(pb);
|
2003-11-19 04:34:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
pos_min = 0;
|
|
|
|
pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
|
|
|
|
|
2008-06-22 11:53:44 +03:00
|
|
|
pts_start = get_dts(s, pos_min);
|
2003-11-19 04:34:40 +02:00
|
|
|
|
2008-06-22 11:53:44 +03:00
|
|
|
pts = get_dts(s, pos_max);
|
2003-11-19 04:34:40 +02:00
|
|
|
|
2005-12-17 20:14:38 +02:00
|
|
|
if (pts - 100000 > pts_start)
|
2004-03-18 22:52:40 +02:00
|
|
|
goto end;
|
2003-11-19 04:34:40 +02:00
|
|
|
|
|
|
|
ffm->write_index = FFM_PACKET_SIZE;
|
|
|
|
|
2008-06-22 11:53:44 +03:00
|
|
|
pts_start = get_dts(s, pos_min);
|
2003-11-19 04:34:40 +02:00
|
|
|
|
2008-06-22 11:53:44 +03:00
|
|
|
pts = get_dts(s, pos_max);
|
2003-11-19 04:34:40 +02:00
|
|
|
|
|
|
|
if (pts - 100000 <= pts_start) {
|
|
|
|
while (1) {
|
2008-10-03 13:16:29 +03:00
|
|
|
int64_t newpos;
|
2003-11-19 04:34:40 +02:00
|
|
|
int64_t newpts;
|
|
|
|
|
|
|
|
newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
|
|
|
|
|
|
|
|
if (newpos == pos_min)
|
|
|
|
break;
|
|
|
|
|
2008-06-22 11:53:44 +03:00
|
|
|
newpts = get_dts(s, newpos);
|
2003-11-19 04:34:40 +02:00
|
|
|
|
|
|
|
if (newpts - 100000 <= pts) {
|
|
|
|
pos_max = newpos;
|
|
|
|
pts = newpts;
|
|
|
|
} else {
|
|
|
|
pos_min = newpos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ffm->write_index += pos_max;
|
|
|
|
}
|
|
|
|
|
2004-03-18 22:52:40 +02:00
|
|
|
end:
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, ptr, SEEK_SET);
|
2003-11-19 04:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-25 10:54:33 +03:00
|
|
|
static int ffm_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_streams; i++)
|
|
|
|
av_freep(&s->streams[i]->codec->rc_eq);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-16 22:53:59 +02:00
|
|
|
static int ffm_append_recommended_configuration(AVStream *st, char **conf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t newsize;
|
|
|
|
av_assert0(conf && st);
|
|
|
|
if (!*conf)
|
|
|
|
return 0;
|
|
|
|
if (!st->recommended_encoder_configuration) {
|
|
|
|
st->recommended_encoder_configuration = *conf;
|
|
|
|
*conf = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
newsize = strlen(*conf) + strlen(st->recommended_encoder_configuration) + 2;
|
|
|
|
if ((ret = av_reallocp(&st->recommended_encoder_configuration, newsize)) < 0)
|
|
|
|
return ret;
|
|
|
|
av_strlcat(st->recommended_encoder_configuration, ",", newsize);
|
|
|
|
av_strlcat(st->recommended_encoder_configuration, *conf, newsize);
|
|
|
|
av_freep(conf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-09 04:49:40 +03:00
|
|
|
static int ffm2_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
|
|
|
AVStream *st;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
AVCodecContext *codec;
|
2015-12-14 23:11:55 +02:00
|
|
|
const AVCodecDescriptor *codec_desc;
|
2016-04-10 21:58:15 +02:00
|
|
|
int ret, i;
|
2015-03-08 23:52:47 +02:00
|
|
|
int f_main = 0, f_cprv = -1, f_stvi = -1, f_stau = -1;
|
2014-11-11 00:22:59 +02:00
|
|
|
AVCodec *enc;
|
2014-11-11 09:20:02 +02:00
|
|
|
char *buffer;
|
2012-10-09 04:49:40 +03:00
|
|
|
|
|
|
|
ffm->packet_size = avio_rb32(pb);
|
2014-09-01 15:04:54 +03:00
|
|
|
if (ffm->packet_size != FFM_PACKET_SIZE) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid packet size %d, expected size was %d\n",
|
|
|
|
ffm->packet_size, FFM_PACKET_SIZE);
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
2012-10-09 04:49:40 +03:00
|
|
|
goto fail;
|
2014-09-01 15:04:54 +03:00
|
|
|
}
|
|
|
|
|
2012-10-09 04:49:40 +03:00
|
|
|
ffm->write_index = avio_rb64(pb);
|
|
|
|
/* get also filesize */
|
|
|
|
if (pb->seekable) {
|
|
|
|
ffm->file_size = avio_size(pb);
|
|
|
|
if (ffm->write_index && 0)
|
|
|
|
adjust_write_index(s);
|
|
|
|
} else {
|
|
|
|
ffm->file_size = (UINT64_C(1) << 63) - 1;
|
|
|
|
}
|
|
|
|
|
2014-08-07 23:12:41 +03:00
|
|
|
while(!avio_feof(pb)) {
|
2012-10-09 04:49:40 +03:00
|
|
|
unsigned id = avio_rb32(pb);
|
|
|
|
unsigned size = avio_rb32(pb);
|
|
|
|
int64_t next = avio_tell(pb) + size;
|
|
|
|
char rc_eq_buf[128];
|
|
|
|
|
|
|
|
if(!id)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch(id) {
|
|
|
|
case MKBETAG('M', 'A', 'I', 'N'):
|
2014-11-11 00:22:59 +02:00
|
|
|
if (f_main++) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-10-09 04:49:40 +03:00
|
|
|
avio_rb32(pb); /* nb_streams */
|
|
|
|
avio_rb32(pb); /* total bitrate */
|
|
|
|
break;
|
|
|
|
case MKBETAG('C', 'O', 'M', 'M'):
|
2014-11-11 00:22:59 +02:00
|
|
|
f_cprv = f_stvi = f_stau = 0;
|
2012-10-09 04:49:40 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2014-09-01 15:04:54 +03:00
|
|
|
if (!st) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
2012-10-09 04:49:40 +03:00
|
|
|
goto fail;
|
2014-09-01 15:04:54 +03:00
|
|
|
}
|
2012-10-09 04:49:40 +03:00
|
|
|
|
|
|
|
avpriv_set_pts_info(st, 64, 1, 1000000);
|
|
|
|
|
|
|
|
codec = st->codec;
|
|
|
|
/* generic info */
|
|
|
|
codec->codec_id = avio_rb32(pb);
|
2015-12-14 23:11:55 +02:00
|
|
|
codec_desc = avcodec_descriptor_get(codec->codec_id);
|
|
|
|
if (!codec_desc) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
|
|
|
|
codec->codec_id = AV_CODEC_ID_NONE;
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-10-09 04:49:40 +03:00
|
|
|
codec->codec_type = avio_r8(pb);
|
2015-12-14 23:11:55 +02:00
|
|
|
if (codec->codec_type != codec_desc->type) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
|
|
|
|
codec_desc->type, codec->codec_type);
|
|
|
|
codec->codec_id = AV_CODEC_ID_NONE;
|
|
|
|
codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-10-09 04:49:40 +03:00
|
|
|
codec->bit_rate = avio_rb32(pb);
|
|
|
|
codec->flags = avio_rb32(pb);
|
|
|
|
codec->flags2 = avio_rb32(pb);
|
|
|
|
codec->debug = avio_rb32(pb);
|
2015-07-27 21:14:31 +02:00
|
|
|
if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
|
2016-04-10 21:58:15 +02:00
|
|
|
int size = avio_rb32(pb);
|
|
|
|
codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!codec->extradata)
|
2012-10-09 04:49:40 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2016-04-10 21:58:15 +02:00
|
|
|
codec->extradata_size = size;
|
|
|
|
avio_read(pb, codec->extradata, size);
|
2012-10-09 04:49:40 +03:00
|
|
|
}
|
2014-11-11 00:22:59 +02:00
|
|
|
break;
|
2014-11-16 02:57:01 +02:00
|
|
|
case MKBETAG('S', 'T', 'V', 'I'):
|
|
|
|
if (f_stvi++) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
codec->time_base.num = avio_rb32(pb);
|
|
|
|
codec->time_base.den = avio_rb32(pb);
|
2015-03-09 00:12:59 +02:00
|
|
|
if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
|
|
|
|
codec->time_base.num, codec->time_base.den);
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-11-16 02:57:01 +02:00
|
|
|
codec->width = avio_rb16(pb);
|
|
|
|
codec->height = avio_rb16(pb);
|
|
|
|
codec->gop_size = avio_rb16(pb);
|
|
|
|
codec->pix_fmt = avio_rb32(pb);
|
2016-04-28 22:50:48 +02:00
|
|
|
if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
|
|
|
|
codec->pix_fmt = AV_PIX_FMT_NONE;
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-11-16 02:57:01 +02:00
|
|
|
codec->qmin = avio_r8(pb);
|
|
|
|
codec->qmax = avio_r8(pb);
|
|
|
|
codec->max_qdiff = avio_r8(pb);
|
|
|
|
codec->qcompress = avio_rb16(pb) / 10000.0;
|
|
|
|
codec->qblur = avio_rb16(pb) / 10000.0;
|
|
|
|
codec->bit_rate_tolerance = avio_rb32(pb);
|
|
|
|
avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
|
|
|
|
codec->rc_eq = av_strdup(rc_eq_buf);
|
|
|
|
codec->rc_max_rate = avio_rb32(pb);
|
|
|
|
codec->rc_min_rate = avio_rb32(pb);
|
|
|
|
codec->rc_buffer_size = avio_rb32(pb);
|
|
|
|
codec->i_quant_factor = av_int2double(avio_rb64(pb));
|
|
|
|
codec->b_quant_factor = av_int2double(avio_rb64(pb));
|
|
|
|
codec->i_quant_offset = av_int2double(avio_rb64(pb));
|
|
|
|
codec->b_quant_offset = av_int2double(avio_rb64(pb));
|
|
|
|
codec->dct_algo = avio_rb32(pb);
|
|
|
|
codec->strict_std_compliance = avio_rb32(pb);
|
|
|
|
codec->max_b_frames = avio_rb32(pb);
|
|
|
|
codec->mpeg_quant = avio_rb32(pb);
|
|
|
|
codec->intra_dc_precision = avio_rb32(pb);
|
|
|
|
codec->me_method = avio_rb32(pb);
|
|
|
|
codec->mb_decision = avio_rb32(pb);
|
|
|
|
codec->nsse_weight = avio_rb32(pb);
|
|
|
|
codec->frame_skip_cmp = avio_rb32(pb);
|
|
|
|
codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
|
|
|
|
codec->codec_tag = avio_rb32(pb);
|
|
|
|
codec->thread_count = avio_r8(pb);
|
|
|
|
codec->coder_type = avio_rb32(pb);
|
|
|
|
codec->me_cmp = avio_rb32(pb);
|
|
|
|
codec->me_subpel_quality = avio_rb32(pb);
|
|
|
|
codec->me_range = avio_rb32(pb);
|
|
|
|
codec->keyint_min = avio_rb32(pb);
|
|
|
|
codec->scenechange_threshold = avio_rb32(pb);
|
|
|
|
codec->b_frame_strategy = avio_rb32(pb);
|
|
|
|
codec->qcompress = av_int2double(avio_rb64(pb));
|
|
|
|
codec->qblur = av_int2double(avio_rb64(pb));
|
|
|
|
codec->max_qdiff = avio_rb32(pb);
|
|
|
|
codec->refs = avio_rb32(pb);
|
|
|
|
break;
|
|
|
|
case MKBETAG('S', 'T', 'A', 'U'):
|
|
|
|
if (f_stau++) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
codec->sample_rate = avio_rb32(pb);
|
|
|
|
codec->channels = avio_rl16(pb);
|
|
|
|
codec->frame_size = avio_rl16(pb);
|
|
|
|
break;
|
2014-11-11 00:22:59 +02:00
|
|
|
case MKBETAG('C', 'P', 'R', 'V'):
|
|
|
|
if (f_cprv++) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
enc = avcodec_find_encoder(codec->codec_id);
|
|
|
|
if (enc && enc->priv_data_size && enc->priv_class) {
|
2014-11-16 22:53:59 +02:00
|
|
|
buffer = av_malloc(size + 1);
|
|
|
|
if (!buffer) {
|
2014-11-11 00:22:59 +02:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-11-16 22:53:59 +02:00
|
|
|
avio_get_str(pb, size, buffer, size + 1);
|
|
|
|
if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
|
|
|
|
goto fail;
|
2012-10-09 04:49:40 +03:00
|
|
|
}
|
|
|
|
break;
|
2014-11-11 09:20:02 +02:00
|
|
|
case MKBETAG('S', '2', 'V', 'I'):
|
2015-12-02 23:47:12 +02:00
|
|
|
if (f_stvi++ || !size) {
|
2014-11-11 09:20:02 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
buffer = av_malloc(size);
|
|
|
|
if (!buffer) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
avio_get_str(pb, INT_MAX, buffer, size);
|
|
|
|
av_set_options_string(codec, buffer, "=", ",");
|
2014-11-16 22:53:59 +02:00
|
|
|
if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
|
|
|
|
goto fail;
|
2014-11-11 09:20:02 +02:00
|
|
|
break;
|
|
|
|
case MKBETAG('S', '2', 'A', 'U'):
|
2015-12-02 23:47:12 +02:00
|
|
|
if (f_stau++ || !size) {
|
2014-11-11 09:20:02 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
buffer = av_malloc(size);
|
|
|
|
if (!buffer) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
avio_get_str(pb, INT_MAX, buffer, size);
|
|
|
|
av_set_options_string(codec, buffer, "=", ",");
|
2015-04-09 00:55:04 +02:00
|
|
|
if ((ret = ffm_append_recommended_configuration(st, &buffer)) < 0)
|
|
|
|
goto fail;
|
2014-11-11 09:20:02 +02:00
|
|
|
break;
|
2012-10-09 04:49:40 +03:00
|
|
|
}
|
|
|
|
avio_seek(pb, next, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
for (i = 0; i < s->nb_streams; i++)
|
|
|
|
avcodec_parameters_from_context(s->streams[i]->codecpar, s->streams[i]->codec);
|
|
|
|
|
2012-10-09 04:49:40 +03:00
|
|
|
/* get until end of block reached */
|
2015-03-09 00:31:48 +02:00
|
|
|
while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
|
2012-10-09 04:49:40 +03:00
|
|
|
avio_r8(pb);
|
|
|
|
|
|
|
|
/* init packet demux */
|
|
|
|
ffm->packet_ptr = ffm->packet;
|
|
|
|
ffm->packet_end = ffm->packet;
|
|
|
|
ffm->frame_offset = 0;
|
|
|
|
ffm->dts = 0;
|
|
|
|
ffm->read_state = READ_HEADER;
|
|
|
|
ffm->first_packet = 1;
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
ffm_close(s);
|
2014-09-01 15:04:54 +03:00
|
|
|
return ret;
|
2012-10-09 04:49:40 +03:00
|
|
|
}
|
2010-07-25 10:52:58 +03:00
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int ffm_read_header(AVFormatContext *s)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
2002-05-20 19:31:13 +03:00
|
|
|
FFMContext *ffm = s->priv_data;
|
2001-07-22 17:18:56 +03:00
|
|
|
AVStream *st;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2001-07-22 17:18:56 +03:00
|
|
|
AVCodecContext *codec;
|
2015-12-14 23:11:55 +02:00
|
|
|
const AVCodecDescriptor *codec_desc;
|
2003-08-08 20:52:30 +03:00
|
|
|
int i, nb_streams;
|
2003-02-11 18:35:48 +02:00
|
|
|
uint32_t tag;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
/* header */
|
2011-02-21 17:43:01 +02:00
|
|
|
tag = avio_rl32(pb);
|
2012-10-09 04:49:40 +03:00
|
|
|
if (tag == MKTAG('F', 'F', 'M', '2'))
|
|
|
|
return ffm2_read_header(s);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (tag != MKTAG('F', 'F', 'M', '1'))
|
|
|
|
goto fail;
|
2011-02-21 17:43:01 +02:00
|
|
|
ffm->packet_size = avio_rb32(pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (ffm->packet_size != FFM_PACKET_SIZE)
|
|
|
|
goto fail;
|
2011-02-21 17:43:01 +02:00
|
|
|
ffm->write_index = avio_rb64(pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
/* get also filesize */
|
2011-03-05 22:06:46 +02:00
|
|
|
if (pb->seekable) {
|
2011-03-04 20:57:36 +02:00
|
|
|
ffm->file_size = avio_size(pb);
|
2012-05-07 03:42:00 +03:00
|
|
|
if (ffm->write_index && 0)
|
2009-03-20 03:23:33 +02:00
|
|
|
adjust_write_index(s);
|
2001-07-22 17:18:56 +03:00
|
|
|
} else {
|
2006-12-07 01:46:11 +02:00
|
|
|
ffm->file_size = (UINT64_C(1) << 63) - 1;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
nb_streams = avio_rb32(pb);
|
|
|
|
avio_rb32(pb); /* total bitrate */
|
2001-07-22 17:18:56 +03:00
|
|
|
/* read each stream */
|
2003-08-08 20:52:30 +03:00
|
|
|
for(i=0;i<nb_streams;i++) {
|
2002-09-12 05:26:58 +03:00
|
|
|
char rc_eq_buf[128];
|
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (!st)
|
|
|
|
goto fail;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 64, 1, 1000000);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2005-07-18 01:24:36 +03:00
|
|
|
codec = st->codec;
|
2001-07-22 17:18:56 +03:00
|
|
|
/* generic info */
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->codec_id = avio_rb32(pb);
|
2015-12-14 23:11:55 +02:00
|
|
|
codec_desc = avcodec_descriptor_get(codec->codec_id);
|
|
|
|
if (!codec_desc) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid codec id: %d\n", codec->codec_id);
|
|
|
|
codec->codec_id = AV_CODEC_ID_NONE;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->codec_type = avio_r8(pb); /* codec_type */
|
2015-12-14 23:11:55 +02:00
|
|
|
if (codec->codec_type != codec_desc->type) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Codec type mismatch: expected %d, found %d\n",
|
|
|
|
codec_desc->type, codec->codec_type);
|
|
|
|
codec->codec_id = AV_CODEC_ID_NONE;
|
|
|
|
codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->bit_rate = avio_rb32(pb);
|
|
|
|
codec->flags = avio_rb32(pb);
|
|
|
|
codec->flags2 = avio_rb32(pb);
|
|
|
|
codec->debug = avio_rb32(pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
/* specific info */
|
|
|
|
switch(codec->codec_type) {
|
2010-03-31 02:30:55 +03:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->time_base.num = avio_rb32(pb);
|
|
|
|
codec->time_base.den = avio_rb32(pb);
|
2015-03-09 00:12:59 +02:00
|
|
|
if (codec->time_base.num <= 0 || codec->time_base.den <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid time base %d/%d\n",
|
|
|
|
codec->time_base.num, codec->time_base.den);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->width = avio_rb16(pb);
|
|
|
|
codec->height = avio_rb16(pb);
|
|
|
|
codec->gop_size = avio_rb16(pb);
|
|
|
|
codec->pix_fmt = avio_rb32(pb);
|
2016-04-28 22:50:48 +02:00
|
|
|
if (!av_pix_fmt_desc_get(codec->pix_fmt)) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid pix fmt id: %d\n", codec->pix_fmt);
|
|
|
|
codec->pix_fmt = AV_PIX_FMT_NONE;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->qmin = avio_r8(pb);
|
|
|
|
codec->qmax = avio_r8(pb);
|
|
|
|
codec->max_qdiff = avio_r8(pb);
|
|
|
|
codec->qcompress = avio_rb16(pb) / 10000.0;
|
|
|
|
codec->qblur = avio_rb16(pb) / 10000.0;
|
|
|
|
codec->bit_rate_tolerance = avio_rb32(pb);
|
2012-01-23 21:49:25 +03:00
|
|
|
avio_get_str(pb, INT_MAX, rc_eq_buf, sizeof(rc_eq_buf));
|
|
|
|
codec->rc_eq = av_strdup(rc_eq_buf);
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->rc_max_rate = avio_rb32(pb);
|
|
|
|
codec->rc_min_rate = avio_rb32(pb);
|
|
|
|
codec->rc_buffer_size = avio_rb32(pb);
|
2011-11-27 17:04:16 +03:00
|
|
|
codec->i_quant_factor = av_int2double(avio_rb64(pb));
|
|
|
|
codec->b_quant_factor = av_int2double(avio_rb64(pb));
|
|
|
|
codec->i_quant_offset = av_int2double(avio_rb64(pb));
|
|
|
|
codec->b_quant_offset = av_int2double(avio_rb64(pb));
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->dct_algo = avio_rb32(pb);
|
|
|
|
codec->strict_std_compliance = avio_rb32(pb);
|
|
|
|
codec->max_b_frames = avio_rb32(pb);
|
|
|
|
codec->mpeg_quant = avio_rb32(pb);
|
|
|
|
codec->intra_dc_precision = avio_rb32(pb);
|
|
|
|
codec->me_method = avio_rb32(pb);
|
|
|
|
codec->mb_decision = avio_rb32(pb);
|
|
|
|
codec->nsse_weight = avio_rb32(pb);
|
|
|
|
codec->frame_skip_cmp = avio_rb32(pb);
|
2011-11-27 17:04:16 +03:00
|
|
|
codec->rc_buffer_aggressivity = av_int2double(avio_rb64(pb));
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->codec_tag = avio_rb32(pb);
|
|
|
|
codec->thread_count = avio_r8(pb);
|
|
|
|
codec->coder_type = avio_rb32(pb);
|
|
|
|
codec->me_cmp = avio_rb32(pb);
|
|
|
|
codec->me_subpel_quality = avio_rb32(pb);
|
|
|
|
codec->me_range = avio_rb32(pb);
|
|
|
|
codec->keyint_min = avio_rb32(pb);
|
|
|
|
codec->scenechange_threshold = avio_rb32(pb);
|
|
|
|
codec->b_frame_strategy = avio_rb32(pb);
|
2011-11-27 17:04:16 +03:00
|
|
|
codec->qcompress = av_int2double(avio_rb64(pb));
|
|
|
|
codec->qblur = av_int2double(avio_rb64(pb));
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->max_qdiff = avio_rb32(pb);
|
|
|
|
codec->refs = avio_rb32(pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
2010-03-31 02:30:55 +03:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2011-02-21 17:43:01 +02:00
|
|
|
codec->sample_rate = avio_rb32(pb);
|
|
|
|
codec->channels = avio_rl16(pb);
|
|
|
|
codec->frame_size = avio_rl16(pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
2002-05-09 04:18:08 +03:00
|
|
|
default:
|
2002-09-12 05:26:58 +03:00
|
|
|
goto fail;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2015-07-27 21:14:31 +02:00
|
|
|
if (codec->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
|
2016-04-10 21:58:15 +02:00
|
|
|
int size = avio_rb32(pb);
|
|
|
|
codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!codec->extradata)
|
2008-06-09 03:36:27 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2016-04-10 21:58:15 +02:00
|
|
|
codec->extradata_size = size;
|
|
|
|
avio_read(pb, codec->extradata, size);
|
2008-06-09 03:36:27 +03:00
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
|
|
|
|
avcodec_parameters_from_context(st->codecpar, codec);
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get until end of block reached */
|
2015-03-09 00:31:48 +02:00
|
|
|
while ((avio_tell(pb) % ffm->packet_size) != 0 && !pb->eof_reached)
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_r8(pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
|
|
|
|
/* init packet demux */
|
|
|
|
ffm->packet_ptr = ffm->packet;
|
|
|
|
ffm->packet_end = ffm->packet;
|
|
|
|
ffm->frame_offset = 0;
|
2008-06-22 11:53:44 +03:00
|
|
|
ffm->dts = 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
ffm->read_state = READ_HEADER;
|
|
|
|
ffm->first_packet = 1;
|
|
|
|
return 0;
|
|
|
|
fail:
|
2010-07-25 10:52:58 +03:00
|
|
|
ffm_close(s);
|
2001-07-22 17:18:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return < 0 if eof */
|
|
|
|
static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
FFMContext *ffm = s->priv_data;
|
2009-03-20 03:23:33 +02:00
|
|
|
int duration, ret;
|
2009-02-02 00:02:57 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
switch(ffm->read_state) {
|
|
|
|
case READ_HEADER:
|
2009-03-20 03:23:33 +02:00
|
|
|
if ((ret = ffm_is_avail_data(s, FRAME_HEADER_SIZE+4)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2015-08-18 02:45:35 +02:00
|
|
|
ff_dlog(s, "pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
|
2011-03-03 21:11:45 +02:00
|
|
|
avio_tell(s->pb), s->pb->pos, ffm->write_index, ffm->file_size);
|
2005-12-17 20:14:38 +02:00
|
|
|
if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
|
2002-05-26 01:31:48 +03:00
|
|
|
FRAME_HEADER_SIZE)
|
2009-03-20 03:23:33 +02:00
|
|
|
return -1;
|
2008-06-09 10:43:14 +03:00
|
|
|
if (ffm->header[1] & FLAG_DTS)
|
|
|
|
if (ffm_read_data(s, ffm->header+16, 4, 1) != 4)
|
2009-03-20 03:23:33 +02:00
|
|
|
return -1;
|
2001-07-22 17:18:56 +03:00
|
|
|
ffm->read_state = READ_DATA;
|
2014-06-03 05:46:04 +03:00
|
|
|
/* fall through */
|
2001-07-22 17:18:56 +03:00
|
|
|
case READ_DATA:
|
2007-07-06 12:32:34 +03:00
|
|
|
size = AV_RB24(ffm->header + 2);
|
2009-03-20 03:23:33 +02:00
|
|
|
if ((ret = ffm_is_avail_data(s, size)) < 0)
|
|
|
|
return ret;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2007-07-06 12:32:34 +03:00
|
|
|
duration = AV_RB24(ffm->header + 5);
|
2002-05-09 04:18:08 +03:00
|
|
|
|
2012-10-17 21:45:59 +03:00
|
|
|
if (av_new_packet(pkt, size) < 0) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
pkt->stream_index = ffm->header[0];
|
2008-05-22 00:04:42 +03:00
|
|
|
if ((unsigned)pkt->stream_index >= s->nb_streams) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
|
2015-10-27 15:35:30 +02:00
|
|
|
av_packet_unref(pkt);
|
2008-05-22 00:04:42 +03:00
|
|
|
ffm->read_state = READ_HEADER;
|
2009-03-20 03:23:33 +02:00
|
|
|
return -1;
|
2008-05-22 00:04:42 +03:00
|
|
|
}
|
2011-03-03 21:11:45 +02:00
|
|
|
pkt->pos = avio_tell(s->pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
if (ffm->header[1] & FLAG_KEY_FRAME)
|
2010-03-31 15:29:58 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2002-04-08 23:58:56 +03:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
ffm->read_state = READ_HEADER;
|
|
|
|
if (ffm_read_data(s, pkt->data, size, 0) != size) {
|
|
|
|
/* bad case: desynchronized packet. we cancel all the packet loading */
|
2015-10-27 15:35:30 +02:00
|
|
|
av_packet_unref(pkt);
|
2009-03-20 03:23:33 +02:00
|
|
|
return -1;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2008-06-09 10:43:14 +03:00
|
|
|
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;
|
2002-05-09 04:18:08 +03:00
|
|
|
pkt->duration = duration;
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* seek to a given time in the file. The file read pointer is
|
2008-01-09 01:08:51 +02:00
|
|
|
positioned at or before pts. XXX: the following code is quite
|
2001-07-22 17:18:56 +03:00
|
|
|
approximative */
|
2004-10-12 02:53:53 +03:00
|
|
|
static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
FFMContext *ffm = s->priv_data;
|
2008-10-03 13:16:29 +03:00
|
|
|
int64_t pos_min, pos_max, pos;
|
2003-02-11 18:35:48 +02:00
|
|
|
int64_t pts_min, pts_max, pts;
|
2001-07-22 17:18:56 +03:00
|
|
|
double pos1;
|
|
|
|
|
2015-08-18 02:45:35 +02:00
|
|
|
ff_dlog(s, "wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
|
2001-07-22 17:18:56 +03:00
|
|
|
/* find the position using linear interpolation (better than
|
|
|
|
dichotomy in typical cases) */
|
2011-09-08 21:36:19 +03:00
|
|
|
if (ffm->write_index && ffm->write_index < ffm->file_size) {
|
2011-09-08 21:09:40 +03:00
|
|
|
if (get_dts(s, FFM_PACKET_SIZE) < wanted_pts) {
|
|
|
|
pos_min = FFM_PACKET_SIZE;
|
|
|
|
pos_max = ffm->write_index - FFM_PACKET_SIZE;
|
|
|
|
} else {
|
|
|
|
pos_min = ffm->write_index;
|
|
|
|
pos_max = ffm->file_size - FFM_PACKET_SIZE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pos_min = FFM_PACKET_SIZE;
|
|
|
|
pos_max = ffm->file_size - FFM_PACKET_SIZE;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
while (pos_min <= pos_max) {
|
2008-06-22 11:53:44 +03:00
|
|
|
pts_min = get_dts(s, pos_min);
|
|
|
|
pts_max = get_dts(s, pos_max);
|
2012-10-17 21:14:43 +03:00
|
|
|
if (pts_min > wanted_pts || pts_max <= wanted_pts) {
|
2011-09-08 21:09:40 +03:00
|
|
|
pos = pts_min > wanted_pts ? pos_min : pos_max;
|
|
|
|
goto found;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
/* linear interpolation */
|
2002-04-08 23:58:56 +03:00
|
|
|
pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
|
2001-07-22 17:18:56 +03:00
|
|
|
(double)(pts_max - pts_min);
|
2003-02-11 18:35:48 +02:00
|
|
|
pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
|
2001-07-22 17:18:56 +03:00
|
|
|
if (pos <= pos_min)
|
|
|
|
pos = pos_min;
|
|
|
|
else if (pos >= pos_max)
|
|
|
|
pos = pos_max;
|
2008-06-22 11:53:44 +03:00
|
|
|
pts = get_dts(s, pos);
|
2001-07-22 17:18:56 +03:00
|
|
|
/* check if we are lucky */
|
|
|
|
if (pts == wanted_pts) {
|
|
|
|
goto found;
|
|
|
|
} else if (pts > wanted_pts) {
|
|
|
|
pos_max = pos - FFM_PACKET_SIZE;
|
|
|
|
} else {
|
|
|
|
pos_min = pos + FFM_PACKET_SIZE;
|
|
|
|
}
|
|
|
|
}
|
2004-10-12 02:53:53 +03:00
|
|
|
pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
|
2009-03-21 09:35:52 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
found:
|
2011-09-14 20:24:55 +03:00
|
|
|
if (ffm_seek1(s, pos) < 0)
|
|
|
|
return -1;
|
2008-06-07 03:35:37 +03:00
|
|
|
|
|
|
|
/* reset read state */
|
|
|
|
ffm->read_state = READ_HEADER;
|
|
|
|
ffm->packet_ptr = ffm->packet;
|
|
|
|
ffm->packet_end = ffm->packet;
|
|
|
|
ffm->first_packet = 1;
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-24 05:06:51 +03:00
|
|
|
static int ffm_probe(AVProbeData *p)
|
|
|
|
{
|
2007-04-08 14:34:15 +03:00
|
|
|
if (
|
2005-12-17 20:14:38 +02:00
|
|
|
p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
|
2012-10-09 04:49:40 +03:00
|
|
|
(p->buf[3] == '1' || p->buf[3] == '2'))
|
2002-05-24 05:06:51 +03:00
|
|
|
return AVPROBE_SCORE_MAX + 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-20 04:08:34 +02:00
|
|
|
static const AVOption options[] = {
|
|
|
|
{"server_attached", NULL, offsetof(FFMContext, server_attached), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_EXPORT },
|
2016-02-20 09:33:49 +02:00
|
|
|
{"ffm_write_index", NULL, offsetof(FFMContext, write_index), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
|
|
|
|
{"ffm_file_size", NULL, offsetof(FFMContext, file_size), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_EXPORT },
|
2015-11-20 04:08:34 +02:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass ffm_class = {
|
|
|
|
.class_name = "ffm demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_ffm_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "ffm",
|
2012-07-31 00:04:28 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
|
2011-07-16 23:18:12 +03:00
|
|
|
.priv_data_size = sizeof(FFMContext),
|
|
|
|
.read_probe = ffm_probe,
|
|
|
|
.read_header = ffm_read_header,
|
|
|
|
.read_packet = ffm_read_packet,
|
|
|
|
.read_close = ffm_close,
|
|
|
|
.read_seek = ffm_seek,
|
2015-11-20 04:08:34 +02:00
|
|
|
.priv_class = &ffm_class,
|
2002-05-20 19:31:13 +03:00
|
|
|
};
|