FFmpeg
tmv.c
Go to the documentation of this 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 /**
23  * @file
24  * 8088flex TMV file demuxer
25  * @author Daniel Verkamp
26  * @see http://www.oldskool.org/pc/8088_Corruption
27  */
28 
30 #include "libavutil/intreadwrite.h"
31 #include "avformat.h"
32 #include "demux.h"
33 #include "internal.h"
34 
35 enum {
36  TMV_PADDING = 0x01,
37  TMV_STEREO = 0x02,
38 };
39 
40 #define TMV_TAG MKTAG('T', 'M', 'A', 'V')
41 
42 typedef struct TMVContext {
43  unsigned audio_chunk_size;
44  unsigned video_chunk_size;
45  unsigned padding;
46  unsigned stream_index;
47 } TMVContext;
48 
49 #define TMV_HEADER_SIZE 12
50 
51 #define PROBE_MIN_SAMPLE_RATE 5000
52 #define PROBE_MAX_FPS 120
53 #define PROBE_MIN_AUDIO_SIZE (PROBE_MIN_SAMPLE_RATE / PROBE_MAX_FPS)
54 
55 static int tmv_probe(const AVProbeData *p)
56 {
57  if (AV_RL32(p->buf) == TMV_TAG &&
59  AV_RL16(p->buf+6) >= PROBE_MIN_AUDIO_SIZE &&
60  !p->buf[8] && // compression method
61  p->buf[9] && // char cols
62  p->buf[10]) // char rows
63  return AVPROBE_SCORE_MAX /
64  ((p->buf[9] == 40 && p->buf[10] == 25) ? 1 : 4);
65  return 0;
66 }
67 
69 {
70  TMVContext *tmv = s->priv_data;
71  AVIOContext *pb = s->pb;
72  AVStream *vst, *ast;
73  AVRational fps;
74  unsigned comp_method, char_cols, char_rows, features;
75 
76  if (avio_rl32(pb) != TMV_TAG)
77  return -1;
78 
79  if (!(vst = avformat_new_stream(s, NULL)))
80  return AVERROR(ENOMEM);
81 
82  if (!(ast = avformat_new_stream(s, NULL)))
83  return AVERROR(ENOMEM);
84 
85  ast->codecpar->sample_rate = avio_rl16(pb);
86  if (!ast->codecpar->sample_rate) {
87  av_log(s, AV_LOG_ERROR, "invalid sample rate\n");
88  return -1;
89  }
90 
91  tmv->audio_chunk_size = avio_rl16(pb);
92  if (!tmv->audio_chunk_size) {
93  av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
94  return -1;
95  }
96 
97  comp_method = avio_r8(pb);
98  if (comp_method) {
99  av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
100  comp_method);
101  return -1;
102  }
103 
104  char_cols = avio_r8(pb);
105  char_rows = avio_r8(pb);
106  tmv->video_chunk_size = char_cols * char_rows * 2;
107  if (!tmv->video_chunk_size) {
108  av_log(s, AV_LOG_ERROR, "invalid video chunk size\n");
109  return AVERROR_INVALIDDATA;
110  }
111 
112  features = avio_r8(pb);
113  if (features & ~(TMV_PADDING | TMV_STEREO)) {
114  av_log(s, AV_LOG_ERROR, "unsupported features 0x%02x\n",
115  features & ~(TMV_PADDING | TMV_STEREO));
116  return -1;
117  }
118 
121  av_channel_layout_default(&ast->codecpar->ch_layout, !!(features & TMV_STEREO) + 1);
123  ast->codecpar->bit_rate = ast->codecpar->sample_rate *
125  avpriv_set_pts_info(ast, 32, 1, ast->codecpar->sample_rate);
126 
128  fps.den = tmv->audio_chunk_size;
129  av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
130 
134  vst->codecpar->width = char_cols * 8;
135  vst->codecpar->height = char_rows * 8;
136  avpriv_set_pts_info(vst, 32, fps.den, fps.num);
137 
138  if (features & TMV_PADDING)
139  tmv->padding =
140  ((tmv->video_chunk_size + tmv->audio_chunk_size + 511) & ~511) -
141  (tmv->video_chunk_size + tmv->audio_chunk_size);
142 
143  vst->codecpar->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
144  fps.num * 8) / fps.den;
145 
146  return 0;
147 }
148 
150 {
151  TMVContext *tmv = s->priv_data;
152  AVIOContext *pb = s->pb;
153  int ret, pkt_size = tmv->stream_index ?
155 
156  if (avio_feof(pb))
157  return AVERROR_EOF;
158 
159  ret = av_get_packet(pb, pkt, pkt_size);
160 
161  if (tmv->stream_index)
162  avio_skip(pb, tmv->padding);
163 
164  pkt->stream_index = tmv->stream_index;
165  tmv->stream_index ^= 1;
167 
168  return ret;
169 }
170 
171 static int tmv_read_seek(AVFormatContext *s, int stream_index,
172  int64_t timestamp, int flags)
173 {
174  TMVContext *tmv = s->priv_data;
175  int64_t pos;
176 
177  if (stream_index)
178  return -1;
179 
180  pos = timestamp *
181  (tmv->audio_chunk_size + tmv->video_chunk_size + tmv->padding);
182 
183  if (avio_seek(s->pb, pos + TMV_HEADER_SIZE, SEEK_SET) < 0)
184  return -1;
185  tmv->stream_index = 0;
186  return 0;
187 }
188 
190  .p.name = "tmv",
191  .p.long_name = NULL_IF_CONFIG_SMALL("8088flex TMV"),
192  .p.flags = AVFMT_GENERIC_INDEX,
193  .priv_data_size = sizeof(TMVContext),
198 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
tmv_read_header
static int tmv_read_header(AVFormatContext *s)
Definition: tmv.c:68
TMVContext
Definition: tmv.c:42
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
TMV_PADDING
@ TMV_PADDING
Definition: tmv.c:36
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:862
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
tmv_read_packet
static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: tmv.c:149
ff_tmv_demuxer
const FFInputFormat ff_tmv_demuxer
Definition: tmv.c:189
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
TMV_HEADER_SIZE
#define TMV_HEADER_SIZE
Definition: tmv.c:49
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
AVRational::num
int num
Numerator.
Definition: rational.h:59
TMVContext::padding
unsigned padding
Definition: tmv.c:45
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
TMVContext::video_chunk_size
unsigned video_chunk_size
Definition: tmv.c:44
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:538
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
tmv_probe
static int tmv_probe(const AVProbeData *p)
Definition: tmv.c:55
TMVContext::stream_index
unsigned stream_index
Definition: tmv.c:46
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
TMV_TAG
#define TMV_TAG
Definition: tmv.c:40
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:834
tmv_read_seek
static int tmv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: tmv.c:171
AVCodecParameters::height
int height
Definition: codec_par.h:135
demux.h
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:91
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
channel_layout.h
PROBE_MIN_AUDIO_SIZE
#define PROBE_MIN_AUDIO_SIZE
Definition: tmv.c:53
AVRational::den
int den
Denominator.
Definition: rational.h:60
PROBE_MIN_SAMPLE_RATE
#define PROBE_MIN_SAMPLE_RATE
Definition: tmv.c:51
TMV_STEREO
@ TMV_STEREO
Definition: tmv.c:37
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:333
AVCodecParameters::format
int format
Definition: codec_par.h:92
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FFInputFormat
Definition: demux.h:37
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
TMVContext::audio_chunk_size
unsigned audio_chunk_size
Definition: tmv.c:43
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_TMV
@ AV_CODEC_ID_TMV
Definition: codec_id.h:178
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346