2 * 8088flex TMV file demuxer
3 * Copyright (c) 2009 Daniel Verkamp <daniel at drv.nu>
5 * This file is part of FFmpeg.
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.
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.
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
24 * 8088flex TMV file demuxer
25 * @author Daniel Verkamp
26 * @see http://www.oldskool.org/pc/8088_Corruption
29 #include "libavutil/intreadwrite.h"
38 #define TMV_TAG MKTAG('T', 'M', 'A', 'V')
40 typedef struct TMVContext {
41 unsigned audio_chunk_size;
42 unsigned video_chunk_size;
44 unsigned stream_index;
47 #define TMV_HEADER_SIZE 12
49 #define PROBE_MIN_SAMPLE_RATE 5000
50 #define PROBE_MAX_FPS 120
51 #define PROBE_MIN_AUDIO_SIZE (PROBE_MIN_SAMPLE_RATE / PROBE_MAX_FPS)
53 static int tmv_probe(AVProbeData *p)
55 if (AV_RL32(p->buf) == TMV_TAG &&
56 AV_RL16(p->buf+4) >= PROBE_MIN_SAMPLE_RATE &&
57 AV_RL16(p->buf+6) >= PROBE_MIN_AUDIO_SIZE &&
58 !p->buf[8] && // compression method
59 p->buf[9] && // char cols
60 p->buf[10]) // char rows
61 return AVPROBE_SCORE_MAX /
62 ((p->buf[9] == 40 && p->buf[10] == 25) ? 1 : 4);
66 static int tmv_read_header(AVFormatContext *s)
68 TMVContext *tmv = s->priv_data;
69 AVIOContext *pb = s->pb;
72 unsigned comp_method, char_cols, char_rows, features;
74 if (avio_rl32(pb) != TMV_TAG)
77 if (!(vst = avformat_new_stream(s, NULL)))
78 return AVERROR(ENOMEM);
80 if (!(ast = avformat_new_stream(s, NULL)))
81 return AVERROR(ENOMEM);
83 ast->codec->sample_rate = avio_rl16(pb);
84 if (!ast->codec->sample_rate) {
85 av_log(s, AV_LOG_ERROR, "invalid sample rate\n");
89 tmv->audio_chunk_size = avio_rl16(pb);
90 if (!tmv->audio_chunk_size) {
91 av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
95 comp_method = avio_r8(pb);
97 av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
102 char_cols = avio_r8(pb);
103 char_rows = avio_r8(pb);
104 tmv->video_chunk_size = char_cols * char_rows * 2;
106 features = avio_r8(pb);
107 if (features & ~(TMV_PADDING | TMV_STEREO)) {
108 av_log(s, AV_LOG_ERROR, "unsupported features 0x%02x\n",
109 features & ~(TMV_PADDING | TMV_STEREO));
113 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
114 ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
115 ast->codec->channels = features & TMV_STEREO ? 2 : 1;
116 ast->codec->bits_per_coded_sample = 8;
117 ast->codec->bit_rate = ast->codec->sample_rate *
118 ast->codec->bits_per_coded_sample;
119 avpriv_set_pts_info(ast, 32, 1, ast->codec->sample_rate);
121 fps.num = ast->codec->sample_rate * ast->codec->channels;
122 fps.den = tmv->audio_chunk_size;
123 av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
125 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
126 vst->codec->codec_id = AV_CODEC_ID_TMV;
127 vst->codec->pix_fmt = PIX_FMT_PAL8;
128 vst->codec->width = char_cols * 8;
129 vst->codec->height = char_rows * 8;
130 avpriv_set_pts_info(vst, 32, fps.den, fps.num);
132 if (features & TMV_PADDING)
134 ((tmv->video_chunk_size + tmv->audio_chunk_size + 511) & ~511) -
135 (tmv->video_chunk_size + tmv->audio_chunk_size);
137 vst->codec->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
138 fps.num * 8) / fps.den;
143 static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
145 TMVContext *tmv = s->priv_data;
146 AVIOContext *pb = s->pb;
147 int ret, pkt_size = tmv->stream_index ?
148 tmv->audio_chunk_size : tmv->video_chunk_size;
153 ret = av_get_packet(pb, pkt, pkt_size);
155 if (tmv->stream_index)
156 avio_skip(pb, tmv->padding);
158 pkt->stream_index = tmv->stream_index;
159 tmv->stream_index ^= 1;
160 pkt->flags |= AV_PKT_FLAG_KEY;
165 static int tmv_read_seek(AVFormatContext *s, int stream_index,
166 int64_t timestamp, int flags)
168 TMVContext *tmv = s->priv_data;
175 (tmv->audio_chunk_size + tmv->video_chunk_size + tmv->padding);
177 if (avio_seek(s->pb, pos + TMV_HEADER_SIZE, SEEK_SET) < 0)
179 tmv->stream_index = 0;
183 AVInputFormat ff_tmv_demuxer = {
185 .long_name = NULL_IF_CONFIG_SMALL("8088flex TMV"),
186 .priv_data_size = sizeof(TMVContext),
187 .read_probe = tmv_probe,
188 .read_header = tmv_read_header,
189 .read_packet = tmv_read_packet,
190 .read_seek = tmv_read_seek,
191 .flags = AVFMT_GENERIC_INDEX,