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"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "sauce.h"
|
|
|
|
|
|
|
|
#define LINE_RATE 6000 /* characters per second */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int chars_per_frame;
|
2010-07-27 18:54:26 +03:00
|
|
|
uint64_t fsize; /**< file size less metadata buffer */
|
2010-07-18 11:06:55 +03:00
|
|
|
} TtyDemuxContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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; \
|
|
|
|
av_metadata_set2(&avctx->metadata, name, buf, 0); \
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_EFI_META("filename", 12)
|
|
|
|
GET_EFI_META("title", 36)
|
|
|
|
|
|
|
|
s->fsize = start_pos;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_header(AVFormatContext *avctx,
|
|
|
|
AVFormatParameters *ap)
|
|
|
|
{
|
|
|
|
TtyDemuxContext *s = avctx->priv_data;
|
|
|
|
AVStream *st = av_new_stream(avctx, 0);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
st->codec->codec_tag = 0;
|
2010-09-26 02:27:16 +03:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2010-07-18 11:06:55 +03:00
|
|
|
st->codec->codec_id = CODEC_ID_ANSI;
|
|
|
|
if (ap->width) st->codec->width = ap->width;
|
|
|
|
if (ap->height) st->codec->height = ap->height;
|
|
|
|
|
|
|
|
if (!ap->time_base.num) {
|
|
|
|
av_set_pts_info(st, 60, 1, 25);
|
|
|
|
} else {
|
|
|
|
av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* simulate tty display speed */
|
|
|
|
s->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
|
|
|
|
|
|
|
|
if (!url_is_streamed(avctx->pb)) {
|
|
|
|
s->fsize = url_fsize(avctx->pb);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
TtyDemuxContext *s = avctx->priv_data;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (url_feof(avctx->pb))
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
n = s->chars_per_frame;
|
|
|
|
if (s->fsize) {
|
|
|
|
// ignore metadata buffer
|
|
|
|
uint64_t p = url_ftell(avctx->pb);
|
|
|
|
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 15:34:02 +02:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2010-07-18 11:06:55 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
.extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
|
|
|
|
};
|