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.

157 lines
4.5KB

  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. static int tmv_probe(AVProbeData *p)
  41. {
  42. if (AV_RL32(p->buf) == TMV_TAG)
  43. return AVPROBE_SCORE_MAX;
  44. return 0;
  45. }
  46. static int tmv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  47. {
  48. TMVContext *tmv = s->priv_data;
  49. ByteIOContext *pb = s->pb;
  50. AVStream *vst, *ast;
  51. AVRational fps;
  52. unsigned comp_method, char_cols, char_rows, features;
  53. if (get_le32(pb) != TMV_TAG)
  54. return -1;
  55. if (!(vst = av_new_stream(s, 0)))
  56. return AVERROR(ENOMEM);
  57. if (!(ast = av_new_stream(s, 0)))
  58. return AVERROR(ENOMEM);
  59. ast->codec->sample_rate = get_le16(pb);
  60. tmv->audio_chunk_size = get_le16(pb);
  61. if (!tmv->audio_chunk_size) {
  62. av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
  63. return -1;
  64. }
  65. comp_method = get_byte(pb);
  66. if (comp_method) {
  67. av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
  68. comp_method);
  69. return -1;
  70. }
  71. char_cols = get_byte(pb);
  72. char_rows = get_byte(pb);
  73. tmv->video_chunk_size = char_cols * char_rows * 2;
  74. features = get_byte(pb);
  75. if (features & ~(TMV_PADDING | TMV_STEREO)) {
  76. av_log(s, AV_LOG_ERROR, "unsupported features 0x%02x\n",
  77. features & ~(TMV_PADDING | TMV_STEREO));
  78. return -1;
  79. }
  80. ast->codec->codec_type = CODEC_TYPE_AUDIO;
  81. ast->codec->codec_id = CODEC_ID_PCM_U8;
  82. ast->codec->sample_fmt = SAMPLE_FMT_U8;
  83. ast->codec->channels = features & TMV_STEREO ? 2 : 1;
  84. ast->codec->bits_per_coded_sample = 8;
  85. ast->codec->bit_rate = ast->codec->sample_rate *
  86. ast->codec->bits_per_coded_sample;
  87. av_set_pts_info(ast, 32, 1, ast->codec->sample_rate);
  88. fps.num = ast->codec->sample_rate * ast->codec->channels;
  89. fps.den = tmv->audio_chunk_size;
  90. av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
  91. vst->codec->codec_type = CODEC_TYPE_VIDEO;
  92. vst->codec->codec_id = CODEC_ID_TMV;
  93. vst->codec->pix_fmt = PIX_FMT_PAL8;
  94. vst->codec->width = char_cols * 8;
  95. vst->codec->height = char_rows * 8;
  96. av_set_pts_info(vst, 32, fps.den, fps.num);
  97. if (features & TMV_PADDING)
  98. tmv->padding =
  99. ((tmv->video_chunk_size + tmv->audio_chunk_size + 511) & ~511) -
  100. (tmv->video_chunk_size + tmv->audio_chunk_size);
  101. vst->codec->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
  102. fps.num * 8) / fps.den;
  103. return 0;
  104. }
  105. static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
  106. {
  107. TMVContext *tmv = s->priv_data;
  108. ByteIOContext *pb = s->pb;
  109. int ret, pkt_size = tmv->stream_index ?
  110. tmv->audio_chunk_size : tmv->video_chunk_size;
  111. if (url_feof(pb))
  112. return AVERROR_EOF;
  113. ret = av_get_packet(pb, pkt, pkt_size);
  114. if (tmv->stream_index)
  115. url_fskip(pb, tmv->padding);
  116. pkt->stream_index = tmv->stream_index;
  117. tmv->stream_index ^= 1;
  118. pkt->flags |= PKT_FLAG_KEY;
  119. return ret;
  120. }
  121. AVInputFormat tmv_demuxer = {
  122. "tmv",
  123. NULL_IF_CONFIG_SMALL("8088flex TMV"),
  124. sizeof(TMVContext),
  125. tmv_probe,
  126. tmv_read_header,
  127. tmv_read_packet,
  128. .flags = AVFMT_GENERIC_INDEX,
  129. };