tmv.c 5.6 KB

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