]> git.sesse.net Git - ffmpeg/blob - libavformat/tmv.c
Do not allow 0 sample rate in TMV demuxer
[ffmpeg] / libavformat / tmv.c
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 /**
23  * 8088flex TMV file demuxer
24  * @file libavformat/tmv.c
25  * @author Daniel Verkamp
26  * @sa http://www.oldskool.org/pc/8088_Corruption
27  */
28
29 #include "libavutil/intreadwrite.h"
30 #include "avformat.h"
31
32 enum {
33     TMV_PADDING = 0x01,
34     TMV_STEREO  = 0x02,
35 };
36
37 #define TMV_TAG MKTAG('T', 'M', 'A', 'V')
38
39 typedef struct TMVContext {
40     unsigned audio_chunk_size;
41     unsigned video_chunk_size;
42     unsigned padding;
43     unsigned stream_index;
44 } TMVContext;
45
46 #define PROBE_MIN_SAMPLE_RATE 5000
47 #define PROBE_MAX_FPS         120
48 #define PROBE_MIN_AUDIO_SIZE  (PROBE_MIN_SAMPLE_RATE / PROBE_MAX_FPS)
49
50 static int tmv_probe(AVProbeData *p)
51 {
52     if (AV_RL32(p->buf)   == TMV_TAG &&
53         AV_RL16(p->buf+4) >= PROBE_MIN_SAMPLE_RATE &&
54         AV_RL16(p->buf+6) >= PROBE_MIN_AUDIO_SIZE  &&
55                !p->buf[8] && // compression method
56                 p->buf[9] && // char cols
57                 p->buf[10])  // char rows
58         return AVPROBE_SCORE_MAX / (p->buf[9] == 40 && p->buf[10] == 25)? 1 : 4;
59     return 0;
60 }
61
62 static int tmv_read_header(AVFormatContext *s, AVFormatParameters *ap)
63 {
64     TMVContext *tmv   = s->priv_data;
65     ByteIOContext *pb = s->pb;
66     AVStream *vst, *ast;
67     AVRational fps;
68     unsigned comp_method, char_cols, char_rows, features;
69
70     if (get_le32(pb) != TMV_TAG)
71         return -1;
72
73     if (!(vst = av_new_stream(s, 0)))
74         return AVERROR(ENOMEM);
75
76     if (!(ast = av_new_stream(s, 0)))
77         return AVERROR(ENOMEM);
78
79     ast->codec->sample_rate = get_le16(pb);
80     if (!ast->codec->sample_rate) {
81         av_log(s, AV_LOG_ERROR, "invalid sample rate\n");
82         return -1;
83     }
84
85     tmv->audio_chunk_size   = get_le16(pb);
86     if (!tmv->audio_chunk_size) {
87         av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
88         return -1;
89     }
90
91     comp_method             = get_byte(pb);
92     if (comp_method) {
93         av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
94                comp_method);
95         return -1;
96     }
97
98     char_cols = get_byte(pb);
99     char_rows = get_byte(pb);
100     tmv->video_chunk_size = char_cols * char_rows * 2;
101
102     features  = get_byte(pb);
103     if (features & ~(TMV_PADDING | TMV_STEREO)) {
104         av_log(s, AV_LOG_ERROR, "unsupported features 0x%02x\n",
105                features & ~(TMV_PADDING | TMV_STEREO));
106         return -1;
107     }
108
109     ast->codec->codec_type            = CODEC_TYPE_AUDIO;
110     ast->codec->codec_id              = CODEC_ID_PCM_U8;
111     ast->codec->sample_fmt            = SAMPLE_FMT_U8;
112     ast->codec->channels              = features & TMV_STEREO ? 2 : 1;
113     ast->codec->bits_per_coded_sample = 8;
114     ast->codec->bit_rate              = ast->codec->sample_rate *
115                                         ast->codec->bits_per_coded_sample;
116     av_set_pts_info(ast, 32, 1, ast->codec->sample_rate);
117
118     fps.num = ast->codec->sample_rate * ast->codec->channels;
119     fps.den = tmv->audio_chunk_size;
120     av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
121
122     vst->codec->codec_type = CODEC_TYPE_VIDEO;
123     vst->codec->codec_id   = CODEC_ID_TMV;
124     vst->codec->pix_fmt    = PIX_FMT_PAL8;
125     vst->codec->width      = char_cols * 8;
126     vst->codec->height     = char_rows * 8;
127     av_set_pts_info(vst, 32, fps.den, fps.num);
128
129     if (features & TMV_PADDING)
130         tmv->padding =
131             ((tmv->video_chunk_size + tmv->audio_chunk_size + 511) & ~511) -
132              (tmv->video_chunk_size + tmv->audio_chunk_size);
133
134     vst->codec->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
135                             fps.num * 8) / fps.den;
136
137     return 0;
138 }
139
140 static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
141 {
142     TMVContext *tmv   = s->priv_data;
143     ByteIOContext *pb = s->pb;
144     int ret, pkt_size = tmv->stream_index ?
145                         tmv->audio_chunk_size : tmv->video_chunk_size;
146
147     if (url_feof(pb))
148         return AVERROR_EOF;
149
150     ret = av_get_packet(pb, pkt, pkt_size);
151
152     if (tmv->stream_index)
153         url_fskip(pb, tmv->padding);
154
155     pkt->stream_index  = tmv->stream_index;
156     tmv->stream_index ^= 1;
157     pkt->flags        |= PKT_FLAG_KEY;
158
159     return ret;
160 }
161
162 AVInputFormat tmv_demuxer = {
163     "tmv",
164     NULL_IF_CONFIG_SMALL("8088flex TMV"),
165     sizeof(TMVContext),
166     tmv_probe,
167     tmv_read_header,
168     tmv_read_packet,
169     .flags = AVFMT_GENERIC_INDEX,
170 };