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.

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