/lib/ffmpeg/libavformat/tmv.c

http://github.com/xbmc/xbmc · C · 199 lines · 139 code · 34 blank · 26 comment · 22 complexity · bf849bb5442dbcd74d781b4eb3d509a8 MD5 · raw file

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