2010-07-18 11:06:55 +03:00
|
|
|
/*
|
|
|
|
* Tele-typewriter demuxer
|
|
|
|
* Copyright (c) 2010 Peter Ross <pross@xvid.org>
|
|
|
|
*
|
|
|
|
* 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-07-19 14:50:07 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Tele-typewriter demuxer
|
|
|
|
*/
|
|
|
|
|
2010-07-18 11:06:55 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2011-05-23 20:23:55 +03:00
|
|
|
#include "libavutil/log.h"
|
2011-05-22 13:46:29 +03:00
|
|
|
#include "libavutil/dict.h"
|
2011-05-23 20:23:55 +03:00
|
|
|
#include "libavutil/opt.h"
|
2011-05-24 08:43:01 +03:00
|
|
|
#include "libavutil/parseutils.h"
|
2010-07-18 11:06:55 +03:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2010-07-18 11:06:55 +03:00
|
|
|
#include "sauce.h"
|
|
|
|
|
2020-01-27 22:53:08 +02:00
|
|
|
static int isansicode(int x)
|
|
|
|
{
|
|
|
|
return x == 0x1B || x == 0x0A || x == 0x0D || (x >= 0x20 && x < 0x7f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char tty_extensions[31] = "ans,art,asc,diz,ice,nfo,txt,vt";
|
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct TtyDemuxContext {
|
2011-05-23 20:23:55 +03:00
|
|
|
AVClass *class;
|
2010-07-18 11:06:55 +03:00
|
|
|
int chars_per_frame;
|
2010-07-27 18:54:26 +03:00
|
|
|
uint64_t fsize; /**< file size less metadata buffer */
|
2013-04-05 17:30:56 +03:00
|
|
|
int width, height; /**< Set by a private option. */
|
2013-04-05 17:24:40 +03:00
|
|
|
AVRational framerate; /**< Set by a private option. */
|
2010-07-18 11:06:55 +03:00
|
|
|
} TtyDemuxContext;
|
|
|
|
|
2020-01-27 22:53:08 +02:00
|
|
|
static int read_probe(const AVProbeData *p)
|
|
|
|
{
|
|
|
|
int cnt = 0;
|
|
|
|
|
2020-02-05 23:22:30 +02:00
|
|
|
if (!p->buf_size)
|
|
|
|
return 0;
|
|
|
|
|
2020-02-02 14:51:49 +02:00
|
|
|
for (int i = 0; i < 8 && i < p->buf_size; i++)
|
|
|
|
cnt += !!isansicode(p->buf[i]);
|
|
|
|
|
|
|
|
if (cnt != 8)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (int i = 8; i < p->buf_size; i++)
|
2020-01-27 22:53:08 +02:00
|
|
|
cnt += !!isansicode(p->buf[i]);
|
|
|
|
|
2020-03-13 01:53:16 +02:00
|
|
|
return (cnt * 99LL / p->buf_size) * (cnt > 400) *
|
2020-01-27 22:53:08 +02:00
|
|
|
!!av_match_ext(p->filename, tty_extensions);
|
|
|
|
}
|
|
|
|
|
2010-07-18 11:06:55 +03:00
|
|
|
/**
|
|
|
|
* Parse EFI header
|
|
|
|
*/
|
|
|
|
static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
|
|
|
|
{
|
|
|
|
TtyDemuxContext *s = avctx->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = avctx->pb;
|
2010-07-18 11:06:55 +03:00
|
|
|
char buf[37];
|
|
|
|
int len;
|
|
|
|
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, start_pos, SEEK_SET);
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_r8(pb) != 0x1A)
|
2010-07-18 11:06:55 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
#define GET_EFI_META(name,size) \
|
2011-02-21 17:43:01 +02:00
|
|
|
len = avio_r8(pb); \
|
2010-07-18 11:06:55 +03:00
|
|
|
if (len < 1 || len > size) \
|
|
|
|
return -1; \
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_read(pb, buf, size) == size) { \
|
2010-07-18 11:06:55 +03:00
|
|
|
buf[len] = 0; \
|
2011-05-22 13:46:29 +03:00
|
|
|
av_dict_set(&avctx->metadata, name, buf, 0); \
|
2010-07-18 11:06:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GET_EFI_META("filename", 12)
|
|
|
|
GET_EFI_META("title", 36)
|
|
|
|
|
|
|
|
s->fsize = start_pos;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int read_header(AVFormatContext *avctx)
|
2010-07-18 11:06:55 +03:00
|
|
|
{
|
|
|
|
TtyDemuxContext *s = avctx->priv_data;
|
2013-04-05 17:30:56 +03:00
|
|
|
int ret = 0;
|
2011-06-18 12:43:24 +03:00
|
|
|
AVStream *st = avformat_new_stream(avctx, NULL);
|
2011-06-03 21:43:48 +03:00
|
|
|
|
|
|
|
if (!st) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
st->codecpar->codec_tag = 0;
|
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_ANSI;
|
2011-05-24 08:43:01 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->width = s->width;
|
|
|
|
st->codecpar->height = s->height;
|
2013-04-05 17:24:40 +03:00
|
|
|
avpriv_set_pts_info(st, 60, s->framerate.den, s->framerate.num);
|
|
|
|
st->avg_frame_rate = s->framerate;
|
2010-07-18 11:06:55 +03:00
|
|
|
|
|
|
|
/* simulate tty display speed */
|
2011-05-23 20:23:55 +03:00
|
|
|
s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
|
2010-07-18 11:06:55 +03:00
|
|
|
|
2016-09-27 16:26:37 +02:00
|
|
|
if (avctx->pb->seekable & AVIO_SEEKABLE_NORMAL) {
|
2011-03-04 20:57:36 +02:00
|
|
|
s->fsize = avio_size(avctx->pb);
|
2010-07-18 11:06:55 +03:00
|
|
|
st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
|
|
|
|
|
|
|
|
if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
|
|
|
|
efi_read(avctx, s->fsize - 51);
|
|
|
|
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(avctx->pb, 0, SEEK_SET);
|
2010-07-18 11:06:55 +03:00
|
|
|
}
|
|
|
|
|
2011-06-03 21:43:48 +03:00
|
|
|
fail:
|
|
|
|
return ret;
|
2010-07-18 11:06:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
TtyDemuxContext *s = avctx->priv_data;
|
|
|
|
int n;
|
|
|
|
|
2014-08-07 23:12:41 +03:00
|
|
|
if (avio_feof(avctx->pb))
|
2010-07-18 11:06:55 +03:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
n = s->chars_per_frame;
|
|
|
|
if (s->fsize) {
|
|
|
|
// ignore metadata buffer
|
2011-03-03 21:11:45 +02:00
|
|
|
uint64_t p = avio_tell(avctx->pb);
|
2012-09-02 10:37:11 +03:00
|
|
|
if (p == s->fsize)
|
|
|
|
return AVERROR_EOF;
|
2010-07-18 11:06:55 +03:00
|
|
|
if (p + s->chars_per_frame > s->fsize)
|
|
|
|
n = s->fsize - p;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->size = av_get_packet(avctx->pb, pkt, n);
|
2012-09-04 02:44:37 +03:00
|
|
|
if (pkt->size < 0)
|
|
|
|
return pkt->size;
|
2020-02-02 15:03:45 +02:00
|
|
|
pkt->stream_index = 0;
|
2020-02-10 17:47:03 +02:00
|
|
|
pkt->pts = pkt->pos / s->chars_per_frame;
|
2011-02-03 15:34:02 +02:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2010-07-18 11:06:55 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-24 08:43:01 +03:00
|
|
|
#define OFFSET(x) offsetof(TtyDemuxContext, x)
|
|
|
|
#define DEC AV_OPT_FLAG_DECODING_PARAM
|
2011-05-23 20:23:55 +03:00
|
|
|
static const AVOption options[] = {
|
2012-08-31 13:22:31 +03:00
|
|
|
{ "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
|
2013-04-05 17:30:56 +03:00
|
|
|
{ "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
|
2016-06-09 05:00:52 +02:00
|
|
|
{ "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
|
2011-05-23 20:23:55 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass tty_demuxer_class = {
|
|
|
|
.class_name = "TTY demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_tty_demuxer = {
|
2010-07-18 11:06:55 +03:00
|
|
|
.name = "tty",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
|
|
|
|
.priv_data_size = sizeof(TtyDemuxContext),
|
2020-01-27 22:53:08 +02:00
|
|
|
.read_probe = read_probe,
|
2010-07-18 11:06:55 +03:00
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
2020-01-27 22:53:08 +02:00
|
|
|
.extensions = tty_extensions,
|
2011-05-23 20:23:55 +03:00
|
|
|
.priv_class = &tty_demuxer_class,
|
2020-02-02 15:03:45 +02:00
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
2010-07-18 11:06:55 +03:00
|
|
|
};
|