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.

171 lines
5.1KB

  1. /*
  2. * 8088flex TMV file demuxer
  3. * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
  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. * 8088flex TMV file demuxer
  23. * @file libavformat/tmv.c
  24. * @author Daniel Verkamp
  25. * @sa http://www.oldskool.org/pc/8088_Corruption
  26. */
  27. #include "libavutil/intreadwrite.h"
  28. #include "avformat.h"
  29. enum {
  30. TMV_PADDING = 0x01,
  31. TMV_STEREO = 0x02,
  32. };
  33. #define TMV_TAG MKTAG('T', 'M', 'A', 'V')
  34. typedef struct TMVContext {
  35. unsigned audio_chunk_size;
  36. unsigned video_chunk_size;
  37. unsigned padding;
  38. unsigned stream_index;
  39. } TMVContext;
  40. #define PROBE_MIN_SAMPLE_RATE 5000
  41. #define PROBE_MAX_FPS 120
  42. #define PROBE_MIN_AUDIO_SIZE (PROBE_MIN_SAMPLE_RATE / PROBE_MAX_FPS)
  43. static int tmv_probe(AVProbeData *p)
  44. {
  45. if (AV_RL32(p->buf) == TMV_TAG &&
  46. AV_RL16(p->buf+4) >= PROBE_MIN_SAMPLE_RATE &&
  47. AV_RL16(p->buf+6) >= PROBE_MIN_AUDIO_SIZE &&
  48. !p->buf[8] && // compression method
  49. p->buf[9] && // char cols
  50. p->buf[10]) // char rows
  51. return AVPROBE_SCORE_MAX / (p->buf[9] == 40 && p->buf[10] == 25)? 1 : 4;
  52. return 0;
  53. }
  54. static int tmv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  55. {
  56. TMVContext *tmv = s->priv_data;
  57. ByteIOContext *pb = s->pb;
  58. AVStream *vst, *ast;
  59. AVRational fps;
  60. unsigned comp_method, char_cols, char_rows, features;
  61. if (get_le32(pb) != TMV_TAG)
  62. return -1;
  63. if (!(vst = av_new_stream(s, 0)))
  64. return AVERROR(ENOMEM);
  65. if (!(ast = av_new_stream(s, 0)))
  66. return AVERROR(ENOMEM);
  67. ast->codec->sample_rate = get_le16(pb);
  68. if (!ast->codec->sample_rate) {
  69. av_log(s, AV_LOG_ERROR, "invalid sample rate\n");
  70. return -1;
  71. }
  72. tmv->audio_chunk_size = get_le16(pb);
  73. if (!tmv->audio_chunk_size) {
  74. av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
  75. return -1;
  76. }
  77. comp_method = get_byte(pb);
  78. if (comp_method) {
  79. av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
  80. comp_method);
  81. return -1;
  82. }
  83. char_cols = get_byte(pb);
  84. char_rows = get_byte(pb);
  85. tmv->video_chunk_size = char_cols * char_rows * 2;
  86. features = get_byte(pb);
  87. if (features & ~(TMV_PADDING | TMV_STEREO)) {
  88. av_log(s, AV_LOG_ERROR, "unsupported features 0x%02x\n",
  89. features & ~(TMV_PADDING | TMV_STEREO));
  90. return -1;
  91. }
  92. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  93. ast->codec->codec_id = CODEC_ID_PCM_U8;
  94. ast->codec->sample_fmt = SAMPLE_FMT_U8;
  95. ast->codec->channels = features & TMV_STEREO ? 2 : 1;
  96. ast->codec->bits_per_coded_sample = 8;
  97. ast->codec->bit_rate = ast->codec->sample_rate *
  98. ast->codec->bits_per_coded_sample;
  99. av_set_pts_info(ast, 32, 1, ast->codec->sample_rate);
  100. fps.num = ast->codec->sample_rate * ast->codec->channels;
  101. fps.den = tmv->audio_chunk_size;
  102. av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
  103. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  104. vst->codec->codec_id = CODEC_ID_TMV;
  105. vst->codec->pix_fmt = PIX_FMT_PAL8;
  106. vst->codec->width = char_cols * 8;
  107. vst->codec->height = char_rows * 8;
  108. av_set_pts_info(vst, 32, fps.den, fps.num);
  109. if (features & TMV_PADDING)
  110. tmv->padding =
  111. ((tmv->video_chunk_size + tmv->audio_chunk_size + 511) & ~511) -
  112. (tmv->video_chunk_size + tmv->audio_chunk_size);
  113. vst->codec->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
  114. fps.num * 8) / fps.den;
  115. return 0;
  116. }
  117. static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
  118. {
  119. TMVContext *tmv = s->priv_data;
  120. ByteIOContext *pb = s->pb;
  121. int ret, pkt_size = tmv->stream_index ?
  122. tmv->audio_chunk_size : tmv->video_chunk_size;
  123. if (url_feof(pb))
  124. return AVERROR_EOF;
  125. ret = av_get_packet(pb, pkt, pkt_size);
  126. if (tmv->stream_index)
  127. url_fskip(pb, tmv->padding);
  128. pkt->stream_index = tmv->stream_index;
  129. tmv->stream_index ^= 1;
  130. pkt->flags |= PKT_FLAG_KEY;
  131. return ret;
  132. }
  133. AVInputFormat tmv_demuxer = {
  134. "tmv",
  135. NULL_IF_CONFIG_SMALL("8088flex TMV"),
  136. sizeof(TMVContext),
  137. tmv_probe,
  138. tmv_read_header,
  139. tmv_read_packet,
  140. .flags = AVFMT_GENERIC_INDEX,
  141. };