tty.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "avformat.h"
  28. #include "sauce.h"
  29. #define LINE_RATE 6000 /* characters per second */
  30. typedef struct {
  31. int chars_per_frame;
  32. uint64_t fsize; /**< file size less metadata buffer */
  33. } TtyDemuxContext;
  34. /**
  35. * Parse EFI header
  36. */
  37. static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
  38. {
  39. TtyDemuxContext *s = avctx->priv_data;
  40. AVIOContext *pb = avctx->pb;
  41. char buf[37];
  42. int len;
  43. avio_seek(pb, start_pos, SEEK_SET);
  44. if (avio_r8(pb) != 0x1A)
  45. return -1;
  46. #define GET_EFI_META(name,size) \
  47. len = avio_r8(pb); \
  48. if (len < 1 || len > size) \
  49. return -1; \
  50. if (avio_read(pb, buf, size) == size) { \
  51. buf[len] = 0; \
  52. av_metadata_set2(&avctx->metadata, name, buf, 0); \
  53. }
  54. GET_EFI_META("filename", 12)
  55. GET_EFI_META("title", 36)
  56. s->fsize = start_pos;
  57. return 0;
  58. }
  59. static int read_header(AVFormatContext *avctx,
  60. AVFormatParameters *ap)
  61. {
  62. TtyDemuxContext *s = avctx->priv_data;
  63. AVStream *st = av_new_stream(avctx, 0);
  64. if (!st)
  65. return AVERROR(ENOMEM);
  66. st->codec->codec_tag = 0;
  67. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  68. st->codec->codec_id = CODEC_ID_ANSI;
  69. if (ap->width) st->codec->width = ap->width;
  70. if (ap->height) st->codec->height = ap->height;
  71. if (!ap->time_base.num) {
  72. av_set_pts_info(st, 60, 1, 25);
  73. } else {
  74. av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
  75. }
  76. /* simulate tty display speed */
  77. s->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
  78. if (!url_is_streamed(avctx->pb)) {
  79. s->fsize = avio_size(avctx->pb);
  80. st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
  81. if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
  82. efi_read(avctx, s->fsize - 51);
  83. avio_seek(avctx->pb, 0, SEEK_SET);
  84. }
  85. return 0;
  86. }
  87. static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
  88. {
  89. TtyDemuxContext *s = avctx->priv_data;
  90. int n;
  91. if (avctx->pb->eof_reached)
  92. return AVERROR_EOF;
  93. n = s->chars_per_frame;
  94. if (s->fsize) {
  95. // ignore metadata buffer
  96. uint64_t p = avio_tell(avctx->pb);
  97. if (p + s->chars_per_frame > s->fsize)
  98. n = s->fsize - p;
  99. }
  100. pkt->size = av_get_packet(avctx->pb, pkt, n);
  101. if (pkt->size <= 0)
  102. return AVERROR(EIO);
  103. pkt->flags |= AV_PKT_FLAG_KEY;
  104. return 0;
  105. }
  106. AVInputFormat ff_tty_demuxer = {
  107. .name = "tty",
  108. .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
  109. .priv_data_size = sizeof(TtyDemuxContext),
  110. .read_header = read_header,
  111. .read_packet = read_packet,
  112. .extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
  113. };