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.

153 lines
4.2KB

  1. /*
  2. * Tele-typewriter demuxer
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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/opt.h"
  29. #include "avformat.h"
  30. #include "sauce.h"
  31. typedef struct {
  32. AVClass *class;
  33. int chars_per_frame;
  34. uint64_t fsize; /**< file size less metadata buffer */
  35. } TtyDemuxContext;
  36. /**
  37. * Parse EFI header
  38. */
  39. static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
  40. {
  41. TtyDemuxContext *s = avctx->priv_data;
  42. AVIOContext *pb = avctx->pb;
  43. char buf[37];
  44. int len;
  45. avio_seek(pb, start_pos, SEEK_SET);
  46. if (avio_r8(pb) != 0x1A)
  47. return -1;
  48. #define GET_EFI_META(name,size) \
  49. len = avio_r8(pb); \
  50. if (len < 1 || len > size) \
  51. return -1; \
  52. if (avio_read(pb, buf, size) == size) { \
  53. buf[len] = 0; \
  54. av_metadata_set2(&avctx->metadata, name, buf, 0); \
  55. }
  56. GET_EFI_META("filename", 12)
  57. GET_EFI_META("title", 36)
  58. s->fsize = start_pos;
  59. return 0;
  60. }
  61. static int read_header(AVFormatContext *avctx,
  62. AVFormatParameters *ap)
  63. {
  64. TtyDemuxContext *s = avctx->priv_data;
  65. AVStream *st = av_new_stream(avctx, 0);
  66. if (!st)
  67. return AVERROR(ENOMEM);
  68. st->codec->codec_tag = 0;
  69. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  70. st->codec->codec_id = CODEC_ID_ANSI;
  71. if (ap->width) st->codec->width = ap->width;
  72. if (ap->height) st->codec->height = ap->height;
  73. if (!ap->time_base.num) {
  74. av_set_pts_info(st, 60, 1, 25);
  75. } else {
  76. av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
  77. }
  78. /* simulate tty display speed */
  79. #if FF_API_FORMAT_PARAMETERS
  80. if (ap->sample_rate)
  81. s->chars_per_frame = ap->sample_rate;
  82. #endif
  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. return 0;
  92. }
  93. static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
  94. {
  95. TtyDemuxContext *s = avctx->priv_data;
  96. int n;
  97. if (avctx->pb->eof_reached)
  98. return AVERROR_EOF;
  99. n = s->chars_per_frame;
  100. if (s->fsize) {
  101. // ignore metadata buffer
  102. uint64_t p = avio_tell(avctx->pb);
  103. if (p + s->chars_per_frame > s->fsize)
  104. n = s->fsize - p;
  105. }
  106. pkt->size = av_get_packet(avctx->pb, pkt, n);
  107. if (pkt->size <= 0)
  108. return AVERROR(EIO);
  109. pkt->flags |= AV_PKT_FLAG_KEY;
  110. return 0;
  111. }
  112. static const AVOption options[] = {
  113. { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), FF_OPT_TYPE_INT, {.dbl = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
  114. { NULL },
  115. };
  116. static const AVClass tty_demuxer_class = {
  117. .class_name = "TTY demuxer",
  118. .item_name = av_default_item_name,
  119. .option = options,
  120. .version = LIBAVUTIL_VERSION_INT,
  121. };
  122. AVInputFormat ff_tty_demuxer = {
  123. .name = "tty",
  124. .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
  125. .priv_data_size = sizeof(TtyDemuxContext),
  126. .read_header = read_header,
  127. .read_packet = read_packet,
  128. .extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
  129. .priv_class = &tty_demuxer_class,
  130. };