tmv.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * @file
  23. * 8088flex TMV file demuxer
  24. * @author Daniel Verkamp
  25. * @see 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 TMV_HEADER_SIZE 12
  41. #define PROBE_MIN_SAMPLE_RATE 5000
  42. #define PROBE_MAX_FPS 120
  43. #define PROBE_MIN_AUDIO_SIZE (PROBE_MIN_SAMPLE_RATE / PROBE_MAX_FPS)
  44. static int tmv_probe(AVProbeData *p)
  45. {
  46. if (AV_RL32(p->buf) == TMV_TAG &&
  47. AV_RL16(p->buf+4) >= PROBE_MIN_SAMPLE_RATE &&
  48. AV_RL16(p->buf+6) >= PROBE_MIN_AUDIO_SIZE &&
  49. !p->buf[8] && // compression method
  50. p->buf[9] && // char cols
  51. p->buf[10]) // char rows
  52. return AVPROBE_SCORE_MAX /
  53. ((p->buf[9] == 40 && p->buf[10] == 25) ? 1 : 4);
  54. return 0;
  55. }
  56. static int tmv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  57. {
  58. TMVContext *tmv = s->priv_data;
  59. AVIOContext *pb = s->pb;
  60. AVStream *vst, *ast;
  61. AVRational fps;
  62. unsigned comp_method, char_cols, char_rows, features;
  63. if (avio_rl32(pb) != TMV_TAG)
  64. return -1;
  65. if (!(vst = av_new_stream(s, 0)))
  66. return AVERROR(ENOMEM);
  67. if (!(ast = av_new_stream(s, 0)))
  68. return AVERROR(ENOMEM);
  69. ast->codec->sample_rate = avio_rl16(pb);
  70. if (!ast->codec->sample_rate) {
  71. av_log(s, AV_LOG_ERROR, "invalid sample rate\n");
  72. return -1;
  73. }
  74. tmv->audio_chunk_size = avio_rl16(pb);
  75. if (!tmv->audio_chunk_size) {
  76. av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
  77. return -1;
  78. }
  79. comp_method = avio_r8(pb);
  80. if (comp_method) {
  81. av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
  82. comp_method);
  83. return -1;
  84. }
  85. char_cols = avio_r8(pb);
  86. char_rows = avio_r8(pb);
  87. tmv->video_chunk_size = char_cols * char_rows * 2;
  88. features = avio_r8(pb);
  89. if (features & ~(TMV_PADDING | TMV_STEREO)) {
  90. av_log(s, AV_LOG_ERROR, "unsupported features 0x%02x\n",
  91. features & ~(TMV_PADDING | TMV_STEREO));
  92. return -1;
  93. }
  94. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  95. ast->codec->codec_id = CODEC_ID_PCM_U8;
  96. ast->codec->channels = features & TMV_STEREO ? 2 : 1;
  97. ast->codec->bits_per_coded_sample = 8;
  98. ast->codec->bit_rate = ast->codec->sample_rate *
  99. ast->codec->bits_per_coded_sample;
  100. av_set_pts_info(ast, 32, 1, ast->codec->sample_rate);
  101. fps.num = ast->codec->sample_rate * ast->codec->channels;
  102. fps.den = tmv->audio_chunk_size;
  103. av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
  104. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  105. vst->codec->codec_id = CODEC_ID_TMV;
  106. vst->codec->pix_fmt = PIX_FMT_PAL8;
  107. vst->codec->width = char_cols * 8;
  108. vst->codec->height = char_rows * 8;
  109. av_set_pts_info(vst, 32, fps.den, fps.num);
  110. if (features & TMV_PADDING)
  111. tmv->padding =
  112. ((tmv->video_chunk_size + tmv->audio_chunk_size + 511) & ~511) -
  113. (tmv->video_chunk_size + tmv->audio_chunk_size);
  114. vst->codec->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
  115. fps.num * 8) / fps.den;
  116. return 0;
  117. }
  118. static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
  119. {
  120. TMVContext *tmv = s->priv_data;
  121. AVIOContext *pb = s->pb;
  122. int ret, pkt_size = tmv->stream_index ?
  123. tmv->audio_chunk_size : tmv->video_chunk_size;
  124. if (url_feof(pb))
  125. return AVERROR_EOF;
  126. ret = av_get_packet(pb, pkt, pkt_size);
  127. if (tmv->stream_index)
  128. avio_skip(pb, tmv->padding);
  129. pkt->stream_index = tmv->stream_index;
  130. tmv->stream_index ^= 1;
  131. pkt->flags |= AV_PKT_FLAG_KEY;
  132. return ret;
  133. }
  134. static int tmv_read_seek(AVFormatContext *s, int stream_index,
  135. int64_t timestamp, int flags)
  136. {
  137. TMVContext *tmv = s->priv_data;
  138. int64_t pos;
  139. if (stream_index)
  140. return -1;
  141. pos = timestamp *
  142. (tmv->audio_chunk_size + tmv->video_chunk_size + tmv->padding);
  143. avio_seek(s->pb, pos + TMV_HEADER_SIZE, SEEK_SET);
  144. tmv->stream_index = 0;
  145. return 0;
  146. }
  147. AVInputFormat ff_tmv_demuxer = {
  148. .name = "tmv",
  149. .long_name = NULL_IF_CONFIG_SMALL("8088flex TMV"),
  150. .priv_data_size = sizeof(TMVContext),
  151. .read_probe = tmv_probe,
  152. .read_header = tmv_read_header,
  153. .read_packet = tmv_read_packet,
  154. .read_seek = tmv_read_seek,
  155. .flags = AVFMT_GENERIC_INDEX,
  156. };