2010-11-13 14:18:59 +00:00
|
|
|
/*
|
|
|
|
* SSA/ASS 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
|
|
|
|
*/
|
|
|
|
|
2012-08-06 13:49:32 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-11-13 14:18:59 +00:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "ass.h"
|
2022-03-16 17:18:28 +00:00
|
|
|
#include "codec_internal.h"
|
2022-02-23 12:56:49 +00:00
|
|
|
#include "config_components.h"
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/mem.h"
|
2010-11-13 14:18:59 +00:00
|
|
|
|
|
|
|
static av_cold int ass_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
2012-12-29 21:09:59 +00:00
|
|
|
avctx->subtitle_header = av_malloc(avctx->extradata_size + 1);
|
2012-01-29 13:47:04 +00:00
|
|
|
if (!avctx->subtitle_header)
|
2010-11-13 14:18:59 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2019-07-24 20:55:15 +00:00
|
|
|
if (avctx->extradata_size)
|
|
|
|
memcpy(avctx->subtitle_header, avctx->extradata, avctx->extradata_size);
|
2012-12-29 21:09:59 +00:00
|
|
|
avctx->subtitle_header[avctx->extradata_size] = 0;
|
2010-11-13 14:18:59 +00:00
|
|
|
avctx->subtitle_header_size = avctx->extradata_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-30 17:52:41 +00:00
|
|
|
static int ass_decode_frame(AVCodecContext *avctx, AVSubtitle *sub,
|
2022-03-30 21:42:44 +00:00
|
|
|
int *got_sub_ptr, const AVPacket *avpkt)
|
2010-12-16 08:16:33 +00:00
|
|
|
{
|
subtitles: introduce ASS codec id and use it.
Currently, we have a AV_CODEC_ID_SSA, which matches the way the ASS/SSA
markup is muxed in a standalone .ass/.ssa file. This means the AVPacket
data starts with a "Dialogue:" string, followed by a timing information
(start and end of the event as string) and a trailing CRLF after each
line. One packet can contain several lines. We'll refer to this layout
as "SSA" or "SSA lines".
In matroska, this markup is not stored as such: it has no "Dialogue:"
prefix, it contains a ReadOrder field, the timing information is not in
the payload, and it doesn't contain the trailing CRLF. See [1] for more
info. We'll refer to this layout as "ASS".
Since we have only one common codec for both formats, the matroska
demuxer is constructing an AVPacket following the "SSA lines" format.
This causes several problems, so it was decided to change this into
clean ASS packets.
Some insight about what is changed or unchanged in this commit:
CODECS
------
- the decoding process still writes "SSA lines" markup inside the ass
fields of the subtitles rectangles (sub->rects[n]->ass), which is
still the current common way of representing decoded subtitles
markup. It is meant to change later.
- new ASS codec id: AV_CODEC_ID_ASS (which is different from the
legacy AV_CODEC_ID_SSA)
- lavc/assdec: the "ass" decoder is renamed into "ssa" (instead of
"ass") for consistency with the codec id and allows to add a real
ass decoder. This ass decoder receives clean ASS lines (so it starts
with a ReadOrder, is followed by the Layer, etc). We make sure this
is decoded properly in a new ass-line rectangle of the decoded
subtitles (the ssa decoder OTOH is doing a simple straightforward
copy). Using the packet timing instead of data string makes sure the
ass-line now contains the appropriate timing.
- lavc/assenc: just like the ass decoder, the "ssa" encoder is renamed
into "ssa" (instead of "ass") for consistency with the codec id, and
allows to add a real "ass" encoder.
One important thing about this encoder is that it only supports one
ass rectangle: we could have put several dialogue events in the
AVPacket (separated by a \0 for instance) but this would have cause
trouble for the muxer which needs not only the start time, but also
the duration: typically, you have merged events with the same start
time (stored in the AVPacket->pts) but a different duration. At the
moment, only the matroska do the merge with the SSA-line codec.
We will need to make sure all the decoders in the future can't add
more than one rectangle (and only one Dialogue line in it
obviously).
FORMATS
-------
- lavf/assenc: the .ass/.ssa muxer can take both SSA and ASS packets.
In the case of ASS packets as input, it adds the timing based on the
AVPacket pts and duration, and mux it with "Dialogue:", trailing
CRLF, etc.
- lavf/assdec: unchanged; it currently still only outputs SSA-lines
packets.
- lavf/mkv: the demuxer can now output ASS packets without the need of
any "SSA-lines" reconstruction hack. It will become the default at
next libavformat bump, and the SSA support will be dropped from the
demuxer. The muxer can take ASS packets since it's muxed normally,
and still supports the old SSA packets. All the SSA support and
hacks in Matroska code will be dropped at next lavf bump.
[1]: http://www.matroska.org/technical/specs/subtitles/ssa.html
2013-01-03 02:06:43 +00:00
|
|
|
if (avpkt->size <= 0)
|
|
|
|
return avpkt->size;
|
|
|
|
|
2016-01-06 12:43:23 +00:00
|
|
|
sub->rects = av_malloc(sizeof(*sub->rects));
|
|
|
|
if (!sub->rects)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
|
|
|
|
if (!sub->rects[0])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
sub->num_rects = 1;
|
|
|
|
sub->rects[0]->type = SUBTITLE_ASS;
|
|
|
|
sub->rects[0]->ass = av_strdup(avpkt->data);
|
|
|
|
if (!sub->rects[0]->ass)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
*got_sub_ptr = 1;
|
subtitles: introduce ASS codec id and use it.
Currently, we have a AV_CODEC_ID_SSA, which matches the way the ASS/SSA
markup is muxed in a standalone .ass/.ssa file. This means the AVPacket
data starts with a "Dialogue:" string, followed by a timing information
(start and end of the event as string) and a trailing CRLF after each
line. One packet can contain several lines. We'll refer to this layout
as "SSA" or "SSA lines".
In matroska, this markup is not stored as such: it has no "Dialogue:"
prefix, it contains a ReadOrder field, the timing information is not in
the payload, and it doesn't contain the trailing CRLF. See [1] for more
info. We'll refer to this layout as "ASS".
Since we have only one common codec for both formats, the matroska
demuxer is constructing an AVPacket following the "SSA lines" format.
This causes several problems, so it was decided to change this into
clean ASS packets.
Some insight about what is changed or unchanged in this commit:
CODECS
------
- the decoding process still writes "SSA lines" markup inside the ass
fields of the subtitles rectangles (sub->rects[n]->ass), which is
still the current common way of representing decoded subtitles
markup. It is meant to change later.
- new ASS codec id: AV_CODEC_ID_ASS (which is different from the
legacy AV_CODEC_ID_SSA)
- lavc/assdec: the "ass" decoder is renamed into "ssa" (instead of
"ass") for consistency with the codec id and allows to add a real
ass decoder. This ass decoder receives clean ASS lines (so it starts
with a ReadOrder, is followed by the Layer, etc). We make sure this
is decoded properly in a new ass-line rectangle of the decoded
subtitles (the ssa decoder OTOH is doing a simple straightforward
copy). Using the packet timing instead of data string makes sure the
ass-line now contains the appropriate timing.
- lavc/assenc: just like the ass decoder, the "ssa" encoder is renamed
into "ssa" (instead of "ass") for consistency with the codec id, and
allows to add a real "ass" encoder.
One important thing about this encoder is that it only supports one
ass rectangle: we could have put several dialogue events in the
AVPacket (separated by a \0 for instance) but this would have cause
trouble for the muxer which needs not only the start time, but also
the duration: typically, you have merged events with the same start
time (stored in the AVPacket->pts) but a different duration. At the
moment, only the matroska do the merge with the SSA-line codec.
We will need to make sure all the decoders in the future can't add
more than one rectangle (and only one Dialogue line in it
obviously).
FORMATS
-------
- lavf/assenc: the .ass/.ssa muxer can take both SSA and ASS packets.
In the case of ASS packets as input, it adds the timing based on the
AVPacket pts and duration, and mux it with "Dialogue:", trailing
CRLF, etc.
- lavf/assdec: unchanged; it currently still only outputs SSA-lines
packets.
- lavf/mkv: the demuxer can now output ASS packets without the need of
any "SSA-lines" reconstruction hack. It will become the default at
next libavformat bump, and the SSA support will be dropped from the
demuxer. The muxer can take ASS packets since it's muxed normally,
and still supports the old SSA packets. All the SSA support and
hacks in Matroska code will be dropped at next lavf bump.
[1]: http://www.matroska.org/technical/specs/subtitles/ssa.html
2013-01-03 02:06:43 +00:00
|
|
|
return avpkt->size;
|
2010-12-16 08:16:33 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 18:55:36 +00:00
|
|
|
#if CONFIG_SSA_DECODER
|
2022-03-16 20:09:54 +00:00
|
|
|
const FFCodec ff_ssa_decoder = {
|
|
|
|
.p.name = "ssa",
|
2022-08-29 11:38:02 +00:00
|
|
|
CODEC_LONG_NAME("ASS (Advanced SubStation Alpha) subtitle"),
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.type = AVMEDIA_TYPE_SUBTITLE,
|
|
|
|
.p.id = AV_CODEC_ID_ASS,
|
2014-09-14 18:55:36 +00:00
|
|
|
.init = ass_decode_init,
|
2022-03-30 21:28:24 +00:00
|
|
|
FF_CODEC_DECODE_SUB_CB(ass_decode_frame),
|
2014-09-14 18:55:36 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_ASS_DECODER
|
2022-03-16 20:09:54 +00:00
|
|
|
const FFCodec ff_ass_decoder = {
|
|
|
|
.p.name = "ass",
|
2022-08-29 11:38:02 +00:00
|
|
|
CODEC_LONG_NAME("ASS (Advanced SubStation Alpha) subtitle"),
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.type = AVMEDIA_TYPE_SUBTITLE,
|
|
|
|
.p.id = AV_CODEC_ID_ASS,
|
2010-11-13 14:18:59 +00:00
|
|
|
.init = ass_decode_init,
|
2022-03-30 21:28:24 +00:00
|
|
|
FF_CODEC_DECODE_SUB_CB(ass_decode_frame),
|
2010-11-13 14:18:59 +00:00
|
|
|
};
|
subtitles: introduce ASS codec id and use it.
Currently, we have a AV_CODEC_ID_SSA, which matches the way the ASS/SSA
markup is muxed in a standalone .ass/.ssa file. This means the AVPacket
data starts with a "Dialogue:" string, followed by a timing information
(start and end of the event as string) and a trailing CRLF after each
line. One packet can contain several lines. We'll refer to this layout
as "SSA" or "SSA lines".
In matroska, this markup is not stored as such: it has no "Dialogue:"
prefix, it contains a ReadOrder field, the timing information is not in
the payload, and it doesn't contain the trailing CRLF. See [1] for more
info. We'll refer to this layout as "ASS".
Since we have only one common codec for both formats, the matroska
demuxer is constructing an AVPacket following the "SSA lines" format.
This causes several problems, so it was decided to change this into
clean ASS packets.
Some insight about what is changed or unchanged in this commit:
CODECS
------
- the decoding process still writes "SSA lines" markup inside the ass
fields of the subtitles rectangles (sub->rects[n]->ass), which is
still the current common way of representing decoded subtitles
markup. It is meant to change later.
- new ASS codec id: AV_CODEC_ID_ASS (which is different from the
legacy AV_CODEC_ID_SSA)
- lavc/assdec: the "ass" decoder is renamed into "ssa" (instead of
"ass") for consistency with the codec id and allows to add a real
ass decoder. This ass decoder receives clean ASS lines (so it starts
with a ReadOrder, is followed by the Layer, etc). We make sure this
is decoded properly in a new ass-line rectangle of the decoded
subtitles (the ssa decoder OTOH is doing a simple straightforward
copy). Using the packet timing instead of data string makes sure the
ass-line now contains the appropriate timing.
- lavc/assenc: just like the ass decoder, the "ssa" encoder is renamed
into "ssa" (instead of "ass") for consistency with the codec id, and
allows to add a real "ass" encoder.
One important thing about this encoder is that it only supports one
ass rectangle: we could have put several dialogue events in the
AVPacket (separated by a \0 for instance) but this would have cause
trouble for the muxer which needs not only the start time, but also
the duration: typically, you have merged events with the same start
time (stored in the AVPacket->pts) but a different duration. At the
moment, only the matroska do the merge with the SSA-line codec.
We will need to make sure all the decoders in the future can't add
more than one rectangle (and only one Dialogue line in it
obviously).
FORMATS
-------
- lavf/assenc: the .ass/.ssa muxer can take both SSA and ASS packets.
In the case of ASS packets as input, it adds the timing based on the
AVPacket pts and duration, and mux it with "Dialogue:", trailing
CRLF, etc.
- lavf/assdec: unchanged; it currently still only outputs SSA-lines
packets.
- lavf/mkv: the demuxer can now output ASS packets without the need of
any "SSA-lines" reconstruction hack. It will become the default at
next libavformat bump, and the SSA support will be dropped from the
demuxer. The muxer can take ASS packets since it's muxed normally,
and still supports the old SSA packets. All the SSA support and
hacks in Matroska code will be dropped at next lavf bump.
[1]: http://www.matroska.org/technical/specs/subtitles/ssa.html
2013-01-03 02:06:43 +00:00
|
|
|
#endif
|