yading@11: /* yading@11: * Copyright (c) 2012 Clément Bœsch yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * MPL2 subtitles format demuxer yading@11: */ yading@11: yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "subtitles.h" yading@11: yading@11: typedef struct { yading@11: FFDemuxSubtitlesQueue q; yading@11: } MPL2Context; yading@11: yading@11: static int mpl2_probe(AVProbeData *p) yading@11: { yading@11: int i; yading@11: char c; yading@11: int64_t start, end; yading@11: const unsigned char *ptr = p->buf; yading@11: const unsigned char *ptr_end = ptr + p->buf_size; yading@11: yading@11: for (i = 0; i < 2; i++) { yading@11: if (sscanf(ptr, "[%"SCNd64"][%"SCNd64"]%c", &start, &end, &c) != 3 && yading@11: sscanf(ptr, "[%"SCNd64"][]%c", &start, &c) != 2) yading@11: return 0; yading@11: ptr += strcspn(ptr, "\r\n") + 1; yading@11: if (ptr >= ptr_end) yading@11: return 0; yading@11: } yading@11: return AVPROBE_SCORE_MAX; yading@11: } yading@11: yading@11: static int read_ts(char **line, int64_t *pts_start, int *duration) yading@11: { yading@11: char c; yading@11: int len; yading@11: int64_t end; yading@11: yading@11: if (sscanf(*line, "[%"SCNd64"][]%c%n", yading@11: pts_start, &c, &len) >= 2) { yading@11: *duration = -1; yading@11: *line += len - 1; yading@11: return 0; yading@11: } yading@11: if (sscanf(*line, "[%"SCNd64"][%"SCNd64"]%c%n", yading@11: pts_start, &end, &c, &len) >= 3) { yading@11: *duration = end - *pts_start; yading@11: *line += len - 1; yading@11: return 0; yading@11: } yading@11: return -1; yading@11: } yading@11: yading@11: static int mpl2_read_header(AVFormatContext *s) yading@11: { yading@11: MPL2Context *mpl2 = s->priv_data; yading@11: AVStream *st = avformat_new_stream(s, NULL); yading@11: int res = 0; yading@11: yading@11: if (!st) yading@11: return AVERROR(ENOMEM); yading@11: avpriv_set_pts_info(st, 64, 1, 10); yading@11: st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; yading@11: st->codec->codec_id = AV_CODEC_ID_MPL2; yading@11: yading@11: while (!url_feof(s->pb)) { yading@11: char line[4096]; yading@11: char *p = line; yading@11: const int64_t pos = avio_tell(s->pb); yading@11: int len = ff_get_line(s->pb, line, sizeof(line)); yading@11: int64_t pts_start; yading@11: int duration; yading@11: yading@11: if (!len) yading@11: break; yading@11: yading@11: line[strcspn(line, "\r\n")] = 0; yading@11: yading@11: if (!read_ts(&p, &pts_start, &duration)) { yading@11: AVPacket *sub; yading@11: yading@11: sub = ff_subtitles_queue_insert(&mpl2->q, p, strlen(p), 0); yading@11: if (!sub) yading@11: return AVERROR(ENOMEM); yading@11: sub->pos = pos; yading@11: sub->pts = pts_start; yading@11: sub->duration = duration; yading@11: } yading@11: } yading@11: yading@11: ff_subtitles_queue_finalize(&mpl2->q); yading@11: return res; yading@11: } yading@11: yading@11: static int mpl2_read_packet(AVFormatContext *s, AVPacket *pkt) yading@11: { yading@11: MPL2Context *mpl2 = s->priv_data; yading@11: return ff_subtitles_queue_read_packet(&mpl2->q, pkt); yading@11: } yading@11: yading@11: static int mpl2_read_seek(AVFormatContext *s, int stream_index, yading@11: int64_t min_ts, int64_t ts, int64_t max_ts, int flags) yading@11: { yading@11: MPL2Context *mpl2 = s->priv_data; yading@11: return ff_subtitles_queue_seek(&mpl2->q, s, stream_index, yading@11: min_ts, ts, max_ts, flags); yading@11: } yading@11: yading@11: static int mpl2_read_close(AVFormatContext *s) yading@11: { yading@11: MPL2Context *mpl2 = s->priv_data; yading@11: ff_subtitles_queue_clean(&mpl2->q); yading@11: return 0; yading@11: } yading@11: yading@11: AVInputFormat ff_mpl2_demuxer = { yading@11: .name = "mpl2", yading@11: .long_name = NULL_IF_CONFIG_SMALL("MPL2 subtitles"), yading@11: .priv_data_size = sizeof(MPL2Context), yading@11: .read_probe = mpl2_probe, yading@11: .read_header = mpl2_read_header, yading@11: .read_packet = mpl2_read_packet, yading@11: .read_seek2 = mpl2_read_seek, yading@11: .read_close = mpl2_read_close, yading@11: .extensions = "txt,mpl2", yading@11: };