annotate ffmpeg/libavcodec/samidec.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 * Copyright (c) 2012 Clément Bœsch
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 /**
yading@10 22 * @file
yading@10 23 * SAMI subtitle decoder
yading@10 24 * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx
yading@10 25 */
yading@10 26
yading@10 27 #include "ass.h"
yading@10 28 #include "libavutil/avstring.h"
yading@10 29 #include "libavutil/bprint.h"
yading@10 30
yading@10 31 typedef struct {
yading@10 32 AVBPrint source;
yading@10 33 AVBPrint content;
yading@10 34 AVBPrint full;
yading@10 35 } SAMIContext;
yading@10 36
yading@10 37 static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
yading@10 38 {
yading@10 39 SAMIContext *sami = avctx->priv_data;
yading@10 40 int ret = 0;
yading@10 41 char *tag = NULL;
yading@10 42 char *dupsrc = av_strdup(src);
yading@10 43 char *p = dupsrc;
yading@10 44
yading@10 45 av_bprint_clear(&sami->content);
yading@10 46 for (;;) {
yading@10 47 char *saveptr = NULL;
yading@10 48 int prev_chr_is_space = 0;
yading@10 49 AVBPrint *dst = &sami->content;
yading@10 50
yading@10 51 /* parse & extract paragraph tag */
yading@10 52 p = av_stristr(p, "<P");
yading@10 53 if (!p)
yading@10 54 break;
yading@10 55 if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
yading@10 56 p++;
yading@10 57 continue;
yading@10 58 }
yading@10 59 if (dst->len) // add a separator with the previous paragraph if there was one
yading@10 60 av_bprintf(dst, "\\N");
yading@10 61 tag = av_strtok(p, ">", &saveptr);
yading@10 62 if (!tag || !saveptr)
yading@10 63 break;
yading@10 64 p = saveptr;
yading@10 65
yading@10 66 /* check if the current paragraph is the "source" (speaker name) */
yading@10 67 if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
yading@10 68 dst = &sami->source;
yading@10 69 av_bprint_clear(dst);
yading@10 70 }
yading@10 71
yading@10 72 /* if empty event -> skip subtitle */
yading@10 73 while (av_isspace(*p))
yading@10 74 p++;
yading@10 75 if (!strncmp(p, "&nbsp;", 6)) {
yading@10 76 ret = -1;
yading@10 77 goto end;
yading@10 78 }
yading@10 79
yading@10 80 /* extract the text, stripping most of the tags */
yading@10 81 while (*p) {
yading@10 82 if (*p == '<') {
yading@10 83 if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
yading@10 84 break;
yading@10 85 if (!av_strncasecmp(p, "<BR", 3))
yading@10 86 av_bprintf(dst, "\\N");
yading@10 87 p++;
yading@10 88 while (*p && *p != '>')
yading@10 89 p++;
yading@10 90 if (!*p)
yading@10 91 break;
yading@10 92 if (*p == '>')
yading@10 93 p++;
yading@10 94 }
yading@10 95 if (!av_isspace(*p))
yading@10 96 av_bprint_chars(dst, *p, 1);
yading@10 97 else if (!prev_chr_is_space)
yading@10 98 av_bprint_chars(dst, ' ', 1);
yading@10 99 prev_chr_is_space = av_isspace(*p);
yading@10 100 p++;
yading@10 101 }
yading@10 102 }
yading@10 103
yading@10 104 av_bprint_clear(&sami->full);
yading@10 105 if (sami->source.len)
yading@10 106 av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->source.str);
yading@10 107 av_bprintf(&sami->full, "%s\r\n", sami->content.str);
yading@10 108
yading@10 109 end:
yading@10 110 av_free(dupsrc);
yading@10 111 return ret;
yading@10 112 }
yading@10 113
yading@10 114 static int sami_decode_frame(AVCodecContext *avctx,
yading@10 115 void *data, int *got_sub_ptr, AVPacket *avpkt)
yading@10 116 {
yading@10 117 AVSubtitle *sub = data;
yading@10 118 const char *ptr = avpkt->data;
yading@10 119 SAMIContext *sami = avctx->priv_data;
yading@10 120
yading@10 121 if (ptr && avpkt->size > 0 && !sami_paragraph_to_ass(avctx, ptr)) {
yading@10 122 int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
yading@10 123 int ts_duration = avpkt->duration != -1 ?
yading@10 124 av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
yading@10 125 ff_ass_add_rect(sub, sami->full.str, ts_start, ts_duration, 0);
yading@10 126 }
yading@10 127 *got_sub_ptr = sub->num_rects > 0;
yading@10 128 return avpkt->size;
yading@10 129 }
yading@10 130
yading@10 131 static av_cold int sami_init(AVCodecContext *avctx)
yading@10 132 {
yading@10 133 SAMIContext *sami = avctx->priv_data;
yading@10 134 av_bprint_init(&sami->source, 0, 2048);
yading@10 135 av_bprint_init(&sami->content, 0, 2048);
yading@10 136 av_bprint_init(&sami->full, 0, 2048);
yading@10 137 return ff_ass_subtitle_header_default(avctx);
yading@10 138 }
yading@10 139
yading@10 140 static av_cold int sami_close(AVCodecContext *avctx)
yading@10 141 {
yading@10 142 SAMIContext *sami = avctx->priv_data;
yading@10 143 av_bprint_finalize(&sami->source, NULL);
yading@10 144 av_bprint_finalize(&sami->content, NULL);
yading@10 145 av_bprint_finalize(&sami->full, NULL);
yading@10 146 return 0;
yading@10 147 }
yading@10 148
yading@10 149 AVCodec ff_sami_decoder = {
yading@10 150 .name = "sami",
yading@10 151 .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
yading@10 152 .type = AVMEDIA_TYPE_SUBTITLE,
yading@10 153 .id = AV_CODEC_ID_SAMI,
yading@10 154 .priv_data_size = sizeof(SAMIContext),
yading@10 155 .init = sami_init,
yading@10 156 .close = sami_close,
yading@10 157 .decode = sami_decode_frame,
yading@10 158 };