LCOV - code coverage report
Current view: top level - libavformat - segafilmenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 184 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 6 0.0 %

          Line data    Source code
       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             : 
      32             : #include "libavutil/intreadwrite.h"
      33             : #include "avformat.h"
      34             : #include "internal.h"
      35             : #include "avio_internal.h"
      36             : 
      37             : typedef struct FILMPacket {
      38             :     int audio;
      39             :     int keyframe;
      40             :     int32_t pts;
      41             :     int32_t duration;
      42             :     int32_t size;
      43             :     int32_t index;
      44             :     struct FILMPacket *next;
      45             : } FILMPacket;
      46             : 
      47             : typedef struct FILMOutputContext {
      48             :     const AVClass *class;
      49             :     int audio_index;
      50             :     int video_index;
      51             :     int64_t stab_pos;
      52             :     FILMPacket *start;
      53             :     FILMPacket *last;
      54             :     int64_t packet_count;
      55             : } FILMOutputContext;
      56             : 
      57           0 : static int film_write_packet_to_header(AVFormatContext *format_context, FILMPacket *pkt)
      58             : {
      59           0 :     AVIOContext *pb = format_context->pb;
      60             :     /* The bits in these two 32-bit integers contain info about the contents of this sample */
      61           0 :     int32_t info1 = 0;
      62           0 :     int32_t info2 = 0;
      63             : 
      64           0 :     if (pkt->audio) {
      65             :         /* Always the same, carries no more information than "this is audio" */
      66           0 :         info1 = 0xFFFFFFFF;
      67           0 :         info2 = 1;
      68             :     } else {
      69           0 :         info1 = pkt->pts;
      70           0 :         info2 = pkt->duration;
      71             :         /* The top bit being set indicates a key frame */
      72           0 :         if (!pkt->keyframe)
      73           0 :             info1 |= (1 << 31);
      74             :     }
      75             : 
      76             :     /* Write the 16-byte sample info packet to the STAB chunk in the header */
      77           0 :     avio_wb32(pb, pkt->index);
      78           0 :     avio_wb32(pb, pkt->size);
      79           0 :     avio_wb32(pb, info1);
      80           0 :     avio_wb32(pb, info2);
      81             : 
      82           0 :     return 0;
      83             : }
      84             : 
      85           0 : static int film_write_packet(AVFormatContext *format_context, AVPacket *pkt)
      86             : {
      87             :     FILMPacket *metadata;
      88           0 :     AVIOContext *pb = format_context->pb;
      89           0 :     FILMOutputContext *film = format_context->priv_data;
      90           0 :     int encoded_buf_size = 0;
      91             :     enum AVCodecID codec_id;
      92             : 
      93             :     /* Track the metadata used to write the header and add it to the linked list */
      94           0 :     metadata = av_mallocz(sizeof(FILMPacket));
      95           0 :     if (!metadata)
      96           0 :         return AVERROR(ENOMEM);
      97           0 :     metadata->audio = pkt->stream_index == film->audio_index;
      98           0 :     metadata->keyframe = pkt->flags & AV_PKT_FLAG_KEY;
      99           0 :     metadata->pts = pkt->pts;
     100           0 :     metadata->duration = pkt->duration;
     101           0 :     metadata->size = pkt->size;
     102           0 :     if (film->last == NULL) {
     103           0 :         metadata->index = 0;
     104             :     } else {
     105           0 :         metadata->index = film->last->index + film->last->size;
     106           0 :         film->last->next = metadata;
     107             :     }
     108           0 :     metadata->next = NULL;
     109           0 :     if (film->start == NULL)
     110           0 :         film->start = metadata;
     111           0 :     film->packet_count++;
     112           0 :     film->last = metadata;
     113             : 
     114           0 :     codec_id = format_context->streams[pkt->stream_index]->codecpar->codec_id;
     115             : 
     116             :     /* Sega Cinepak has an extra two-byte header; write dummy data there,
     117             :      * then adjust the cvid header to accommodate for the extra size */
     118           0 :     if (codec_id == AV_CODEC_ID_CINEPAK) {
     119           0 :         encoded_buf_size = AV_RB24(&pkt->data[1]);
     120             :         /* Already Sega Cinepak, so no need to reformat the packets */
     121           0 :         if (encoded_buf_size != pkt->size && (pkt->size % encoded_buf_size) != 0) {
     122           0 :             avio_write(pb, pkt->data, pkt->size);
     123             :         } else {
     124           0 :             uint8_t padding[2] = {0, 0};
     125             :             /* In Sega Cinepak, the reported size in the Cinepak header is
     126             :              * 8 bytes too short. However, the size in the STAB section of the header
     127             :              * is correct, taking into account the extra two bytes. */
     128           0 :             AV_WB24(&pkt->data[1], pkt->size - 8 + 2);
     129           0 :             metadata->size += 2;
     130             : 
     131           0 :             avio_write(pb, pkt->data, 10);
     132           0 :             avio_write(pb, padding, 2);
     133           0 :             avio_write(pb, &pkt->data[10], pkt->size - 10);
     134             :         }
     135             :     } else {
     136             :         /* Other formats can just be written as-is */
     137           0 :         avio_write(pb, pkt->data, pkt->size);
     138             :     }
     139             : 
     140           0 :     return 0;
     141             : }
     142             : 
     143           0 : static int get_audio_codec_id(enum AVCodecID codec_id)
     144             : {
     145             :     /* 0 (PCM) and 2 (ADX) are the only known values */
     146           0 :     switch (codec_id) {
     147           0 :     case AV_CODEC_ID_PCM_S8_PLANAR:
     148             :     case AV_CODEC_ID_PCM_S16BE_PLANAR:
     149           0 :         return 0;
     150             :         break;
     151           0 :     case AV_CODEC_ID_ADPCM_ADX:
     152           0 :         return 2;
     153             :         break;
     154           0 :     default:
     155           0 :         return -1;
     156             :     }
     157             : }
     158             : 
     159           0 : static int film_init(AVFormatContext *format_context)
     160             : {
     161           0 :     AVStream *audio = NULL;
     162           0 :     FILMOutputContext *film = format_context->priv_data;
     163           0 :     film->audio_index = -1;
     164           0 :     film->video_index = -1;
     165           0 :     film->stab_pos = 0;
     166           0 :     film->packet_count = 0;
     167           0 :     film->start = NULL;
     168           0 :     film->last = NULL;
     169             : 
     170           0 :     for (int i = 0; i < format_context->nb_streams; i++) {
     171           0 :         AVStream *st = format_context->streams[i];
     172           0 :         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
     173           0 :             if (film->audio_index > -1) {
     174           0 :                 av_log(format_context, AV_LOG_ERROR, "Sega FILM allows a maximum of one audio stream.\n");
     175           0 :                 return AVERROR(EINVAL);
     176             :             }
     177           0 :             film->audio_index = i;
     178           0 :             audio = st;
     179             :         }
     180             : 
     181           0 :         if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
     182           0 :             if (film->video_index > -1) {
     183           0 :                 av_log(format_context, AV_LOG_ERROR, "Sega FILM allows a maximum of one video stream.\n");
     184           0 :                 return AVERROR(EINVAL);
     185             :             }
     186           0 :             film->video_index = i;
     187             :         }
     188             : 
     189           0 :         if (film->video_index == -1) {
     190           0 :             av_log(format_context, AV_LOG_ERROR, "No video stream present.\n");
     191           0 :             return AVERROR(EINVAL);
     192             :         }
     193             :     }
     194             : 
     195           0 :     if (audio != NULL && get_audio_codec_id(audio->codecpar->codec_id) < 0) {
     196           0 :         av_log(format_context, AV_LOG_ERROR, "Incompatible audio stream format.\n");
     197           0 :         return AVERROR(EINVAL);
     198             :     }
     199             : 
     200           0 :     return 0;
     201             : }
     202             : 
     203           0 : static int shift_data(AVFormatContext *format_context, int64_t shift_size)
     204             : {
     205           0 :     int ret = 0;
     206           0 :     int64_t pos, pos_end = avio_tell(format_context->pb);
     207             :     uint8_t *buf, *read_buf[2];
     208           0 :     int read_buf_id = 0;
     209             :     int read_size[2];
     210             :     AVIOContext *read_pb;
     211             : 
     212           0 :     buf = av_malloc(shift_size * 2);
     213           0 :     if (!buf)
     214           0 :         return AVERROR(ENOMEM);
     215           0 :     read_buf[0] = buf;
     216           0 :     read_buf[1] = buf + shift_size;
     217             : 
     218             :     /* Write the header at the beginning of the file, shifting all content as necessary;
     219             :      * based on the approach used by MOV faststart. */
     220           0 :     avio_flush(format_context->pb);
     221           0 :     ret = format_context->io_open(format_context, &read_pb, format_context->url, AVIO_FLAG_READ, NULL);
     222           0 :     if (ret < 0) {
     223           0 :         av_log(format_context, AV_LOG_ERROR, "Unable to re-open %s output file to "
     224             :                "write the header\n", format_context->url);
     225           0 :         av_free(buf);
     226           0 :         return ret;
     227             :     }
     228             : 
     229             :     /* mark the end of the shift to up to the last data we wrote, and get ready
     230             :      * for writing */
     231           0 :     pos_end = avio_tell(format_context->pb);
     232           0 :     avio_seek(format_context->pb, shift_size, SEEK_SET);
     233             : 
     234             :     /* start reading at where the new header will be placed */
     235           0 :     avio_seek(read_pb, 0, SEEK_SET);
     236           0 :     pos = avio_tell(read_pb);
     237             : 
     238             : #define READ_BLOCK do {                                                             \
     239             :     read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], shift_size);  \
     240             :     read_buf_id ^= 1;                                                               \
     241             : } while (0)
     242             : 
     243             :     /* shift data by chunk of at most shift_size */
     244           0 :     READ_BLOCK;
     245             :     do {
     246             :         int n;
     247           0 :         READ_BLOCK;
     248           0 :         n = read_size[read_buf_id];
     249           0 :         if (n <= 0)
     250           0 :             break;
     251           0 :         avio_write(format_context->pb, read_buf[read_buf_id], n);
     252           0 :         pos += n;
     253           0 :     } while (pos < pos_end);
     254           0 :     ff_format_io_close(format_context, &read_pb);
     255             : 
     256           0 :     av_free(buf);
     257           0 :     return 0;
     258             : }
     259             : 
     260           0 : static int film_write_header(AVFormatContext *format_context)
     261             : {
     262           0 :     int ret = 0;
     263             :     int64_t sample_table_size, stabsize, headersize;
     264             :     int8_t audio_codec;
     265           0 :     AVIOContext *pb = format_context->pb;
     266           0 :     FILMOutputContext *film = format_context->priv_data;
     267             :     FILMPacket *prev, *packet;
     268           0 :     AVStream *audio = NULL;
     269           0 :     AVStream *video = NULL;
     270             : 
     271             :     /* Calculate how much we need to reserve for the header;
     272             :      * this is the amount the rest of the data will be shifted up by. */
     273           0 :     sample_table_size = film->packet_count * 16;
     274           0 :     stabsize = 16 + sample_table_size;
     275           0 :     headersize = 16 + /* FILM header base */
     276             :                  32 + /* FDSC chunk */
     277             :                  stabsize;
     278             : 
     279           0 :     ret = shift_data(format_context, headersize);
     280           0 :     if (ret < 0)
     281           0 :         return ret;
     282             :     /* Seek back to the beginning to start writing the header now */
     283           0 :     avio_seek(pb, 0, SEEK_SET);
     284             : 
     285           0 :     if (film->audio_index > -1)
     286           0 :         audio = format_context->streams[film->audio_index];
     287           0 :     if (film->video_index > -1)
     288           0 :         video = format_context->streams[film->video_index];
     289             : 
     290           0 :     if (audio != NULL) {
     291           0 :         audio_codec = get_audio_codec_id(audio->codecpar->codec_id);
     292           0 :         if (audio_codec < 0) {
     293           0 :             av_log(format_context, AV_LOG_ERROR, "Incompatible audio stream format.\n");
     294           0 :             return AVERROR(EINVAL);
     295             :         }
     296             :     }
     297             : 
     298           0 :     if (video->codecpar->format != AV_PIX_FMT_RGB24) {
     299           0 :         av_log(format_context, AV_LOG_ERROR, "Pixel format must be rgb24.\n");
     300           0 :         return AVERROR(EINVAL);
     301             :     }
     302             : 
     303             :     /* First, write the FILM header; this is very simple */
     304             : 
     305           0 :     ffio_wfourcc(pb, "FILM");
     306           0 :     avio_wb32(pb, 48 + stabsize);
     307             :     /* This seems to be okay to hardcode, since this muxer targets 1.09 features;
     308             :      * videos produced by this muxer are readable by 1.08 and lower players. */
     309           0 :     ffio_wfourcc(pb, "1.09");
     310             :     /* I have no idea what this field does, might be reserved */
     311           0 :     avio_wb32(pb, 0);
     312             : 
     313             :     /* Next write the FDSC (file description) chunk */
     314           0 :     ffio_wfourcc(pb, "FDSC");
     315           0 :     avio_wb32(pb, 0x20); /* Size of FDSC chunk */
     316             : 
     317             :     /* The only two supported codecs; raw video is rare */
     318           0 :     switch (video->codecpar->codec_id) {
     319           0 :     case AV_CODEC_ID_CINEPAK:
     320           0 :         ffio_wfourcc(pb, "cvid");
     321           0 :         break;
     322           0 :     case AV_CODEC_ID_RAWVIDEO:
     323           0 :         ffio_wfourcc(pb, "raw ");
     324           0 :         break;
     325           0 :     default:
     326           0 :         av_log(format_context, AV_LOG_ERROR, "Incompatible video stream format.\n");
     327           0 :         return AVERROR(EINVAL);
     328             :     }
     329             : 
     330           0 :     avio_wb32(pb, video->codecpar->height);
     331           0 :     avio_wb32(pb, video->codecpar->width);
     332           0 :     avio_w8(pb, 24); /* Bits per pixel - observed to always be 24 */
     333             : 
     334           0 :     if (audio != NULL) {
     335           0 :         avio_w8(pb, audio->codecpar->channels); /* Audio channels */
     336           0 :         avio_w8(pb, audio->codecpar->bits_per_coded_sample); /* Audio bit depth */
     337           0 :         avio_w8(pb, audio_codec); /* Compression - 0 is PCM, 2 is ADX */
     338           0 :         avio_wb16(pb, audio->codecpar->sample_rate); /* Audio sampling rate */
     339             :     } else {
     340             :         /* Set all these fields to 0 if there's no audio */
     341           0 :         avio_w8(pb, 0);
     342           0 :         avio_w8(pb, 0);
     343           0 :         avio_w8(pb, 0);
     344           0 :         avio_wb16(pb, 0);
     345             :     }
     346             : 
     347             :     /* I have no idea what this pair of fields does either, might be reserved */
     348           0 :     avio_wb32(pb, 0);
     349           0 :     avio_wb16(pb, 0);
     350             : 
     351             :     /* Finally, write the STAB (sample table) chunk */
     352           0 :     ffio_wfourcc(pb, "STAB");
     353           0 :     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           0 :     avio_wb32(pb, av_q2d(av_inv_q(video->time_base)));
     364             : 
     365           0 :     avio_wb32(pb, film->packet_count);
     366             : 
     367           0 :     avio_flush(pb);
     368             : 
     369             :     /* Finally, write out each packet's data to the header */
     370           0 :     packet = film->start;
     371           0 :     while (packet != NULL) {
     372           0 :         film_write_packet_to_header(format_context, packet);
     373           0 :         prev = packet;
     374           0 :         packet = packet->next;
     375           0 :         av_freep(&prev);
     376             :     }
     377             : 
     378           0 :     return 0;
     379             : }
     380             : 
     381             : static const AVClass film_muxer_class = {
     382             :     .class_name     = "Sega FILM muxer",
     383             :     .item_name      = av_default_item_name,
     384             :     .version        = LIBAVUTIL_VERSION_INT,
     385             : };
     386             : 
     387             : AVOutputFormat ff_segafilm_muxer = {
     388             :     .name           = "film_cpk",
     389             :     .long_name      = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
     390             :     .extensions     = "cpk",
     391             :     .priv_data_size = sizeof(FILMOutputContext),
     392             :     .audio_codec    = AV_CODEC_ID_PCM_S16BE_PLANAR,
     393             :     .video_codec    = AV_CODEC_ID_CINEPAK,
     394             :     .init           = film_init,
     395             :     .write_trailer  = film_write_header,
     396             :     .write_packet   = film_write_packet,
     397             :     .priv_class     = &film_muxer_class,
     398             : };

Generated by: LCOV version 1.13