Statistics
| Branch: | Revision:

ffmpeg / libavformat / tmv.c @ 7c7d3e09

History | View | Annotate | Download (4.51 KB)

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