2012-06-17 19:11:49 +00:00
|
|
|
/*
|
|
|
|
* SubRip subtitle muxer
|
|
|
|
* Copyright (c) 2012 Nicolas George <nicolas.george@normalesup.org>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "libavutil/log.h"
|
2012-11-30 23:35:01 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2012-06-17 19:11:49 +00:00
|
|
|
|
|
|
|
/* TODO: add options for:
|
|
|
|
- character encoding;
|
|
|
|
- LF / CRLF;
|
|
|
|
- byte order mark.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct SRTContext{
|
|
|
|
unsigned index;
|
|
|
|
} SRTContext;
|
|
|
|
|
|
|
|
static int srt_write_header(AVFormatContext *avf)
|
|
|
|
{
|
2012-10-24 22:27:10 +00:00
|
|
|
SRTContext *srt = avf->priv_data;
|
|
|
|
|
2012-06-17 19:11:49 +00:00
|
|
|
if (avf->nb_streams != 1 ||
|
|
|
|
avf->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
|
|
|
|
av_log(avf, AV_LOG_ERROR,
|
|
|
|
"SRT supports only a single subtitles stream.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2012-08-07 20:45:46 +00:00
|
|
|
if (avf->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT &&
|
2014-09-14 18:30:27 +00:00
|
|
|
avf->streams[0]->codec->codec_id != AV_CODEC_ID_SUBRIP) {
|
2012-06-17 19:11:49 +00:00
|
|
|
av_log(avf, AV_LOG_ERROR,
|
|
|
|
"Unsupported subtitles codec: %s\n",
|
|
|
|
avcodec_get_name(avf->streams[0]->codec->codec_id));
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
|
2012-10-24 22:27:10 +00:00
|
|
|
srt->index = 1;
|
2012-06-17 19:11:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
SRTContext *srt = avf->priv_data;
|
|
|
|
|
2014-09-14 18:30:27 +00:00
|
|
|
// TODO: reindent
|
2012-06-17 19:11:49 +00:00
|
|
|
int64_t s = pkt->pts, e, d = pkt->duration;
|
2012-11-30 23:35:01 +00:00
|
|
|
int size, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
|
|
|
|
const uint8_t *p;
|
|
|
|
|
|
|
|
p = av_packet_get_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
|
|
|
|
if (p && size == 16) {
|
|
|
|
x1 = AV_RL32(p );
|
|
|
|
y1 = AV_RL32(p + 4);
|
|
|
|
x2 = AV_RL32(p + 8);
|
|
|
|
y2 = AV_RL32(p + 12);
|
|
|
|
}
|
2012-06-17 19:11:49 +00:00
|
|
|
|
|
|
|
if (d <= 0)
|
matroskadec, matroskadec, srtenc: Read/Write duration for subtitles.
After much discussion and back-and-forth, we reached the conclusion
that matroska uses convergence_duration for subtitle duration because
a 32bit value isn't large enough to store the duration if sub-micro-second
timebases are used. Matroska may not be the only one that supports these
timebases, but it's certainly the only one that ffmpeg attempts to support
in this way.
The long term solution that we seemed to reach was that if we encounter
a matroska file with a sub-micro-second timebase, we should internally
scale it up to at least micro-second, and then duration can be used
normally. This suggests that on the encode side, we should not allow
generation of files with sub-micro-second timebases, but that's a separate
issue.
That being a non-trivial change, and the subtitle interoperability breakage
being very real, I'm re-submitting this small change for consideration.
In this diff, we make sure that duration is populated by the matroska
demuxer, and that convergence_duration is respected in matroskaenc and
srtenc, but that duration is used otherwise. This ends up being a strict
improvement - pipelines that use convergence duration are unchanged, and
ones that are currently broken due to the duration mismatch will start
working - except for the ones with the extreme timebases, but those were
already broken.
Signed-off-by: Philip Langdale <philipl@overt.org>
2012-05-23 04:50:02 +00:00
|
|
|
/* For backward compatibility, fallback to convergence_duration. */
|
2012-06-17 19:11:49 +00:00
|
|
|
d = pkt->convergence_duration;
|
2012-10-24 22:26:13 +00:00
|
|
|
if (s == AV_NOPTS_VALUE || d < 0) {
|
2012-10-24 22:27:10 +00:00
|
|
|
av_log(avf, AV_LOG_WARNING,
|
|
|
|
"Insufficient timestamps in event number %d.\n", srt->index);
|
|
|
|
return 0;
|
2012-06-17 19:11:49 +00:00
|
|
|
}
|
|
|
|
e = s + d;
|
2012-11-30 23:35:01 +00:00
|
|
|
avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
|
2012-06-17 19:11:49 +00:00
|
|
|
srt->index,
|
|
|
|
(int)(s / 3600000), (int)(s / 60000) % 60,
|
|
|
|
(int)(s / 1000) % 60, (int)(s % 1000),
|
|
|
|
(int)(e / 3600000), (int)(e / 60000) % 60,
|
|
|
|
(int)(e / 1000) % 60, (int)(e % 1000));
|
2012-11-30 23:35:01 +00:00
|
|
|
if (p)
|
|
|
|
avio_printf(avf->pb, " X1:%03d X2:%03d Y1:%03d Y2:%03d",
|
|
|
|
x1, x2, y1, y2);
|
|
|
|
avio_printf(avf->pb, "\n");
|
2014-09-14 18:30:27 +00:00
|
|
|
|
2012-06-17 19:11:49 +00:00
|
|
|
avio_write(avf->pb, pkt->data, pkt->size);
|
2014-09-14 18:30:27 +00:00
|
|
|
avio_write(avf->pb, "\n\n", 2);
|
2012-10-24 22:27:10 +00:00
|
|
|
srt->index++;
|
2012-06-17 19:11:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVOutputFormat ff_srt_muxer = {
|
|
|
|
.name = "srt",
|
2012-07-30 21:04:28 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
|
2012-06-17 19:11:49 +00:00
|
|
|
.mime_type = "application/x-subrip",
|
|
|
|
.extensions = "srt",
|
|
|
|
.priv_data_size = sizeof(SRTContext),
|
|
|
|
.write_header = srt_write_header,
|
|
|
|
.write_packet = srt_write_packet,
|
2012-10-26 22:01:49 +00:00
|
|
|
.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
|
2012-10-21 01:12:04 +00:00
|
|
|
.subtitle_codec = AV_CODEC_ID_SUBRIP,
|
2012-06-17 19:11:49 +00:00
|
|
|
};
|