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.

161 lines
4.6KB

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