2010-12-29 01:52:53 +02:00
|
|
|
/*
|
|
|
|
* SubRip subtitle decoder
|
|
|
|
* Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.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
|
|
|
|
*/
|
|
|
|
|
2022-02-23 14:56:49 +02:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2010-12-29 01:52:53 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2012-10-25 01:01:42 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/parseutils.h"
|
2010-12-29 01:52:53 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "ass.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2015-08-30 16:24:47 +02:00
|
|
|
#include "htmlsubtitles.h"
|
2014-09-28 12:57:46 +03:00
|
|
|
|
2017-05-06 01:50:58 +02:00
|
|
|
static int srt_to_ass(AVCodecContext *avctx, AVBPrint *dst,
|
2014-09-28 12:57:46 +03:00
|
|
|
const char *in, int x1, int y1, int x2, int y2)
|
2010-12-29 01:52:53 +02:00
|
|
|
{
|
|
|
|
if (x1 >= 0 && y1 >= 0) {
|
2015-02-08 13:38:06 +02:00
|
|
|
/* XXX: here we rescale coordinate assuming they are in DVD resolution
|
|
|
|
* (720x480) since we don't have anything better */
|
|
|
|
|
|
|
|
if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1) && x2 >= x1 && y2 >= y1) {
|
|
|
|
/* text rectangle defined, write the text at the center of the rectangle */
|
|
|
|
const int cx = x1 + (x2 - x1)/2;
|
|
|
|
const int cy = y1 + (y2 - y1)/2;
|
2017-02-20 00:37:53 +02:00
|
|
|
const int scaled_x = cx * (int64_t)ASS_DEFAULT_PLAYRESX / 720;
|
|
|
|
const int scaled_y = cy * (int64_t)ASS_DEFAULT_PLAYRESY / 480;
|
2015-02-08 13:38:06 +02:00
|
|
|
av_bprintf(dst, "{\\an5}{\\pos(%d,%d)}", scaled_x, scaled_y);
|
|
|
|
} else {
|
|
|
|
/* only the top left corner, assume the text starts in that corner */
|
2017-02-20 00:37:53 +02:00
|
|
|
const int scaled_x = x1 * (int64_t)ASS_DEFAULT_PLAYRESX / 720;
|
|
|
|
const int scaled_y = y1 * (int64_t)ASS_DEFAULT_PLAYRESY / 480;
|
2015-02-08 13:38:06 +02:00
|
|
|
av_bprintf(dst, "{\\an1}{\\pos(%d,%d)}", scaled_x, scaled_y);
|
|
|
|
}
|
2010-12-29 01:52:53 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 01:50:58 +02:00
|
|
|
return ff_htmlmarkup_to_ass(avctx, dst, in);
|
2010-12-29 01:52:53 +02:00
|
|
|
}
|
|
|
|
|
2022-03-30 19:52:41 +02:00
|
|
|
static int srt_decode_frame(AVCodecContext *avctx, AVSubtitle *sub,
|
2022-03-30 23:42:44 +02:00
|
|
|
int *got_sub_ptr, const AVPacket *avpkt)
|
2010-12-29 01:52:53 +02:00
|
|
|
{
|
2014-09-28 12:57:46 +03:00
|
|
|
AVBPrint buffer;
|
2016-01-06 14:43:23 +02:00
|
|
|
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
|
2021-03-06 18:29:37 +02:00
|
|
|
int ret;
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t size;
|
2012-10-25 01:01:42 +03:00
|
|
|
const uint8_t *p = av_packet_get_side_data(avpkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
|
2016-01-06 14:43:23 +02:00
|
|
|
FFASSDecoderContext *s = avctx->priv_data;
|
2012-10-25 01:01:42 +03:00
|
|
|
|
|
|
|
if (p && size == 16) {
|
|
|
|
x1 = AV_RL32(p );
|
|
|
|
y1 = AV_RL32(p + 4);
|
|
|
|
x2 = AV_RL32(p + 8);
|
|
|
|
y2 = AV_RL32(p + 12);
|
|
|
|
}
|
2010-12-29 01:52:53 +02:00
|
|
|
|
|
|
|
if (avpkt->size <= 0)
|
|
|
|
return avpkt->size;
|
|
|
|
|
2014-09-28 12:57:46 +03:00
|
|
|
av_bprint_init(&buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
|
|
|
|
|
2017-05-06 01:50:58 +02:00
|
|
|
ret = srt_to_ass(avctx, &buffer, avpkt->data, x1, y1, x2, y2);
|
|
|
|
if (ret >= 0)
|
|
|
|
ret = ff_ass_add_rect(sub, buffer.str, s->readorder++, 0, NULL, NULL);
|
2014-09-28 12:57:46 +03:00
|
|
|
av_bprint_finalize(&buffer, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2010-12-29 01:52:53 +02:00
|
|
|
|
|
|
|
*got_sub_ptr = sub->num_rects > 0;
|
|
|
|
return avpkt->size;
|
|
|
|
}
|
|
|
|
|
2012-08-12 06:27:51 +03:00
|
|
|
#if CONFIG_SRT_DECODER
|
2012-10-25 01:01:42 +03:00
|
|
|
/* deprecated decoder */
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_srt_decoder = {
|
|
|
|
.p.name = "srt",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
|
|
|
|
.p.type = AVMEDIA_TYPE_SUBTITLE,
|
|
|
|
.p.id = AV_CODEC_ID_SUBRIP,
|
2010-12-29 01:52:53 +02:00
|
|
|
.init = ff_ass_subtitle_header_default,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_SUB_CB(srt_decode_frame),
|
2016-01-06 14:43:23 +02:00
|
|
|
.flush = ff_ass_decoder_flush,
|
|
|
|
.priv_data_size = sizeof(FFASSDecoderContext),
|
2010-12-29 01:52:53 +02:00
|
|
|
};
|
2012-08-12 06:27:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_SUBRIP_DECODER
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_subrip_decoder = {
|
|
|
|
.p.name = "subrip",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
|
|
|
|
.p.type = AVMEDIA_TYPE_SUBTITLE,
|
|
|
|
.p.id = AV_CODEC_ID_SUBRIP,
|
2012-08-12 06:27:51 +03:00
|
|
|
.init = ff_ass_subtitle_header_default,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_SUB_CB(srt_decode_frame),
|
2016-01-06 14:43:23 +02:00
|
|
|
.flush = ff_ass_decoder_flush,
|
|
|
|
.priv_data_size = sizeof(FFASSDecoderContext),
|
2012-08-12 06:27:51 +03:00
|
|
|
};
|
|
|
|
#endif
|