2 * Sega FILM Format (CPK) Muxer
3 * Copyright (C) 2003 The FFmpeg project
4 * Copyright (C) 2018 Misty De Meo
6 * This file is part of FFmpeg.
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.
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.
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
25 * Sega FILM (.cpk) file muxer
26 * @author Misty De Meo <misty@brew.sh>
28 * @see For more information regarding the Sega FILM file format, visit:
29 * http://wiki.multimedia.cx/index.php?title=Sega_FILM
32 #include "libavutil/intreadwrite.h"
35 #include "avio_internal.h"
37 typedef struct FILMPacket {
44 struct FILMPacket *next;
47 typedef struct FILMOutputContext {
56 static int film_write_packet_to_header(AVFormatContext *format_context, FILMPacket *pkt)
58 AVIOContext *pb = format_context->pb;
59 /* The bits in these two 32-bit integers contain info about the contents of this sample */
64 /* Always the same, carries no more information than "this is audio" */
69 info2 = pkt->duration;
70 /* The top bit being set indicates a key frame */
75 /* Write the 16-byte sample info packet to the STAB chunk in the header */
76 avio_wb32(pb, pkt->index);
77 avio_wb32(pb, pkt->size);
84 static int film_write_packet(AVFormatContext *format_context, AVPacket *pkt)
87 AVIOContext *pb = format_context->pb;
88 FILMOutputContext *film = format_context->priv_data;
89 int encoded_buf_size = 0;
90 enum AVCodecID codec_id;
92 /* Track the metadata used to write the header and add it to the linked list */
93 metadata = av_mallocz(sizeof(FILMPacket));
95 return AVERROR(ENOMEM);
96 metadata->audio = pkt->stream_index == film->audio_index;
97 metadata->keyframe = pkt->flags & AV_PKT_FLAG_KEY;
98 metadata->pts = pkt->pts;
99 metadata->duration = pkt->duration;
100 metadata->size = pkt->size;
101 if (film->last == NULL) {
104 metadata->index = film->last->index + film->last->size;
105 film->last->next = metadata;
107 metadata->next = NULL;
108 if (film->start == NULL)
109 film->start = metadata;
110 film->packet_count++;
111 film->last = metadata;
113 codec_id = format_context->streams[pkt->stream_index]->codecpar->codec_id;
115 /* Sega Cinepak has an extra two-byte header; write dummy data there,
116 * then adjust the cvid header to accommodate for the extra size */
117 if (codec_id == AV_CODEC_ID_CINEPAK) {
118 encoded_buf_size = AV_RB24(&pkt->data[1]);
119 /* Already Sega Cinepak, so no need to reformat the packets */
120 if (encoded_buf_size != pkt->size && (pkt->size % encoded_buf_size) != 0) {
121 avio_write(pb, pkt->data, pkt->size);
123 uint8_t padding[2] = {0, 0};
124 /* In Sega Cinepak, the reported size in the Cinepak header is
125 * 8 bytes too short. However, the size in the STAB section of the header
126 * is correct, taking into account the extra two bytes. */
127 AV_WB24(&pkt->data[1], pkt->size - 8 + 2);
130 avio_write(pb, pkt->data, 10);
131 avio_write(pb, padding, 2);
132 avio_write(pb, &pkt->data[10], pkt->size - 10);
135 /* Other formats can just be written as-is */
136 avio_write(pb, pkt->data, pkt->size);
142 static int get_audio_codec_id(enum AVCodecID codec_id)
144 /* 0 (PCM) and 2 (ADX) are the only known values */
146 case AV_CODEC_ID_PCM_S8_PLANAR:
147 case AV_CODEC_ID_PCM_S16BE_PLANAR:
149 case AV_CODEC_ID_ADPCM_ADX:
156 static int film_init(AVFormatContext *format_context)
158 AVStream *audio = NULL;
159 FILMOutputContext *film = format_context->priv_data;
160 film->audio_index = -1;
161 film->video_index = -1;
163 film->packet_count = 0;
167 for (int i = 0; i < format_context->nb_streams; i++) {
168 AVStream *st = format_context->streams[i];
169 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
170 if (film->audio_index > -1) {
171 av_log(format_context, AV_LOG_ERROR, "Sega FILM allows a maximum of one audio stream.\n");
172 return AVERROR(EINVAL);
174 film->audio_index = i;
178 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
179 if (film->video_index > -1) {
180 av_log(format_context, AV_LOG_ERROR, "Sega FILM allows a maximum of one video stream.\n");
181 return AVERROR(EINVAL);
183 if (st->codecpar->codec_id != AV_CODEC_ID_CINEPAK &&
184 st->codecpar->codec_id != AV_CODEC_ID_RAWVIDEO) {
185 av_log(format_context, AV_LOG_ERROR,
186 "Incompatible video stream format.\n");
187 return AVERROR(EINVAL);
189 if (st->codecpar->format != AV_PIX_FMT_RGB24) {
190 av_log(format_context, AV_LOG_ERROR,
191 "Pixel format must be rgb24.\n");
192 return AVERROR(EINVAL);
194 film->video_index = i;
198 if (film->video_index == -1) {
199 av_log(format_context, AV_LOG_ERROR, "No video stream present.\n");
200 return AVERROR(EINVAL);
203 if (audio != NULL && get_audio_codec_id(audio->codecpar->codec_id) < 0) {
204 av_log(format_context, AV_LOG_ERROR, "Incompatible audio stream format.\n");
205 return AVERROR(EINVAL);
211 static int shift_data(AVFormatContext *format_context, int64_t shift_size)
214 int64_t pos, pos_end;
215 uint8_t *buf, *read_buf[2];
218 AVIOContext *read_pb;
220 buf = av_malloc(shift_size * 2);
222 return AVERROR(ENOMEM);
224 read_buf[1] = buf + shift_size;
226 /* Write the header at the beginning of the file, shifting all content as necessary;
227 * based on the approach used by MOV faststart. */
228 avio_flush(format_context->pb);
229 ret = format_context->io_open(format_context, &read_pb, format_context->url, AVIO_FLAG_READ, NULL);
231 av_log(format_context, AV_LOG_ERROR, "Unable to re-open %s output file to "
232 "write the header\n", format_context->url);
237 /* mark the end of the shift to up to the last data we wrote, and get ready
239 pos_end = avio_tell(format_context->pb);
240 avio_seek(format_context->pb, shift_size, SEEK_SET);
242 /* start reading at where the new header will be placed */
243 avio_seek(read_pb, 0, SEEK_SET);
244 pos = avio_tell(read_pb);
246 #define READ_BLOCK do { \
247 read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], shift_size); \
251 /* shift data by chunk of at most shift_size */
256 n = read_size[read_buf_id];
259 avio_write(format_context->pb, read_buf[read_buf_id], n);
261 } while (pos < pos_end);
262 ff_format_io_close(format_context, &read_pb);
268 static int film_write_header(AVFormatContext *format_context)
271 int64_t sample_table_size, stabsize, headersize;
273 AVIOContext *pb = format_context->pb;
274 FILMOutputContext *film = format_context->priv_data;
275 FILMPacket *prev, *packet;
276 AVStream *audio = NULL;
277 AVStream *video = NULL;
279 /* Calculate how much we need to reserve for the header;
280 * this is the amount the rest of the data will be shifted up by. */
281 sample_table_size = film->packet_count * 16;
282 stabsize = 16 + sample_table_size;
283 headersize = 16 + /* FILM header base */
284 32 + /* FDSC chunk */
287 ret = shift_data(format_context, headersize);
290 /* Seek back to the beginning to start writing the header now */
291 avio_seek(pb, 0, SEEK_SET);
293 if (film->audio_index > -1)
294 audio = format_context->streams[film->audio_index];
295 if (film->video_index > -1)
296 video = format_context->streams[film->video_index];
299 audio_codec = get_audio_codec_id(audio->codecpar->codec_id);
300 if (audio_codec < 0) {
301 av_log(format_context, AV_LOG_ERROR, "Incompatible audio stream format.\n");
302 return AVERROR(EINVAL);
306 /* First, write the FILM header; this is very simple */
308 ffio_wfourcc(pb, "FILM");
309 avio_wb32(pb, 48 + stabsize);
310 /* This seems to be okay to hardcode, since this muxer targets 1.09 features;
311 * videos produced by this muxer are readable by 1.08 and lower players. */
312 ffio_wfourcc(pb, "1.09");
313 /* I have no idea what this field does, might be reserved */
316 /* Next write the FDSC (file description) chunk */
317 ffio_wfourcc(pb, "FDSC");
318 avio_wb32(pb, 0x20); /* Size of FDSC chunk */
320 /* The only two supported codecs; raw video is rare */
321 switch (video->codecpar->codec_id) {
322 case AV_CODEC_ID_CINEPAK:
323 ffio_wfourcc(pb, "cvid");
325 case AV_CODEC_ID_RAWVIDEO:
326 ffio_wfourcc(pb, "raw ");
330 avio_wb32(pb, video->codecpar->height);
331 avio_wb32(pb, video->codecpar->width);
332 avio_w8(pb, 24); /* Bits per pixel - observed to always be 24 */
335 avio_w8(pb, audio->codecpar->channels); /* Audio channels */
336 avio_w8(pb, audio->codecpar->bits_per_coded_sample); /* Audio bit depth */
337 avio_w8(pb, audio_codec); /* Compression - 0 is PCM, 2 is ADX */
338 avio_wb16(pb, audio->codecpar->sample_rate); /* Audio sampling rate */
340 /* Set all these fields to 0 if there's no audio */
347 /* I have no idea what this pair of fields does either, might be reserved */
351 /* Finally, write the STAB (sample table) chunk */
352 ffio_wfourcc(pb, "STAB");
353 avio_wb32(pb, 16 + (film->packet_count * 16));
354 /* Framerate base frequency. Here we're assuming that the frame rate is even.
355 * In real world Sega FILM files, there are usually a couple of approaches:
356 * a) framerate base frequency is the same as the framerate, and ticks
357 * increment by 1 every frame, or
358 * b) framerate base frequency is a much larger number, and ticks
359 * increment by larger steps every frame.
360 * The latter occurs even in cases where the frame rate is even; for example, in
361 * Lunar: Silver Star Story, the base frequency is 600 and each frame, the ticks
362 * are incremented by 25 for an evenly spaced framerate of 24fps. */
363 avio_wb32(pb, av_q2d(av_inv_q(video->time_base)));
365 avio_wb32(pb, film->packet_count);
367 /* Finally, write out each packet's data to the header */
368 packet = film->start;
369 while (packet != NULL) {
370 film_write_packet_to_header(format_context, packet);
372 packet = packet->next;
379 AVOutputFormat ff_segafilm_muxer = {
381 .long_name = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
383 .priv_data_size = sizeof(FILMOutputContext),
384 .audio_codec = AV_CODEC_ID_PCM_S16BE_PLANAR,
385 .video_codec = AV_CODEC_ID_CINEPAK,
387 .write_trailer = film_write_header,
388 .write_packet = film_write_packet,