1 /*
2 * Sega FILM Format (CPK) Muxer
3 * Copyright (C) 2003 The FFmpeg project
4 * Copyright (C) 2018 Misty De Meo
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Sega FILM (.cpk) file muxer
26 * @author Misty De Meo <misty@brew.sh>
27 *
28 * @see For more information regarding the Sega FILM file format, visit:
29 * http://wiki.multimedia.cx/index.php?title=Sega_FILM
30 */
31
38
45
47 {
51 uint32_t info1, info2;
53
55
56 /* Sega Cinepak has an extra two-byte header; write dummy data there,
57 * then adjust the cvid header to accommodate for the extra size */
60 /* Already Sega Cinepak, so no need to reformat the packets */
61 if (encoded_buf_size !=
pkt->
size && (
pkt->
size % encoded_buf_size) != 0) {
63 } else {
64 /* In Sega Cinepak, the reported size in the Cinepak header is
65 * 8 bytes too short. However, the size in the STAB section of the header
66 * is correct, taking into account the extra two bytes. */
69
73 }
74 } else {
75 /* Other formats can just be written as-is */
77 }
78
79 /* Add the 16-byte sample info entry to the dynamic buffer
80 * for the STAB chunk in the header */
86 /* Always the same, carries no more information than "this is audio" */
87 info1 = 0xFFFFFFFF;
88 info2 = 1;
89 } else {
92 /* The top bit being set indicates a key frame */
95 }
98
100 }
101
103 {
104 /* 0 (PCM) and 2 (ADX) are the only known values */
108 return 0;
110 return 2;
111 default:
112 return -1;
113 }
114 }
115
117 {
120
123
128 av_log(format_context,
AV_LOG_ERROR,
"Sega FILM allows a maximum of one audio stream.\n");
130 }
133 "Incompatible audio stream format.\n");
135 }
137 }
138
141 av_log(format_context,
AV_LOG_ERROR,
"Sega FILM allows a maximum of one video stream.\n");
143 }
147 "Incompatible video stream format.\n");
149 }
152 "Pixel format must be rgb24.\n");
154 }
156 }
157 }
158
162 }
166
167 return 0;
168 }
169
171 unsigned header_size)
172 {
176
179
180 return 0;
181 }
182
184 {
186 unsigned stabsize, headersize, packet_count;
190
191 /* Calculate how much we need to reserve for the header;
192 * this is the amount the rest of the data will be shifted up by. */
194 if (headersize < 64) {
197 }
198 packet_count = (headersize - 64) / 16;
199 stabsize = 16 + 16 * packet_count;
200 headersize = 16 + /* FILM header base */
201 32 + /* FDSC chunk */
202 stabsize;
203
204 /* Write the header at the position in the buffer reserved for it.
205 * First, write the FILM header; this is very simple */
207 bytestream_put_be32(&ptr,
MKBETAG(
'F',
'I',
'L',
'M'));
208 bytestream_put_be32(&ptr, headersize);
209 /* This seems to be okay to hardcode, since this muxer targets 1.09 features;
210 * videos produced by this muxer are readable by 1.08 and lower players. */
211 bytestream_put_be32(&ptr,
MKBETAG(
'1',
'.',
'0',
'9'));
212 /* I have no idea what the next four bytes do, might be reserved */
213 ptr += 4;
214
215 /* Next write the FDSC (file description) chunk */
216 bytestream_put_be32(&ptr,
MKBETAG(
'F',
'D',
'S',
'C'));
217 bytestream_put_be32(&ptr, 0x20); /* Size of FDSC chunk */
218
220
221 /* The only two supported codecs; raw video is rare */
222 switch (
video->codecpar->codec_id) {
224 bytestream_put_be32(&ptr,
MKBETAG(
'c',
'v',
'i',
'd'));
225 break;
227 bytestream_put_be32(&ptr,
MKBETAG(
'r',
'a',
'w',
' '));
228 break;
229 }
230
231 bytestream_put_be32(&ptr,
video->codecpar->height);
232 bytestream_put_be32(&ptr,
video->codecpar->width);
233 bytestream_put_byte(&ptr, 24); /* Bits per pixel - observed to always be 24 */
234
238
241 bytestream_put_byte(&ptr, audio_codec); /* Compression - 0 is PCM, 2 is ADX */
243 } else {
244 /* If there is no audio, all the audio fields should be set to zero.
245 * ffio_fill() already did this for us. */
246 ptr += 1 + 1 + 1 + 2;
247 }
248
249 /* I have no idea what this pair of fields does either, might be reserved */
250 ptr += 4 + 2;
251
252 /* Finally, write the STAB (sample table) chunk */
253 bytestream_put_be32(&ptr,
MKBETAG(
'S',
'T',
'A',
'B'));
254 bytestream_put_be32(&ptr, stabsize);
255 /* Framerate base frequency. Here we're assuming that the frame rate is even.
256 * In real world Sega FILM files, there are usually a couple of approaches:
257 * a) framerate base frequency is the same as the framerate, and ticks
258 * increment by 1 every frame, or
259 * b) framerate base frequency is a much larger number, and ticks
260 * increment by larger steps every frame.
261 * The latter occurs even in cases where the frame rate is even; for example, in
262 * Lunar: Silver Star Story, the base frequency is 600 and each frame, the ticks
263 * are incremented by 25 for an evenly spaced framerate of 24fps. */
265
266 bytestream_put_be32(&ptr, packet_count);
267
268 /* Finally, shift the data and write out the header. */
272
273 return 0;
274 }
275
277 {
279
281 }
282
284 .
p.
name =
"film_cpk",
286 .p.extensions = "cpk",
294 };