FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/tmv.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 62 88 70.5%
Functions: 3 4 75.0%
Branches: 25 46 54.3%

Line Branch Exec Source
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
29 #include "libavutil/channel_layout.h"
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 7186 static int tmv_probe(const AVProbeData *p)
56 {
57
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7185 times.
7186 if (AV_RL32(p->buf) == TMV_TAG &&
58
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL16(p->buf+4) >= PROBE_MIN_SAMPLE_RATE &&
59
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL16(p->buf+6) >= PROBE_MIN_AUDIO_SIZE &&
60
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 !p->buf[8] && // compression method
61
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 p->buf[9] && // char cols
62
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 p->buf[10]) // char rows
63 1 return AVPROBE_SCORE_MAX /
64
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 ((p->buf[9] == 40 && p->buf[10] == 25) ? 1 : 4);
65 7185 return 0;
66 }
67
68 1 static int tmv_read_header(AVFormatContext *s)
69 {
70 1 TMVContext *tmv = s->priv_data;
71 1 AVIOContext *pb = s->pb;
72 AVStream *vst, *ast;
73 AVRational fps;
74 unsigned comp_method, char_cols, char_rows, features;
75
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (avio_rl32(pb) != TMV_TAG)
77 return -1;
78
79
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!(vst = avformat_new_stream(s, NULL)))
80 return AVERROR(ENOMEM);
81
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!(ast = avformat_new_stream(s, NULL)))
83 return AVERROR(ENOMEM);
84
85 1 ast->codecpar->sample_rate = avio_rl16(pb);
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ast->codecpar->sample_rate) {
87 av_log(s, AV_LOG_ERROR, "invalid sample rate\n");
88 return -1;
89 }
90
91 1 tmv->audio_chunk_size = avio_rl16(pb);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!tmv->audio_chunk_size) {
93 av_log(s, AV_LOG_ERROR, "invalid audio chunk size\n");
94 return -1;
95 }
96
97 1 comp_method = avio_r8(pb);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (comp_method) {
99 av_log(s, AV_LOG_ERROR, "unsupported compression method %d\n",
100 comp_method);
101 return -1;
102 }
103
104 1 char_cols = avio_r8(pb);
105 1 char_rows = avio_r8(pb);
106 1 tmv->video_chunk_size = char_cols * char_rows * 2;
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!tmv->video_chunk_size) {
108 av_log(s, AV_LOG_ERROR, "invalid video chunk size\n");
109 return AVERROR_INVALIDDATA;
110 }
111
112 1 features = avio_r8(pb);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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
119 1 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
120 1 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_channel_layout_default(&ast->codecpar->ch_layout, !!(features & TMV_STEREO) + 1);
122 1 ast->codecpar->bits_per_coded_sample = 8;
123 1 ast->codecpar->bit_rate = ast->codecpar->sample_rate *
124 1 ast->codecpar->bits_per_coded_sample;
125 1 avpriv_set_pts_info(ast, 32, 1, ast->codecpar->sample_rate);
126
127 1 fps.num = ast->codecpar->sample_rate * ast->codecpar->ch_layout.nb_channels;
128 1 fps.den = tmv->audio_chunk_size;
129 1 av_reduce(&fps.num, &fps.den, fps.num, fps.den, 0xFFFFFFFFLL);
130
131 1 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
132 1 vst->codecpar->codec_id = AV_CODEC_ID_TMV;
133 1 vst->codecpar->format = AV_PIX_FMT_PAL8;
134 1 vst->codecpar->width = char_cols * 8;
135 1 vst->codecpar->height = char_rows * 8;
136 1 avpriv_set_pts_info(vst, 32, fps.den, fps.num);
137
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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 1 vst->codecpar->bit_rate = ((tmv->video_chunk_size + tmv->padding) *
144 1 fps.num * 8) / fps.den;
145
146 1 return 0;
147 }
148
149 222 static int tmv_read_packet(AVFormatContext *s, AVPacket *pkt)
150 {
151 222 TMVContext *tmv = s->priv_data;
152 222 AVIOContext *pb = s->pb;
153 444 int ret, pkt_size = tmv->stream_index ?
154
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 111 times.
222 tmv->audio_chunk_size : tmv->video_chunk_size;
155
156
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 221 times.
222 if (avio_feof(pb))
157 1 return AVERROR_EOF;
158
159 221 ret = av_get_packet(pb, pkt, pkt_size);
160
161
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 111 times.
221 if (tmv->stream_index)
162 110 avio_skip(pb, tmv->padding);
163
164 221 pkt->stream_index = tmv->stream_index;
165 221 tmv->stream_index ^= 1;
166 221 pkt->flags |= AV_PKT_FLAG_KEY;
167
168 221 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
189 const FFInputFormat ff_tmv_demuxer = {
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),
194 .read_probe = tmv_probe,
195 .read_header = tmv_read_header,
196 .read_packet = tmv_read_packet,
197 .read_seek = tmv_read_seek,
198 };
199