FFmpeg  4.4.6
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 "internal.h"
33 
34 enum {
35  TMV_PADDING = 0x01,
36  TMV_STEREO = 0x02,
37 };
38 
39 #define TMV_TAG MKTAG('T', 'M', 'A', 'V')
40 
41 typedef struct TMVContext {
42  unsigned audio_chunk_size;
43  unsigned video_chunk_size;
44  unsigned padding;
45  unsigned stream_index;
46 } TMVContext;
47 
48 #define TMV_HEADER_SIZE 12
49 
50 #define PROBE_MIN_SAMPLE_RATE 5000
51 #define PROBE_MAX_FPS 120
52 #define PROBE_MIN_AUDIO_SIZE (PROBE_MIN_SAMPLE_RATE / PROBE_MAX_FPS)
53 
54 static int tmv_probe(const AVProbeData *p)
55 {
56  if (AV_RL32(p->buf) == TMV_TAG &&
58  AV_RL16(p->buf+6) >= PROBE_MIN_AUDIO_SIZE &&
59  !p->buf[8] && // compression method
60  p->buf[9] && // char cols
61  p->buf[10]) // char rows
62  return AVPROBE_SCORE_MAX /
63  ((p->buf[9] == 40 && p->buf[10] == 25) ? 1 : 4);
64  return 0;
65 }
66 
68 {
69  TMVContext *tmv = s->priv_data;
70  AVIOContext *pb = s->pb;
71  AVStream *vst, *ast;
72  AVRational fps;
73  unsigned comp_method, char_cols, char_rows, features;
74 
75  if (avio_rl32(pb) != TMV_TAG)
76  return -1;
77 
78  if (!(vst = avformat_new_stream(s, NULL)))
79  return AVERROR(ENOMEM);
80 
81  if (!(ast = avformat_new_stream(s, NULL)))
82  return AVERROR(ENOMEM);
83 
84  ast->codecpar->sample_rate = avio_rl16(pb);
85  if (!ast->codecpar->sample_rate) {
86  av_log(s, AV_LOG_ERROR, "invalid sample rate\n");
87  return -1;
88  }
89 
90  tmv->audio_chunk_size = avio_rl16(pb);
91  if (!tmv->audio_chunk_size) {
92  av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
93  return -1;
94  }
95 
96  comp_method = avio_r8(pb);
97  if (comp_method) {
98  av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
99  comp_method);
100  return -1;
101  }
102 
103  char_cols = avio_r8(pb);
104  char_rows = avio_r8(pb);
105  tmv->video_chunk_size = char_cols * char_rows * 2;
106  if (!tmv->video_chunk_size) {
107  av_log(s, AV_LOG_ERROR, "invalid video chunk size\n");
108  return AVERROR_INVALIDDATA;
109  }
110 
111  features = avio_r8(pb);
112  if (features & ~(TMV_PADDING | TMV_STEREO)) {
113  av_log(s, AV_LOG_ERROR, "unsupported features 0x%02x\n",
114  features & ~(TMV_PADDING | TMV_STEREO));
115  return -1;
116  }
117 
120  if (features & TMV_STEREO) {
121  ast->codecpar->channels = 2;
123  } else {
124  ast->codecpar->channels = 1;
126  }
128  ast->codecpar->bit_rate = ast->codecpar->sample_rate *
130  avpriv_set_pts_info(ast, 32, 1, ast->codecpar->sample_rate);
131 
132  fps.num = ast->codecpar->sample_rate * ast->codecpar->channels;
133  fps.den = tmv->audio_chunk_size;
134  av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
135 
139  vst->codecpar->width = char_cols * 8;
140  vst->codecpar->height = char_rows * 8;
141  avpriv_set_pts_info(vst, 32, fps.den, fps.num);
142 
143  if (features & TMV_PADDING)
144  tmv->padding =
145  ((tmv->video_chunk_size + tmv->audio_chunk_size + 511) & ~511) -
146  (tmv->video_chunk_size + tmv->audio_chunk_size);
147 
148  vst->codecpar->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
149  fps.num * 8) / fps.den;
150 
151  return 0;
152 }
153 
155 {
156  TMVContext *tmv = s->priv_data;
157  AVIOContext *pb = s->pb;
158  int ret, pkt_size = tmv->stream_index ?
160 
161  if (avio_feof(pb))
162  return AVERROR_EOF;
163 
164  ret = av_get_packet(pb, pkt, pkt_size);
165 
166  if (tmv->stream_index)
167  avio_skip(pb, tmv->padding);
168 
169  pkt->stream_index = tmv->stream_index;
170  tmv->stream_index ^= 1;
172 
173  return ret;
174 }
175 
176 static int tmv_read_seek(AVFormatContext *s, int stream_index,
177  int64_t timestamp, int flags)
178 {
179  TMVContext *tmv = s->priv_data;
180  int64_t pos;
181 
182  if (stream_index)
183  return -1;
184 
185  pos = timestamp *
186  (tmv->audio_chunk_size + tmv->video_chunk_size + tmv->padding);
187 
188  if (avio_seek(s->pb, pos + TMV_HEADER_SIZE, SEEK_SET) < 0)
189  return -1;
190  tmv->stream_index = 0;
191  return 0;
192 }
193 
195  .name = "tmv",
196  .long_name = NULL_IF_CONFIG_SMALL("8088flex TMV"),
197  .priv_data_size = sizeof(TMVContext),
203 };
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
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:310
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:364
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:734
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:750
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RL32
Definition: intreadwrite.h:146
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
audio channel layout utility functions
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:318
@ AV_CODEC_ID_TMV
Definition: codec_id.h:175
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4945
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVInputFormat ff_tmv_demuxer
Definition: tmv.c:194
static int tmv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: tmv.c:176
#define PROBE_MIN_AUDIO_SIZE
Definition: tmv.c:52
@ TMV_PADDING
Definition: tmv.c:35
@ TMV_STEREO
Definition: tmv.c:36
#define TMV_HEADER_SIZE
Definition: tmv.c:48
static int tmv_probe(const AVProbeData *p)
Definition: tmv.c:54
#define TMV_TAG
Definition: tmv.c:39
static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: tmv.c:154
static int tmv_read_header(AVFormatContext *s)
Definition: tmv.c:67
#define PROBE_MIN_SAMPLE_RATE
Definition: tmv.c:50
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
unsigned int pos
Definition: spdifenc.c:412
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
int width
Video only.
Definition: codec_par.h:126
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
Definition: tmv.c:41
unsigned stream_index
Definition: tmv.c:45
unsigned video_chunk_size
Definition: tmv.c:43
unsigned audio_chunk_size
Definition: tmv.c:42
unsigned padding
Definition: tmv.c:44
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59