tty.c 5.3 KB

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