annotate ffmpeg/libavcodec/subviewerdec.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 * SubViewer subtitle decoder
yading@10 24 * @see https://en.wikipedia.org/wiki/SubViewer
yading@10 25 */
yading@10 26
yading@10 27 #include "avcodec.h"
yading@10 28 #include "ass.h"
yading@10 29 #include "libavutil/bprint.h"
yading@10 30
yading@10 31 static int subviewer_event_to_ass(AVBPrint *buf, const char *p)
yading@10 32 {
yading@10 33 while (*p) {
yading@10 34 if (!strncmp(p, "[br]", 4)) {
yading@10 35 av_bprintf(buf, "\\N");
yading@10 36 p += 4;
yading@10 37 } else {
yading@10 38 if (p[0] == '\n' && p[1])
yading@10 39 av_bprintf(buf, "\\N");
yading@10 40 else if (*p != '\n' && *p != '\r')
yading@10 41 av_bprint_chars(buf, *p, 1);
yading@10 42 p++;
yading@10 43 }
yading@10 44 }
yading@10 45
yading@10 46 av_bprintf(buf, "\r\n");
yading@10 47 return 0;
yading@10 48 }
yading@10 49
yading@10 50 static int subviewer_decode_frame(AVCodecContext *avctx,
yading@10 51 void *data, int *got_sub_ptr, AVPacket *avpkt)
yading@10 52 {
yading@10 53 char c;
yading@10 54 AVSubtitle *sub = data;
yading@10 55 const char *ptr = avpkt->data;
yading@10 56 AVBPrint buf;
yading@10 57
yading@10 58 /* To be removed later */
yading@10 59 if (ptr && sscanf(ptr, "%*u:%*u:%*u.%*u,%*u:%*u:%*u.%*u%c", &c) == 1) {
yading@10 60 av_log(avctx, AV_LOG_ERROR, "AVPacket is not clean (contains timing "
yading@10 61 "information). You need to upgrade your libavformat or "
yading@10 62 "sanitize your packet.\n");
yading@10 63 return AVERROR_INVALIDDATA;
yading@10 64 }
yading@10 65
yading@10 66 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
yading@10 67 // note: no need to rescale pts & duration since they are in the same
yading@10 68 // timebase as ASS (1/100)
yading@10 69 if (ptr && avpkt->size > 0 && !subviewer_event_to_ass(&buf, ptr))
yading@10 70 ff_ass_add_rect(sub, buf.str, avpkt->pts, avpkt->duration, 0);
yading@10 71 *got_sub_ptr = sub->num_rects > 0;
yading@10 72 av_bprint_finalize(&buf, NULL);
yading@10 73 return avpkt->size;
yading@10 74 }
yading@10 75
yading@10 76 AVCodec ff_subviewer_decoder = {
yading@10 77 .name = "subviewer",
yading@10 78 .long_name = NULL_IF_CONFIG_SMALL("SubViewer subtitle"),
yading@10 79 .type = AVMEDIA_TYPE_SUBTITLE,
yading@10 80 .id = AV_CODEC_ID_SUBVIEWER,
yading@10 81 .decode = subviewer_decode_frame,
yading@10 82 .init = ff_ass_subtitle_header_default,
yading@10 83 };