annotate ffmpeg/libavformat/tty.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Tele-typewriter demuxer
yading@11 3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 /**
yading@11 23 * @file
yading@11 24 * Tele-typewriter demuxer
yading@11 25 */
yading@11 26
yading@11 27 #include "libavutil/intreadwrite.h"
yading@11 28 #include "libavutil/avstring.h"
yading@11 29 #include "libavutil/log.h"
yading@11 30 #include "libavutil/dict.h"
yading@11 31 #include "libavutil/opt.h"
yading@11 32 #include "libavutil/parseutils.h"
yading@11 33 #include "avformat.h"
yading@11 34 #include "internal.h"
yading@11 35 #include "sauce.h"
yading@11 36
yading@11 37 typedef struct {
yading@11 38 AVClass *class;
yading@11 39 int chars_per_frame;
yading@11 40 uint64_t fsize; /**< file size less metadata buffer */
yading@11 41 int width, height; /**< Set by a private option. */
yading@11 42 AVRational framerate; /**< Set by a private option. */
yading@11 43 } TtyDemuxContext;
yading@11 44
yading@11 45 /**
yading@11 46 * Parse EFI header
yading@11 47 */
yading@11 48 static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
yading@11 49 {
yading@11 50 TtyDemuxContext *s = avctx->priv_data;
yading@11 51 AVIOContext *pb = avctx->pb;
yading@11 52 char buf[37];
yading@11 53 int len;
yading@11 54
yading@11 55 avio_seek(pb, start_pos, SEEK_SET);
yading@11 56 if (avio_r8(pb) != 0x1A)
yading@11 57 return -1;
yading@11 58
yading@11 59 #define GET_EFI_META(name,size) \
yading@11 60 len = avio_r8(pb); \
yading@11 61 if (len < 1 || len > size) \
yading@11 62 return -1; \
yading@11 63 if (avio_read(pb, buf, size) == size) { \
yading@11 64 buf[len] = 0; \
yading@11 65 av_dict_set(&avctx->metadata, name, buf, 0); \
yading@11 66 }
yading@11 67
yading@11 68 GET_EFI_META("filename", 12)
yading@11 69 GET_EFI_META("title", 36)
yading@11 70
yading@11 71 s->fsize = start_pos;
yading@11 72 return 0;
yading@11 73 }
yading@11 74
yading@11 75 static int read_header(AVFormatContext *avctx)
yading@11 76 {
yading@11 77 TtyDemuxContext *s = avctx->priv_data;
yading@11 78 int ret = 0;
yading@11 79 AVStream *st = avformat_new_stream(avctx, NULL);
yading@11 80
yading@11 81 if (!st) {
yading@11 82 ret = AVERROR(ENOMEM);
yading@11 83 goto fail;
yading@11 84 }
yading@11 85 st->codec->codec_tag = 0;
yading@11 86 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
yading@11 87 st->codec->codec_id = AV_CODEC_ID_ANSI;
yading@11 88
yading@11 89 st->codec->width = s->width;
yading@11 90 st->codec->height = s->height;
yading@11 91 avpriv_set_pts_info(st, 60, s->framerate.den, s->framerate.num);
yading@11 92 st->avg_frame_rate = s->framerate;
yading@11 93
yading@11 94 /* simulate tty display speed */
yading@11 95 s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
yading@11 96
yading@11 97 if (avctx->pb->seekable) {
yading@11 98 s->fsize = avio_size(avctx->pb);
yading@11 99 st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
yading@11 100
yading@11 101 if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
yading@11 102 efi_read(avctx, s->fsize - 51);
yading@11 103
yading@11 104 avio_seek(avctx->pb, 0, SEEK_SET);
yading@11 105 }
yading@11 106
yading@11 107 fail:
yading@11 108 return ret;
yading@11 109 }
yading@11 110
yading@11 111 static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
yading@11 112 {
yading@11 113 TtyDemuxContext *s = avctx->priv_data;
yading@11 114 int n;
yading@11 115
yading@11 116 if (url_feof(avctx->pb))
yading@11 117 return AVERROR_EOF;
yading@11 118
yading@11 119 n = s->chars_per_frame;
yading@11 120 if (s->fsize) {
yading@11 121 // ignore metadata buffer
yading@11 122 uint64_t p = avio_tell(avctx->pb);
yading@11 123 if (p == s->fsize)
yading@11 124 return AVERROR_EOF;
yading@11 125 if (p + s->chars_per_frame > s->fsize)
yading@11 126 n = s->fsize - p;
yading@11 127 }
yading@11 128
yading@11 129 pkt->size = av_get_packet(avctx->pb, pkt, n);
yading@11 130 if (pkt->size < 0)
yading@11 131 return pkt->size;
yading@11 132 pkt->flags |= AV_PKT_FLAG_KEY;
yading@11 133 return 0;
yading@11 134 }
yading@11 135
yading@11 136 #define OFFSET(x) offsetof(TtyDemuxContext, x)
yading@11 137 #define DEC AV_OPT_FLAG_DECODING_PARAM
yading@11 138 static const AVOption options[] = {
yading@11 139 { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
yading@11 140 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
yading@11 141 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
yading@11 142 { NULL },
yading@11 143 };
yading@11 144
yading@11 145 static const AVClass tty_demuxer_class = {
yading@11 146 .class_name = "TTY demuxer",
yading@11 147 .item_name = av_default_item_name,
yading@11 148 .option = options,
yading@11 149 .version = LIBAVUTIL_VERSION_INT,
yading@11 150 };
yading@11 151
yading@11 152 AVInputFormat ff_tty_demuxer = {
yading@11 153 .name = "tty",
yading@11 154 .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
yading@11 155 .priv_data_size = sizeof(TtyDemuxContext),
yading@11 156 .read_header = read_header,
yading@11 157 .read_packet = read_packet,
yading@11 158 .extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
yading@11 159 .priv_class = &tty_demuxer_class,
yading@11 160 };