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.

132 lines
3.7KB

  1. /*
  2. * @file
  3. * Tele-typewriter demuxer
  4. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/avstring.h"
  24. #include "avformat.h"
  25. #include "sauce.h"
  26. #include <strings.h>
  27. #define LINE_RATE 6000 /* characters per second */
  28. typedef struct {
  29. int chars_per_frame;
  30. uint64_t fsize; /** file size less metadata buffer */
  31. } TtyDemuxContext;
  32. /**
  33. * Parse EFI header
  34. */
  35. static int efi_read(AVFormatContext *avctx, uint64_t start_pos)
  36. {
  37. TtyDemuxContext *s = avctx->priv_data;
  38. ByteIOContext *pb = avctx->pb;
  39. char buf[37];
  40. int len;
  41. url_fseek(pb, start_pos, SEEK_SET);
  42. if (get_byte(pb) != 0x1A)
  43. return -1;
  44. #define GET_EFI_META(name,size) \
  45. len = get_byte(pb); \
  46. if (len < 1 || len > size) \
  47. return -1; \
  48. if (get_buffer(pb, buf, size) == size) { \
  49. buf[len] = 0; \
  50. av_metadata_set2(&avctx->metadata, name, buf, 0); \
  51. }
  52. GET_EFI_META("filename", 12)
  53. GET_EFI_META("title", 36)
  54. s->fsize = start_pos;
  55. return 0;
  56. }
  57. static int read_header(AVFormatContext *avctx,
  58. AVFormatParameters *ap)
  59. {
  60. TtyDemuxContext *s = avctx->priv_data;
  61. AVStream *st = av_new_stream(avctx, 0);
  62. if (!st)
  63. return AVERROR(ENOMEM);
  64. st->codec->codec_tag = 0;
  65. st->codec->codec_type = CODEC_TYPE_VIDEO;
  66. st->codec->codec_id = CODEC_ID_ANSI;
  67. if (ap->width) st->codec->width = ap->width;
  68. if (ap->height) st->codec->height = ap->height;
  69. if (!ap->time_base.num) {
  70. av_set_pts_info(st, 60, 1, 25);
  71. } else {
  72. av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
  73. }
  74. /* simulate tty display speed */
  75. s->chars_per_frame = FFMAX(av_q2d(st->time_base) * (ap->sample_rate ? ap->sample_rate : LINE_RATE), 1);
  76. if (!url_is_streamed(avctx->pb)) {
  77. s->fsize = url_fsize(avctx->pb);
  78. st->duration = (s->fsize + s->chars_per_frame - 1) / s->chars_per_frame;
  79. if (ff_sauce_read(avctx, &s->fsize, 0, 0) < 0)
  80. efi_read(avctx, s->fsize - 51);
  81. url_fseek(avctx->pb, 0, SEEK_SET);
  82. }
  83. return 0;
  84. }
  85. static int read_packet(AVFormatContext *avctx, AVPacket *pkt)
  86. {
  87. TtyDemuxContext *s = avctx->priv_data;
  88. int n;
  89. if (url_feof(avctx->pb))
  90. return AVERROR_EOF;
  91. n = s->chars_per_frame;
  92. if (s->fsize) {
  93. // ignore metadata buffer
  94. uint64_t p = url_ftell(avctx->pb);
  95. if (p + s->chars_per_frame > s->fsize)
  96. n = s->fsize - p;
  97. }
  98. pkt->size = av_get_packet(avctx->pb, pkt, n);
  99. if (pkt->size <= 0)
  100. return AVERROR(EIO);
  101. pkt->flags |= PKT_FLAG_KEY;
  102. return 0;
  103. }
  104. AVInputFormat tty_demuxer = {
  105. .name = "tty",
  106. .long_name = NULL_IF_CONFIG_SMALL("Tele-typewriter"),
  107. .priv_data_size = sizeof(TtyDemuxContext),
  108. .read_header = read_header,
  109. .read_packet = read_packet,
  110. .extensions = "ans,art,asc,diz,ice,nfo,txt,vt",
  111. };