Libav 0.7.1
|
00001 /* 00002 * SSA/ASS common funtions 00003 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org> 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 "avcodec.h" 00023 #include "ass.h" 00024 #include "libavutil/avstring.h" 00025 00041 static int ff_ass_subtitle_header(AVCodecContext *avctx, 00042 const char *font, int font_size, 00043 int color, int back_color, 00044 int bold, int italic, int underline, 00045 int alignment) 00046 { 00047 char header[512]; 00048 00049 snprintf(header, sizeof(header), 00050 "[Script Info]\r\n" 00051 "ScriptType: v4.00+\r\n" 00052 "\r\n" 00053 "[V4+ Styles]\r\n" 00054 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n" 00055 "Style: Default,%s,%d,&H%x,&H%x,&H%x,&H%x,%d,%d,%d,1,1,0,%d,10,10,10,0,0\r\n" 00056 "\r\n" 00057 "[Events]\r\n" 00058 "Format: Layer, Start, End, Text\r\n", 00059 font, font_size, color, color, back_color, back_color, 00060 -bold, -italic, -underline, alignment); 00061 00062 avctx->subtitle_header = av_strdup(header); 00063 if (!avctx->subtitle_header) 00064 return AVERROR(ENOMEM); 00065 avctx->subtitle_header_size = strlen(avctx->subtitle_header); 00066 return 0; 00067 } 00068 00069 int ff_ass_subtitle_header_default(AVCodecContext *avctx) 00070 { 00071 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT, 00072 ASS_DEFAULT_FONT_SIZE, 00073 ASS_DEFAULT_COLOR, 00074 ASS_DEFAULT_BACK_COLOR, 00075 ASS_DEFAULT_BOLD, 00076 ASS_DEFAULT_ITALIC, 00077 ASS_DEFAULT_UNDERLINE, 00078 ASS_DEFAULT_ALIGNMENT); 00079 } 00080 00081 void ff_ass_init(AVSubtitle *sub) 00082 { 00083 memset(sub, 0, sizeof(*sub)); 00084 } 00085 00086 static int ts_to_string(char *str, int strlen, int ts) 00087 { 00088 int h, m, s; 00089 h = ts/360000; ts -= 360000*h; 00090 m = ts/ 6000; ts -= 6000*m; 00091 s = ts/ 100; ts -= 100*s; 00092 return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts); 00093 } 00094 00095 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, 00096 int ts_start, int ts_end, int raw) 00097 { 00098 int len = 0, dlen, duration = ts_end - ts_start; 00099 char s_start[16], s_end[16], header[48] = {0}; 00100 AVSubtitleRect **rects; 00101 00102 if (!raw) { 00103 ts_to_string(s_start, sizeof(s_start), ts_start); 00104 ts_to_string(s_end, sizeof(s_end), ts_end ); 00105 len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,", 00106 s_start, s_end); 00107 } 00108 00109 dlen = strcspn(dialog, "\n"); 00110 dlen += dialog[dlen] == '\n'; 00111 00112 rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects)); 00113 if (!rects) 00114 return AVERROR(ENOMEM); 00115 sub->rects = rects; 00116 sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration); 00117 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0])); 00118 rects[sub->num_rects]->type = SUBTITLE_ASS; 00119 rects[sub->num_rects]->ass = av_malloc(len + dlen + 1); 00120 strcpy (rects[sub->num_rects]->ass , header); 00121 av_strlcpy(rects[sub->num_rects]->ass + len, dialog, dlen + 1); 00122 sub->num_rects++; 00123 return dlen; 00124 }