annotate ffmpeg/libavcodec/ass.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * SSA/ASS common functions
yading@10 3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include "avcodec.h"
yading@10 23 #include "ass.h"
yading@10 24 #include "libavutil/avassert.h"
yading@10 25 #include "libavutil/avstring.h"
yading@10 26 #include "libavutil/bprint.h"
yading@10 27 #include "libavutil/common.h"
yading@10 28
yading@10 29 int ff_ass_subtitle_header(AVCodecContext *avctx,
yading@10 30 const char *font, int font_size,
yading@10 31 int color, int back_color,
yading@10 32 int bold, int italic, int underline,
yading@10 33 int alignment)
yading@10 34 {
yading@10 35 avctx->subtitle_header = av_asprintf(
yading@10 36 "[Script Info]\r\n"
yading@10 37 "ScriptType: v4.00+\r\n"
yading@10 38 "\r\n"
yading@10 39 "[V4+ Styles]\r\n"
yading@10 40 "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
yading@10 41 "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"
yading@10 42 "\r\n"
yading@10 43 "[Events]\r\n"
yading@10 44 "Format: Layer, Start, End, Style, Text\r\n",
yading@10 45 font, font_size, color, color, back_color, back_color,
yading@10 46 -bold, -italic, -underline, alignment);
yading@10 47
yading@10 48 if (!avctx->subtitle_header)
yading@10 49 return AVERROR(ENOMEM);
yading@10 50 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
yading@10 51 return 0;
yading@10 52 }
yading@10 53
yading@10 54 int ff_ass_subtitle_header_default(AVCodecContext *avctx)
yading@10 55 {
yading@10 56 return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
yading@10 57 ASS_DEFAULT_FONT_SIZE,
yading@10 58 ASS_DEFAULT_COLOR,
yading@10 59 ASS_DEFAULT_BACK_COLOR,
yading@10 60 ASS_DEFAULT_BOLD,
yading@10 61 ASS_DEFAULT_ITALIC,
yading@10 62 ASS_DEFAULT_UNDERLINE,
yading@10 63 ASS_DEFAULT_ALIGNMENT);
yading@10 64 }
yading@10 65
yading@10 66 static void insert_ts(AVBPrint *buf, int ts)
yading@10 67 {
yading@10 68 if (ts == -1) {
yading@10 69 av_bprintf(buf, "9:59:59.99,");
yading@10 70 } else {
yading@10 71 int h, m, s;
yading@10 72
yading@10 73 h = ts/360000; ts -= 360000*h;
yading@10 74 m = ts/ 6000; ts -= 6000*m;
yading@10 75 s = ts/ 100; ts -= 100*s;
yading@10 76 av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
yading@10 77 }
yading@10 78 }
yading@10 79
yading@10 80 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
yading@10 81 int ts_start, int duration, int raw)
yading@10 82 {
yading@10 83 AVBPrint buf;
yading@10 84 int ret, dlen;
yading@10 85 AVSubtitleRect **rects;
yading@10 86
yading@10 87 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@10 88 if (!raw || raw == 2) {
yading@10 89 long int layer = 0;
yading@10 90
yading@10 91 if (raw == 2) {
yading@10 92 /* skip ReadOrder */
yading@10 93 dialog = strchr(dialog, ',');
yading@10 94 if (!dialog)
yading@10 95 return AVERROR_INVALIDDATA;
yading@10 96 dialog++;
yading@10 97
yading@10 98 /* extract Layer or Marked */
yading@10 99 layer = strtol(dialog, (char**)&dialog, 10);
yading@10 100 if (*dialog != ',')
yading@10 101 return AVERROR_INVALIDDATA;
yading@10 102 dialog++;
yading@10 103 }
yading@10 104 av_bprintf(&buf, "Dialogue: %ld,", layer);
yading@10 105 insert_ts(&buf, ts_start);
yading@10 106 insert_ts(&buf, duration == -1 ? -1 : ts_start + duration);
yading@10 107 if (raw != 2)
yading@10 108 av_bprintf(&buf, "Default,");
yading@10 109 }
yading@10 110
yading@10 111 dlen = strcspn(dialog, "\n");
yading@10 112 dlen += dialog[dlen] == '\n';
yading@10 113
yading@10 114 av_bprintf(&buf, "%.*s", dlen, dialog);
yading@10 115 if (raw == 2)
yading@10 116 av_bprintf(&buf, "\r\n");
yading@10 117 if (!av_bprint_is_complete(&buf))
yading@10 118 return AVERROR(ENOMEM);
yading@10 119
yading@10 120 rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
yading@10 121 if (!rects)
yading@10 122 return AVERROR(ENOMEM);
yading@10 123 sub->rects = rects;
yading@10 124 sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
yading@10 125 rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
yading@10 126 rects[sub->num_rects]->type = SUBTITLE_ASS;
yading@10 127 ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);
yading@10 128 if (ret < 0)
yading@10 129 return ret;
yading@10 130 sub->num_rects++;
yading@10 131 return dlen;
yading@10 132 }