2012-06-22 21:56:30 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Clément Bœsch
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AVFORMAT_SUBTITLES_H
|
|
|
|
#define AVFORMAT_SUBTITLES_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2014-09-02 20:52:07 +02:00
|
|
|
#include <stddef.h>
|
2012-06-22 21:56:30 +02:00
|
|
|
#include "avformat.h"
|
2012-06-17 11:43:09 +02:00
|
|
|
#include "libavutil/bprint.h"
|
2012-06-22 21:56:30 +02:00
|
|
|
|
avformat/vobsub: fix several issues.
Here is an extract of fate-samples/sub/vobsub.idx, with an additional
text at the end of each line to better identify each bitmap:
timestamp: 00:04:55:445, filepos: 00001b000 Ace!
timestamp: 00:05:00:049, filepos: 00001b800 Wake up, honey!
timestamp: 00:05:02:018, filepos: 00001c800 I gotta go to work.
timestamp: 00:05:02:035, filepos: 00001d000 <???>
timestamp: 00:05:04:203, filepos: 00001d800 Look after Clayton, okay?
timestamp: 00:05:05:947, filepos: 00001e800 I'll be back tonight.
timestamp: 00:05:07:957, filepos: 00001f800 Bye! Love you.
timestamp: 00:05:21:295, filepos: 000020800 Hey, Ace! What's up?
timestamp: 00:05:23:356, filepos: 000021800 Hey, how's it going?
timestamp: 00:05:24:640, filepos: 000022800 Remember what today is? The 3rd!
timestamp: 00:05:27:193, filepos: 000023800 Look over there!
timestamp: 00:05:28:369, filepos: 000024800 Where are they going?
timestamp: 00:05:28:361, filepos: 000025000 <???>
timestamp: 00:05:29:946, filepos: 000025800 Let's go see.
timestamp: 00:05:31:230, filepos: 000026000 I can't, man. I got Clayton.
Note the two "<???>": they are basically split subtitles (with the
previous one), which the dvdsub decoder is now supposed to reconstruct
with a previous commit. But also note that while the first chunk has
increasing timestamps,
timestamp: 00:05:02:018, filepos: 00001c800
timestamp: 00:05:02:035, filepos: 00001d000
...it's not the case of the second one (and this is not an exception in the
original file):
timestamp: 00:05:28:369, filepos: 000024800
timestamp: 00:05:28:361, filepos: 000025000
For the dvdsub decoder, they need to be "filepos'ed" ordered, but the
FFDemuxSubtitlesQueue is timestamps ordered, which is the reason of the
introduction of a sub sort method in the context, to allow giving
priority to the position, and then the timestamps. With that change, the
dvdsub decoder get fed with ordered packets.
Now the packet size estimation was also broken: the filepos differences
in the vobsub index defines the full data read between two subtitles
chunks, and it is necessary to take into account what is read by the
mpegps_read_pes_header() function since the length returned by that
function doesn't count the size of the data it reads. This is fixed with
the introduction of total_read, and {old,new}_pos. By doing this change,
we can drop the unreliable len16 heuristic and simplify the whole loop.
Note that mpegps_read_pes_header() often read more than one PES packet
(typically in one call it can read 0x1ba and 0x1be chunk along with the
relevant 0x1bd packet), which triggers the "total_read + pkt_size >
psize" check. This is an expected behaviour, which could be avoided by
having a more chunked version of mpegps_read_pes_header().
The latest change is the extraction of each stream into its own
subtitles queue. If we don't do this, the maximum size for a subtitle
chunk is broken, and the previous changes can not work. Having each
stream in a different queue requires some little adjustments in the
seek code of the demuxer.
This commit is only meaningful as a whole change and can not be easily
split. The FATE test changes because it uses the vobsub demuxer.
2013-09-29 22:05:14 +02:00
|
|
|
enum sub_sort {
|
|
|
|
SUB_SORT_TS_POS = 0, ///< sort by timestamps, then position
|
|
|
|
SUB_SORT_POS_TS, ///< sort by position, then timestamps
|
|
|
|
};
|
|
|
|
|
2014-09-02 20:48:45 +02:00
|
|
|
enum ff_utf_type {
|
|
|
|
FF_UTF_8, // or other 8 bit encodings
|
|
|
|
FF_UTF16LE,
|
|
|
|
FF_UTF16BE,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int type;
|
|
|
|
AVIOContext *pb;
|
|
|
|
unsigned char buf[8];
|
|
|
|
int buf_pos, buf_len;
|
|
|
|
AVIOContext buf_pb;
|
|
|
|
} FFTextReader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the FFTextReader from the given AVIOContext. This function will
|
|
|
|
* read some bytes from pb, and test for UTF-8 or UTF-16 BOMs. Further accesses
|
|
|
|
* to FFTextReader will read more data from pb.
|
2014-10-29 01:32:44 +01:00
|
|
|
* If s is not NULL, the user will be warned if a UTF-16 conversion takes place.
|
2014-09-02 20:48:45 +02:00
|
|
|
*
|
|
|
|
* The purpose of FFTextReader is to transparently convert read data to UTF-8
|
|
|
|
* if the stream had a UTF-16 BOM.
|
|
|
|
*
|
2014-10-29 01:32:44 +01:00
|
|
|
* @param s Pointer to provide av_log context
|
2014-09-02 20:48:45 +02:00
|
|
|
* @param r object which will be initialized
|
|
|
|
* @param pb stream to read from (referenced as long as FFTextReader is in use)
|
|
|
|
*/
|
2014-10-29 01:32:44 +01:00
|
|
|
void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb);
|
2014-09-02 20:48:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to ff_text_init_avio(), but sets it up to read from a bounded buffer.
|
|
|
|
*
|
|
|
|
* @param r object which will be initialized
|
|
|
|
* @param buf buffer to read from (referenced as long as FFTextReader is in use)
|
|
|
|
* @param size size of buf
|
|
|
|
*/
|
|
|
|
void ff_text_init_buf(FFTextReader *r, void *buf, size_t size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the byte position of the next byte returned by ff_text_r8(). For
|
|
|
|
* UTF-16 source streams, this will return the original position, but it will
|
|
|
|
* be incorrect if a codepoint was only partially read with ff_text_r8().
|
|
|
|
*/
|
|
|
|
int64_t ff_text_pos(FFTextReader *r);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the next byte. The return value is always 0 - 255. Returns 0 on EOF.
|
|
|
|
* If the source stream is UTF-16, this reads from the stream converted to
|
|
|
|
* UTF-8. On invalid UTF-16, 0 is returned.
|
|
|
|
*/
|
|
|
|
int ff_text_r8(FFTextReader *r);
|
|
|
|
|
2014-09-02 20:52:07 +02:00
|
|
|
/**
|
|
|
|
* Return non-zero if EOF was reached.
|
|
|
|
*/
|
|
|
|
int ff_text_eof(FFTextReader *r);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like ff_text_r8(), but don't remove the byte from the buffer.
|
|
|
|
*/
|
|
|
|
int ff_text_peek_r8(FFTextReader *r);
|
|
|
|
|
2014-09-02 20:48:45 +02:00
|
|
|
/**
|
|
|
|
* Read the given number of bytes (in UTF-8). On error or EOF, \0 bytes are
|
|
|
|
* written.
|
|
|
|
*/
|
|
|
|
void ff_text_read(FFTextReader *r, char *buf, size_t size);
|
|
|
|
|
2012-06-22 21:56:30 +02:00
|
|
|
typedef struct {
|
|
|
|
AVPacket *subs; ///< array of subtitles packets
|
|
|
|
int nb_subs; ///< number of subtitles packets
|
|
|
|
int allocated_size; ///< allocated size for subs
|
|
|
|
int current_sub_idx; ///< current position for the read packet callback
|
avformat/vobsub: fix several issues.
Here is an extract of fate-samples/sub/vobsub.idx, with an additional
text at the end of each line to better identify each bitmap:
timestamp: 00:04:55:445, filepos: 00001b000 Ace!
timestamp: 00:05:00:049, filepos: 00001b800 Wake up, honey!
timestamp: 00:05:02:018, filepos: 00001c800 I gotta go to work.
timestamp: 00:05:02:035, filepos: 00001d000 <???>
timestamp: 00:05:04:203, filepos: 00001d800 Look after Clayton, okay?
timestamp: 00:05:05:947, filepos: 00001e800 I'll be back tonight.
timestamp: 00:05:07:957, filepos: 00001f800 Bye! Love you.
timestamp: 00:05:21:295, filepos: 000020800 Hey, Ace! What's up?
timestamp: 00:05:23:356, filepos: 000021800 Hey, how's it going?
timestamp: 00:05:24:640, filepos: 000022800 Remember what today is? The 3rd!
timestamp: 00:05:27:193, filepos: 000023800 Look over there!
timestamp: 00:05:28:369, filepos: 000024800 Where are they going?
timestamp: 00:05:28:361, filepos: 000025000 <???>
timestamp: 00:05:29:946, filepos: 000025800 Let's go see.
timestamp: 00:05:31:230, filepos: 000026000 I can't, man. I got Clayton.
Note the two "<???>": they are basically split subtitles (with the
previous one), which the dvdsub decoder is now supposed to reconstruct
with a previous commit. But also note that while the first chunk has
increasing timestamps,
timestamp: 00:05:02:018, filepos: 00001c800
timestamp: 00:05:02:035, filepos: 00001d000
...it's not the case of the second one (and this is not an exception in the
original file):
timestamp: 00:05:28:369, filepos: 000024800
timestamp: 00:05:28:361, filepos: 000025000
For the dvdsub decoder, they need to be "filepos'ed" ordered, but the
FFDemuxSubtitlesQueue is timestamps ordered, which is the reason of the
introduction of a sub sort method in the context, to allow giving
priority to the position, and then the timestamps. With that change, the
dvdsub decoder get fed with ordered packets.
Now the packet size estimation was also broken: the filepos differences
in the vobsub index defines the full data read between two subtitles
chunks, and it is necessary to take into account what is read by the
mpegps_read_pes_header() function since the length returned by that
function doesn't count the size of the data it reads. This is fixed with
the introduction of total_read, and {old,new}_pos. By doing this change,
we can drop the unreliable len16 heuristic and simplify the whole loop.
Note that mpegps_read_pes_header() often read more than one PES packet
(typically in one call it can read 0x1ba and 0x1be chunk along with the
relevant 0x1bd packet), which triggers the "total_read + pkt_size >
psize" check. This is an expected behaviour, which could be avoided by
having a more chunked version of mpegps_read_pes_header().
The latest change is the extraction of each stream into its own
subtitles queue. If we don't do this, the maximum size for a subtitle
chunk is broken, and the previous changes can not work. Having each
stream in a different queue requires some little adjustments in the
seek code of the demuxer.
This commit is only meaningful as a whole change and can not be easily
split. The FATE test changes because it uses the vobsub demuxer.
2013-09-29 22:05:14 +02:00
|
|
|
enum sub_sort sort; ///< sort method to use when finalizing subtitles
|
2012-06-22 21:56:30 +02:00
|
|
|
} FFDemuxSubtitlesQueue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new subtitle event.
|
|
|
|
*
|
|
|
|
* @param event the subtitle line, may not be zero terminated
|
|
|
|
* @param len the length of the event (in strlen() sense, so without '\0')
|
|
|
|
* @param merge set to 1 if the current event should be concatenated with the
|
|
|
|
* previous one instead of adding a new entry, 0 otherwise
|
|
|
|
*/
|
|
|
|
AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
|
2015-05-10 15:38:40 +02:00
|
|
|
const uint8_t *event, size_t len, int merge);
|
2012-06-22 21:56:30 +02:00
|
|
|
|
|
|
|
/**
|
2015-09-10 21:40:49 +02:00
|
|
|
* Set missing durations, sort subtitles by PTS (and then byte position), and
|
|
|
|
* drop duplicated events.
|
2012-06-22 21:56:30 +02:00
|
|
|
*/
|
2015-09-10 21:40:07 +02:00
|
|
|
void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q);
|
2012-06-22 21:56:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic read_packet() callback for subtitles demuxers using this queue
|
|
|
|
* system.
|
|
|
|
*/
|
|
|
|
int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt);
|
|
|
|
|
2012-11-23 21:40:46 +01:00
|
|
|
/**
|
|
|
|
* Update current_sub_idx to emulate a seek. Except the first parameter, it
|
|
|
|
* matches AVInputFormat->read_seek2 prototypes.
|
|
|
|
*/
|
|
|
|
int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
|
|
|
|
int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
|
|
|
|
|
2012-06-22 21:56:30 +02:00
|
|
|
/**
|
|
|
|
* Remove and destroy all the subtitles packets.
|
|
|
|
*/
|
|
|
|
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q);
|
|
|
|
|
2012-06-17 11:43:09 +02:00
|
|
|
/**
|
|
|
|
* SMIL helper to load next chunk ("<...>" or untagged content) in buf.
|
|
|
|
*
|
|
|
|
* @param c cached character, to avoid a backward seek
|
|
|
|
*/
|
2014-09-02 20:53:08 +02:00
|
|
|
int ff_smil_extract_next_text_chunk(FFTextReader *tr, AVBPrint *buf, char *c);
|
|
|
|
|
2012-06-17 11:43:09 +02:00
|
|
|
/**
|
|
|
|
* SMIL helper to point on the value of an attribute in the given tag.
|
|
|
|
*
|
|
|
|
* @param s SMIL tag ("<...>")
|
|
|
|
* @param attr the attribute to look for
|
|
|
|
*/
|
|
|
|
const char *ff_smil_get_attr_ptr(const char *s, const char *attr);
|
|
|
|
|
2012-12-28 01:15:01 +01:00
|
|
|
/**
|
2014-09-02 20:52:07 +02:00
|
|
|
* @brief Same as ff_subtitles_read_text_chunk(), but read from an AVIOContext.
|
|
|
|
*/
|
|
|
|
void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read a subtitles chunk from FFTextReader.
|
2012-12-28 01:15:01 +01:00
|
|
|
*
|
|
|
|
* A chunk is defined by a multiline "event", ending with a second line break.
|
2012-12-30 23:14:34 +01:00
|
|
|
* The trailing line breaks are trimmed. CRLF are supported.
|
2012-12-28 01:15:01 +01:00
|
|
|
* Example: "foo\r\nbar\r\n\r\nnext" will print "foo\r\nbar" into buf, and pb
|
|
|
|
* will focus on the 'n' of the "next" string.
|
|
|
|
*
|
2014-09-02 20:52:07 +02:00
|
|
|
* @param tr I/O context
|
2012-12-28 01:15:01 +01:00
|
|
|
* @param buf an initialized buf where the chunk is written
|
|
|
|
*
|
|
|
|
* @note buf is cleared before writing into it.
|
|
|
|
*/
|
2014-09-02 20:52:07 +02:00
|
|
|
void ff_subtitles_read_text_chunk(FFTextReader *tr, AVBPrint *buf);
|
2012-12-28 01:15:01 +01:00
|
|
|
|
2013-09-08 18:02:45 +02:00
|
|
|
/**
|
|
|
|
* Get the number of characters to increment to jump to the next line, or to
|
|
|
|
* the end of the string.
|
2013-09-10 01:07:48 +02:00
|
|
|
* The function handles the following line breaks schemes:
|
|
|
|
* LF, CRLF (MS), or standalone CR (old MacOS).
|
2013-09-08 18:02:45 +02:00
|
|
|
*/
|
|
|
|
static av_always_inline int ff_subtitles_next_line(const char *ptr)
|
|
|
|
{
|
2013-09-08 18:05:11 +02:00
|
|
|
int n = strcspn(ptr, "\r\n");
|
|
|
|
ptr += n;
|
|
|
|
if (*ptr == '\r') {
|
|
|
|
ptr++;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (*ptr == '\n')
|
|
|
|
n++;
|
|
|
|
return n;
|
2013-09-08 18:02:45 +02:00
|
|
|
}
|
|
|
|
|
2014-09-02 20:52:07 +02:00
|
|
|
/**
|
|
|
|
* Read a line of text. Discards line ending characters.
|
|
|
|
* The function handles the following line breaks schemes:
|
|
|
|
* LF, CRLF (MS), or standalone CR (old MacOS).
|
|
|
|
*
|
|
|
|
* Returns the number of bytes written to buf. Always writes a terminating 0,
|
|
|
|
* similar as with snprintf.
|
|
|
|
*
|
|
|
|
* @note returns a negative error code if a \0 byte is found
|
|
|
|
*/
|
|
|
|
ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size);
|
|
|
|
|
2012-06-22 21:56:30 +02:00
|
|
|
#endif /* AVFORMAT_SUBTITLES_H */
|