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.

180 lines
5.1KB

  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. for (int i = 0; i < p->buf_size; i++)
  50. cnt += !!isansicode(p->buf[i]);
  51. return (cnt * 100LL / p->buf_size) * (cnt > 400) *
  52. !!av_match_ext(p->filename, tty_extensions);
  53. }
  54. /**
  55. * Parse EFI header
  56. */
  57. static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
  58. {
  59. TtyDemuxContext *s = avctx->priv_data;
  60. AVIOContext *pb = avctx->pb;
  61. char buf[37];
  62. int len;
  63. avio_seek(pb, start_pos, SEEK_SET);
  64. if (avio_r8(pb) != 0x1A)
  65. return -1;
  66. #define GET_EFI_META(name,size) \
  67. len = avio_r8(pb); \
  68. if (len < 1 || len > size) \
  69. return -1; \
  70. if (avio_read(pb, buf, size) == size) { \
  71. buf[len] = 0; \
  72. av_dict_set(&avctx->metadata, name, buf, 0); \
  73. }
  74. GET_EFI_META("filename", 12)
  75. GET_EFI_META("title", 36)
  76. s->fsize = start_pos;
  77. return 0;
  78. }
  79. static int read_header(AVFormatContext *avctx)
  80. {
  81. TtyDemuxContext *s = avctx->priv_data;
  82. int ret = 0;
  83. AVStream *st = avformat_new_stream(avctx, NULL);
  84. if (!st) {
  85. ret = AVERROR(ENOMEM);
  86. goto fail;
  87. }
  88. st->codecpar->codec_tag = 0;
  89. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  90. st->codecpar->codec_id = AV_CODEC_ID_ANSI;
  91. st->codecpar->width = s->width;
  92. st->codecpar->height = s->height;
  93. avpriv_set_pts_info(st, 60, s->framerate.den, s->framerate.num);
  94. st->avg_frame_rate = s->framerate;
  95. /* simulate tty display speed */
  96. s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
  97. if (avctx->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  98. s->fsize = avio_size(avctx->pb);
  99. st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
  100. if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
  101. efi_read(avctx, s->fsize - 51);
  102. avio_seek(avctx->pb, 0, SEEK_SET);
  103. }
  104. fail:
  105. return ret;
  106. }
  107. static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
  108. {
  109. TtyDemuxContext *s = avctx->priv_data;
  110. int n;
  111. if (avio_feof(avctx->pb))
  112. return AVERROR_EOF;
  113. n = s->chars_per_frame;
  114. if (s->fsize) {
  115. // ignore metadata buffer
  116. uint64_t p = avio_tell(avctx->pb);
  117. if (p == s->fsize)
  118. return AVERROR_EOF;
  119. if (p + s->chars_per_frame > s->fsize)
  120. n = s->fsize - p;
  121. }
  122. pkt->size = av_get_packet(avctx->pb, pkt, n);
  123. if (pkt->size < 0)
  124. return pkt->size;
  125. pkt->flags |= AV_PKT_FLAG_KEY;
  126. return 0;
  127. }
  128. #define OFFSET(x) offsetof(TtyDemuxContext, x)
  129. #define DEC AV_OPT_FLAG_DECODING_PARAM
  130. static const AVOption options[] = {
  131. { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
  132. { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  133. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
  134. { NULL },
  135. };
  136. static const AVClass tty_demuxer_class = {
  137. .class_name = "TTY demuxer",
  138. .item_name = av_default_item_name,
  139. .option = options,
  140. .version = LIBAVUTIL_VERSION_INT,
  141. };
  142. AVInputFormat ff_tty_demuxer = {
  143. .name = "tty",
  144. .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
  145. .priv_data_size = sizeof(TtyDemuxContext),
  146. .read_probe = read_probe,
  147. .read_header = read_header,
  148. .read_packet = read_packet,
  149. .extensions = tty_extensions,
  150. .priv_class = &tty_demuxer_class,
  151. };