• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/ass.c

Go to the documentation of this file.
00001 /*
00002  * SSA/ASS common funtions
00003  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 
00026 int ff_ass_subtitle_header(AVCodecContext *avctx,
00027                            const char *font, int font_size,
00028                            int color, int back_color,
00029                            int bold, int italic, int underline,
00030                            int alignment)
00031 {
00032     char header[512];
00033 
00034     snprintf(header, sizeof(header),
00035              "[Script Info]\r\n"
00036              "ScriptType: v4.00+\r\n"
00037              "\r\n"
00038              "[V4+ Styles]\r\n"
00039              "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
00040              "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"
00041              "\r\n"
00042              "[Events]\r\n"
00043              "Format: Layer, Start, End, Text\r\n",
00044              font, font_size, color, color, back_color, back_color,
00045              -bold, -italic, -underline, alignment);
00046 
00047     avctx->subtitle_header = av_strdup(header);
00048     if (!avctx->subtitle_header)
00049         return AVERROR(ENOMEM);
00050     avctx->subtitle_header_size = strlen(avctx->subtitle_header);
00051     return 0;
00052 }
00053 
00054 int ff_ass_subtitle_header_default(AVCodecContext *avctx)
00055 {
00056     return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
00057                                          ASS_DEFAULT_FONT_SIZE,
00058                                          ASS_DEFAULT_COLOR,
00059                                          ASS_DEFAULT_BACK_COLOR,
00060                                          ASS_DEFAULT_BOLD,
00061                                          ASS_DEFAULT_ITALIC,
00062                                          ASS_DEFAULT_UNDERLINE,
00063                                          ASS_DEFAULT_ALIGNMENT);
00064 }
00065 
00066 static int ts_to_string(char *str, int strlen, int ts)
00067 {
00068     int h, m, s;
00069     h = ts/360000;  ts -= 360000*h;
00070     m = ts/  6000;  ts -=   6000*m;
00071     s = ts/   100;  ts -=    100*s;
00072     return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
00073 }
00074 
00075 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
00076                     int ts_start, int ts_end, int raw)
00077 {
00078     int len = 0, dlen, duration = ts_end - ts_start;
00079     char s_start[16], s_end[16], header[48] = {0};
00080     AVSubtitleRect **rects;
00081 
00082     if (!raw) {
00083         ts_to_string(s_start, sizeof(s_start), ts_start);
00084         ts_to_string(s_end,   sizeof(s_end),   ts_end  );
00085         len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,",
00086                        s_start, s_end);
00087     }
00088 
00089     dlen = strcspn(dialog, "\n");
00090     dlen += dialog[dlen] == '\n';
00091 
00092     rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
00093     if (!rects)
00094         return AVERROR(ENOMEM);
00095     sub->rects = rects;
00096     sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
00097     rects[sub->num_rects]       = av_mallocz(sizeof(*rects[0]));
00098     rects[sub->num_rects]->type = SUBTITLE_ASS;
00099     rects[sub->num_rects]->ass  = av_malloc(len + dlen + 1);
00100     strcpy (rects[sub->num_rects]->ass      , header);
00101     av_strlcpy(rects[sub->num_rects]->ass + len, dialog, dlen + 1);
00102     sub->num_rects++;
00103     return dlen;
00104 }

Generated on Wed Apr 11 2012 07:31:32 for FFmpeg by  doxygen 1.7.1