annotate ffmpeg/libavformat/subviewer1dec.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 f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Copyright (c) 2012 Clément Bœsch
yading@11 3 *
yading@11 4 * This file is part of FFmpeg.
yading@11 5 *
yading@11 6 * FFmpeg is free software; you can redistribute it and/or
yading@11 7 * modify it under the terms of the GNU Lesser General Public
yading@11 8 * License as published by the Free Software Foundation; either
yading@11 9 * version 2.1 of the License, or (at your option) any later version.
yading@11 10 *
yading@11 11 * FFmpeg is distributed in the hope that it will be useful,
yading@11 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 14 * Lesser General Public License for more details.
yading@11 15 *
yading@11 16 * You should have received a copy of the GNU Lesser General Public
yading@11 17 * License along with FFmpeg; if not, write to the Free Software
yading@11 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 19 */
yading@11 20
yading@11 21 /**
yading@11 22 * @file
yading@11 23 * SubViewer v1 subtitle demuxer
yading@11 24 */
yading@11 25
yading@11 26 #include "avformat.h"
yading@11 27 #include "internal.h"
yading@11 28 #include "subtitles.h"
yading@11 29
yading@11 30 typedef struct {
yading@11 31 FFDemuxSubtitlesQueue q;
yading@11 32 } SubViewer1Context;
yading@11 33
yading@11 34 static int subviewer1_probe(AVProbeData *p)
yading@11 35 {
yading@11 36 const unsigned char *ptr = p->buf;
yading@11 37
yading@11 38 if (strstr(ptr, "******** START SCRIPT ********"))
yading@11 39 return AVPROBE_SCORE_MAX / 2;
yading@11 40 return 0;
yading@11 41 }
yading@11 42
yading@11 43 static int subviewer1_read_header(AVFormatContext *s)
yading@11 44 {
yading@11 45 int delay = 0;
yading@11 46 AVPacket *sub = NULL;
yading@11 47 SubViewer1Context *subviewer1 = s->priv_data;
yading@11 48 AVStream *st = avformat_new_stream(s, NULL);
yading@11 49
yading@11 50 if (!st)
yading@11 51 return AVERROR(ENOMEM);
yading@11 52 avpriv_set_pts_info(st, 64, 1, 1);
yading@11 53 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
yading@11 54 st->codec->codec_id = AV_CODEC_ID_SUBVIEWER1;
yading@11 55
yading@11 56 while (!url_feof(s->pb)) {
yading@11 57 char line[4096];
yading@11 58 int len = ff_get_line(s->pb, line, sizeof(line));
yading@11 59 int hh, mm, ss;
yading@11 60
yading@11 61 if (!len)
yading@11 62 break;
yading@11 63
yading@11 64 if (!strncmp(line, "[DELAY]", 7)) {
yading@11 65 ff_get_line(s->pb, line, sizeof(line));
yading@11 66 sscanf(line, "%d", &delay);
yading@11 67 }
yading@11 68
yading@11 69 if (sscanf(line, "[%d:%d:%d]", &hh, &mm, &ss) == 3) {
yading@11 70 const int64_t pos = avio_tell(s->pb);
yading@11 71 int64_t pts_start = hh*3600LL + mm*60LL + ss + delay;
yading@11 72
yading@11 73 len = ff_get_line(s->pb, line, sizeof(line));
yading@11 74 line[strcspn(line, "\r\n")] = 0;
yading@11 75 if (!*line) {
yading@11 76 if (sub)
yading@11 77 sub->duration = pts_start - sub->pts;
yading@11 78 } else {
yading@11 79 sub = ff_subtitles_queue_insert(&subviewer1->q, line, len, 0);
yading@11 80 if (!sub)
yading@11 81 return AVERROR(ENOMEM);
yading@11 82 sub->pos = pos;
yading@11 83 sub->pts = pts_start;
yading@11 84 sub->duration = -1;
yading@11 85 }
yading@11 86 }
yading@11 87 }
yading@11 88
yading@11 89 ff_subtitles_queue_finalize(&subviewer1->q);
yading@11 90 return 0;
yading@11 91 }
yading@11 92
yading@11 93 static int subviewer1_read_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 94 {
yading@11 95 SubViewer1Context *subviewer1 = s->priv_data;
yading@11 96 return ff_subtitles_queue_read_packet(&subviewer1->q, pkt);
yading@11 97 }
yading@11 98
yading@11 99 static int subviewer1_read_seek(AVFormatContext *s, int stream_index,
yading@11 100 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
yading@11 101 {
yading@11 102 SubViewer1Context *subviewer1 = s->priv_data;
yading@11 103 return ff_subtitles_queue_seek(&subviewer1->q, s, stream_index,
yading@11 104 min_ts, ts, max_ts, flags);
yading@11 105 }
yading@11 106
yading@11 107 static int subviewer1_read_close(AVFormatContext *s)
yading@11 108 {
yading@11 109 SubViewer1Context *subviewer1 = s->priv_data;
yading@11 110 ff_subtitles_queue_clean(&subviewer1->q);
yading@11 111 return 0;
yading@11 112 }
yading@11 113
yading@11 114 AVInputFormat ff_subviewer1_demuxer = {
yading@11 115 .name = "subviewer1",
yading@11 116 .long_name = NULL_IF_CONFIG_SMALL("SubViewer v1 subtitle format"),
yading@11 117 .priv_data_size = sizeof(SubViewer1Context),
yading@11 118 .read_probe = subviewer1_probe,
yading@11 119 .read_header = subviewer1_read_header,
yading@11 120 .read_packet = subviewer1_read_packet,
yading@11 121 .read_seek2 = subviewer1_read_seek,
yading@11 122 .read_close = subviewer1_read_close,
yading@11 123 .extensions = "sub",
yading@11 124 };