2010-07-18 10:06:55 +02:00
|
|
|
/*
|
|
|
|
* Tele-typewriter demuxer
|
|
|
|
* Copyright (c) 2010 Peter Ross <pross@xvid.org>
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2010-07-18 10:06:55 +02:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-07-18 10:06:55 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-07-18 10:06:55 +02:00
|
|
|
* 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
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-07-18 10:06:55 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2010-07-19 13:50:07 +02:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Tele-typewriter demuxer
|
|
|
|
*/
|
|
|
|
|
2010-07-18 10:06:55 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2011-05-23 19:23:55 +02:00
|
|
|
#include "libavutil/log.h"
|
2011-05-22 12:46:29 +02:00
|
|
|
#include "libavutil/dict.h"
|
2011-05-23 19:23:55 +02:00
|
|
|
#include "libavutil/opt.h"
|
2011-05-24 07:43:01 +02:00
|
|
|
#include "libavutil/parseutils.h"
|
2010-07-18 10:06:55 +02:00
|
|
|
#include "avformat.h"
|
2011-11-29 19:28:15 +01:00
|
|
|
#include "internal.h"
|
2010-07-18 10:06:55 +02:00
|
|
|
#include "sauce.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2011-05-23 19:23:55 +02:00
|
|
|
AVClass *class;
|
2010-07-18 10:06:55 +02:00
|
|
|
int chars_per_frame;
|
2010-07-27 17:54:26 +02:00
|
|
|
uint64_t fsize; /**< file size less metadata buffer */
|
2011-05-24 07:43:01 +02:00
|
|
|
char *video_size;/**< A string describing video size, set by a private option. */
|
2011-06-03 20:58:01 +02:00
|
|
|
char *framerate; /**< Set by a private option. */
|
2010-07-18 10:06:55 +02:00
|
|
|
} TtyDemuxContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse EFI header
|
|
|
|
*/
|
|
|
|
static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
|
|
|
|
{
|
|
|
|
TtyDemuxContext *s = avctx->priv_data;
|
2011-02-20 11:04:12 +01:00
|
|
|
AVIOContext *pb = avctx->pb;
|
2010-07-18 10:06:55 +02:00
|
|
|
char buf[37];
|
|
|
|
int len;
|
|
|
|
|
2011-02-28 14:57:54 +01:00
|
|
|
avio_seek(pb, start_pos, SEEK_SET);
|
2011-02-21 16:43:01 +01:00
|
|
|
if (avio_r8(pb) != 0x1A)
|
2010-07-18 10:06:55 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
#define GET_EFI_META(name,size) \
|
2011-02-21 16:43:01 +01:00
|
|
|
len = avio_r8(pb); \
|
2010-07-18 10:06:55 +02:00
|
|
|
if (len < 1 || len > size) \
|
|
|
|
return -1; \
|
2011-02-21 16:43:01 +01:00
|
|
|
if (avio_read(pb, buf, size) == size) { \
|
2010-07-18 10:06:55 +02:00
|
|
|
buf[len] = 0; \
|
2011-05-22 12:46:29 +02:00
|
|
|
av_dict_set(&avctx->metadata, name, buf, 0); \
|
2010-07-18 10:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GET_EFI_META("filename", 12)
|
|
|
|
GET_EFI_META("title", 36)
|
|
|
|
|
|
|
|
s->fsize = start_pos;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 13:20:36 +01:00
|
|
|
static int read_header(AVFormatContext *avctx)
|
2010-07-18 10:06:55 +02:00
|
|
|
{
|
|
|
|
TtyDemuxContext *s = avctx->priv_data;
|
2011-06-03 20:43:48 +02:00
|
|
|
int width = 0, height = 0, ret = 0;
|
2011-06-18 11:43:24 +02:00
|
|
|
AVStream *st = avformat_new_stream(avctx, NULL);
|
2011-06-03 20:58:01 +02:00
|
|
|
AVRational framerate;
|
2011-06-03 20:43:48 +02:00
|
|
|
|
|
|
|
if (!st) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-07-18 10:06:55 +02:00
|
|
|
st->codec->codec_tag = 0;
|
2010-09-26 01:27:16 +02:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2012-08-05 11:11:04 +02:00
|
|
|
st->codec->codec_id = AV_CODEC_ID_ANSI;
|
2011-05-24 07:43:01 +02:00
|
|
|
|
2011-06-03 20:43:48 +02:00
|
|
|
if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
|
|
|
|
av_log (avctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
|
|
|
|
goto fail;
|
2011-05-24 07:43:01 +02:00
|
|
|
}
|
2011-06-03 20:58:01 +02:00
|
|
|
if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-05-24 07:43:01 +02:00
|
|
|
st->codec->width = width;
|
|
|
|
st->codec->height = height;
|
2011-11-29 19:28:15 +01:00
|
|
|
avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
|
2013-02-19 17:19:30 +01:00
|
|
|
st->avg_frame_rate = framerate;
|
2010-07-18 10:06:55 +02:00
|
|
|
|
|
|
|
/* simulate tty display speed */
|
2011-05-23 19:23:55 +02:00
|
|
|
s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
|
2010-07-18 10:06:55 +02:00
|
|
|
|
2011-03-05 21:06:46 +01:00
|
|
|
if (avctx->pb->seekable) {
|
2011-03-04 19:57:36 +01:00
|
|
|
s->fsize = avio_size(avctx->pb);
|
2010-07-18 10:06:55 +02: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 14:57:54 +01:00
|
|
|
avio_seek(avctx->pb, 0, SEEK_SET);
|
2010-07-18 10:06:55 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 20:43:48 +02:00
|
|
|
fail:
|
|
|
|
return ret;
|
2010-07-18 10:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
TtyDemuxContext *s = avctx->priv_data;
|
|
|
|
int n;
|
|
|
|
|
2011-03-07 21:50:25 +01:00
|
|
|
if (avctx->pb->eof_reached)
|
2010-07-18 10:06:55 +02:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
n = s->chars_per_frame;
|
|
|
|
if (s->fsize) {
|
|
|
|
// ignore metadata buffer
|
2011-03-03 20:11:45 +01:00
|
|
|
uint64_t p = avio_tell(avctx->pb);
|
2010-07-18 10:06:55 +02:00
|
|
|
if (p + s->chars_per_frame > s->fsize)
|
|
|
|
n = s->fsize - p;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt->size = av_get_packet(avctx->pb, pkt, n);
|
|
|
|
if (pkt->size <= 0)
|
|
|
|
return AVERROR(EIO);
|
2011-02-03 14:34:02 +01:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2010-07-18 10:06:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-24 07:43:01 +02:00
|
|
|
#define OFFSET(x) offsetof(TtyDemuxContext, x)
|
|
|
|
#define DEC AV_OPT_FLAG_DECODING_PARAM
|
2011-05-23 19:23:55 +02:00
|
|
|
static const AVOption options[] = {
|
2012-08-31 12:22:31 +02:00
|
|
|
{ "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
|
2011-10-04 07:38:01 +02:00
|
|
|
{ "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
|
|
|
|
{ "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
|
2011-05-23 19:23:55 +02: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-25 23:03:28 +01:00
|
|
|
AVInputFormat ff_tty_demuxer = {
|
2010-07-18 10:06:55 +02:00
|
|
|
.name = "tty",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
|
|
|
|
.priv_data_size = sizeof(TtyDemuxContext),
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
.extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
|
2011-05-23 19:23:55 +02:00
|
|
|
.priv_class = &tty_demuxer_class,
|
2010-07-18 10:06:55 +02:00
|
|
|
};
|