You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.4KB

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