annotate ffmpeg/libavformat/mpl2dec.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 * MPL2 subtitles format 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 } MPL2Context;
yading@11 33
yading@11 34 static int mpl2_probe(AVProbeData *p)
yading@11 35 {
yading@11 36 int i;
yading@11 37 char c;
yading@11 38 int64_t start, end;
yading@11 39 const unsigned char *ptr = p->buf;
yading@11 40 const unsigned char *ptr_end = ptr + p->buf_size;
yading@11 41
yading@11 42 for (i = 0; i < 2; i++) {
yading@11 43 if (sscanf(ptr, "[%"SCNd64"][%"SCNd64"]%c", &start, &end, &c) != 3 &&
yading@11 44 sscanf(ptr, "[%"SCNd64"][]%c", &start, &c) != 2)
yading@11 45 return 0;
yading@11 46 ptr += strcspn(ptr, "\r\n") + 1;
yading@11 47 if (ptr >= ptr_end)
yading@11 48 return 0;
yading@11 49 }
yading@11 50 return AVPROBE_SCORE_MAX;
yading@11 51 }
yading@11 52
yading@11 53 static int read_ts(char **line, int64_t *pts_start, int *duration)
yading@11 54 {
yading@11 55 char c;
yading@11 56 int len;
yading@11 57 int64_t end;
yading@11 58
yading@11 59 if (sscanf(*line, "[%"SCNd64"][]%c%n",
yading@11 60 pts_start, &c, &len) >= 2) {
yading@11 61 *duration = -1;
yading@11 62 *line += len - 1;
yading@11 63 return 0;
yading@11 64 }
yading@11 65 if (sscanf(*line, "[%"SCNd64"][%"SCNd64"]%c%n",
yading@11 66 pts_start, &end, &c, &len) >= 3) {
yading@11 67 *duration = end - *pts_start;
yading@11 68 *line += len - 1;
yading@11 69 return 0;
yading@11 70 }
yading@11 71 return -1;
yading@11 72 }
yading@11 73
yading@11 74 static int mpl2_read_header(AVFormatContext *s)
yading@11 75 {
yading@11 76 MPL2Context *mpl2 = s->priv_data;
yading@11 77 AVStream *st = avformat_new_stream(s, NULL);
yading@11 78 int res = 0;
yading@11 79
yading@11 80 if (!st)
yading@11 81 return AVERROR(ENOMEM);
yading@11 82 avpriv_set_pts_info(st, 64, 1, 10);
yading@11 83 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
yading@11 84 st->codec->codec_id = AV_CODEC_ID_MPL2;
yading@11 85
yading@11 86 while (!url_feof(s->pb)) {
yading@11 87 char line[4096];
yading@11 88 char *p = line;
yading@11 89 const int64_t pos = avio_tell(s->pb);
yading@11 90 int len = ff_get_line(s->pb, line, sizeof(line));
yading@11 91 int64_t pts_start;
yading@11 92 int duration;
yading@11 93
yading@11 94 if (!len)
yading@11 95 break;
yading@11 96
yading@11 97 line[strcspn(line, "\r\n")] = 0;
yading@11 98
yading@11 99 if (!read_ts(&p, &pts_start, &duration)) {
yading@11 100 AVPacket *sub;
yading@11 101
yading@11 102 sub = ff_subtitles_queue_insert(&mpl2->q, p, strlen(p), 0);
yading@11 103 if (!sub)
yading@11 104 return AVERROR(ENOMEM);
yading@11 105 sub->pos = pos;
yading@11 106 sub->pts = pts_start;
yading@11 107 sub->duration = duration;
yading@11 108 }
yading@11 109 }
yading@11 110
yading@11 111 ff_subtitles_queue_finalize(&mpl2->q);
yading@11 112 return res;
yading@11 113 }
yading@11 114
yading@11 115 static int mpl2_read_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 116 {
yading@11 117 MPL2Context *mpl2 = s->priv_data;
yading@11 118 return ff_subtitles_queue_read_packet(&mpl2->q, pkt);
yading@11 119 }
yading@11 120
yading@11 121 static int mpl2_read_seek(AVFormatContext *s, int stream_index,
yading@11 122 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
yading@11 123 {
yading@11 124 MPL2Context *mpl2 = s->priv_data;
yading@11 125 return ff_subtitles_queue_seek(&mpl2->q, s, stream_index,
yading@11 126 min_ts, ts, max_ts, flags);
yading@11 127 }
yading@11 128
yading@11 129 static int mpl2_read_close(AVFormatContext *s)
yading@11 130 {
yading@11 131 MPL2Context *mpl2 = s->priv_data;
yading@11 132 ff_subtitles_queue_clean(&mpl2->q);
yading@11 133 return 0;
yading@11 134 }
yading@11 135
yading@11 136 AVInputFormat ff_mpl2_demuxer = {
yading@11 137 .name = "mpl2",
yading@11 138 .long_name = NULL_IF_CONFIG_SMALL("MPL2 subtitles"),
yading@11 139 .priv_data_size = sizeof(MPL2Context),
yading@11 140 .read_probe = mpl2_probe,
yading@11 141 .read_header = mpl2_read_header,
yading@11 142 .read_packet = mpl2_read_packet,
yading@11 143 .read_seek2 = mpl2_read_seek,
yading@11 144 .read_close = mpl2_read_close,
yading@11 145 .extensions = "txt,mpl2",
yading@11 146 };