Libav 0.7.1
libavformat/assenc.c
Go to the documentation of this file.
00001 /*
00002  * SSA/ASS muxer
00003  * Copyright (c) 2008 Michael Niedermayer
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 
00024 typedef struct ASSContext{
00025     unsigned int extra_index;
00026 }ASSContext;
00027 
00028 static int write_header(AVFormatContext *s)
00029 {
00030     ASSContext *ass = s->priv_data;
00031     AVCodecContext *avctx= s->streams[0]->codec;
00032     uint8_t *last= NULL;
00033 
00034     if(s->nb_streams != 1 || avctx->codec_id != CODEC_ID_SSA){
00035         av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
00036         return -1;
00037     }
00038 
00039     while(ass->extra_index < avctx->extradata_size){
00040         uint8_t *p  = avctx->extradata + ass->extra_index;
00041         uint8_t *end= strchr(p, '\n');
00042         if(!end) end= avctx->extradata + avctx->extradata_size;
00043         else     end++;
00044 
00045         avio_write(s->pb, p, end-p);
00046         ass->extra_index += end-p;
00047 
00048         if(last && !memcmp(last, "[Events]", 8))
00049             break;
00050         last=p;
00051     }
00052 
00053     avio_flush(s->pb);
00054 
00055     return 0;
00056 }
00057 
00058 static int write_packet(AVFormatContext *s, AVPacket *pkt)
00059 {
00060     avio_write(s->pb, pkt->data, pkt->size);
00061 
00062     avio_flush(s->pb);
00063 
00064     return 0;
00065 }
00066 
00067 static int write_trailer(AVFormatContext *s)
00068 {
00069     ASSContext *ass = s->priv_data;
00070     AVCodecContext *avctx= s->streams[0]->codec;
00071 
00072     avio_write(s->pb, avctx->extradata      + ass->extra_index,
00073                       avctx->extradata_size - ass->extra_index);
00074 
00075     avio_flush(s->pb);
00076 
00077     return 0;
00078 }
00079 
00080 AVOutputFormat ff_ass_muxer = {
00081     .name           = "ass",
00082     .long_name      = NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle format"),
00083     .mime_type      = "text/x-ssa",
00084     .extensions     = "ass,ssa",
00085     .priv_data_size = sizeof(ASSContext),
00086     .subtitle_codec = CODEC_ID_SSA,
00087     .write_header   = write_header,
00088     .write_packet   = write_packet,
00089     .write_trailer  = write_trailer,
00090     .flags          = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS,
00091 };