FFmpeg
Functions
subtitles.c File Reference
#include "avformat.h"
#include "subtitles.h"
#include "avio_internal.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/mem.h"

Go to the source code of this file.

Functions

void ff_text_init_avio (void *s, FFTextReader *r, AVIOContext *pb)
 Initialize the FFTextReader from the given AVIOContext. More...
 
void ff_text_init_buf (FFTextReader *r, const void *buf, size_t size)
 Similar to ff_text_init_avio(), but sets it up to read from a bounded buffer. More...
 
int64_t ff_text_pos (FFTextReader *r)
 Return the byte position of the next byte returned by ff_text_r8(). More...
 
int ff_text_r8 (FFTextReader *r)
 Return the next byte. More...
 
void ff_text_read (FFTextReader *r, char *buf, size_t size)
 Read the given number of bytes (in UTF-8). More...
 
int ff_text_eof (FFTextReader *r)
 Return non-zero if EOF was reached. More...
 
int ff_text_peek_r8 (FFTextReader *r)
 Like ff_text_r8(), but don't remove the byte from the buffer. More...
 
AVPacketff_subtitles_queue_insert (FFDemuxSubtitlesQueue *q, const uint8_t *event, size_t len, int merge)
 Insert a new subtitle event. More...
 
AVPacketff_subtitles_queue_insert_bprint (FFDemuxSubtitlesQueue *q, const AVBPrint *event, int merge)
 Same as ff_subtitles_queue_insert but takes an AVBPrint input. More...
 
static int cmp_pkt_sub_ts_pos (const void *a, const void *b)
 
static int cmp_pkt_sub_pos_ts (const void *a, const void *b)
 
static void drop_dups (void *log_ctx, FFDemuxSubtitlesQueue *q)
 
void ff_subtitles_queue_finalize (void *log_ctx, FFDemuxSubtitlesQueue *q)
 Set missing durations, sort subtitles by PTS (and then byte position), and drop duplicated events. More...
 
int ff_subtitles_queue_read_packet (FFDemuxSubtitlesQueue *q, AVPacket *pkt)
 Generic read_packet() callback for subtitles demuxers using this queue system. More...
 
static int search_sub_ts (const FFDemuxSubtitlesQueue *q, int64_t ts)
 
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)
 Update current_sub_idx to emulate a seek. More...
 
void ff_subtitles_queue_clean (FFDemuxSubtitlesQueue *q)
 Remove and destroy all the subtitles packets. More...
 
int ff_subtitles_read_packet (AVFormatContext *s, AVPacket *pkt)
 
int ff_subtitles_read_seek (AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
 
int ff_subtitles_read_close (AVFormatContext *s)
 
int ff_smil_extract_next_text_chunk (FFTextReader *tr, AVBPrint *buf, char *c)
 SMIL helper to load next chunk ("<...>" or untagged content) in buf. More...
 
const char * ff_smil_get_attr_ptr (const char *s, const char *attr)
 SMIL helper to point on the value of an attribute in the given tag. More...
 
static int is_eol (char c)
 
int ff_subtitles_read_text_chunk (FFTextReader *tr, AVBPrint *buf)
 Read a subtitles chunk from FFTextReader. More...
 
int ff_subtitles_read_chunk (AVIOContext *pb, AVBPrint *buf)
 Same as ff_subtitles_read_text_chunk(), but read from an AVIOContext. More...
 
ptrdiff_t ff_subtitles_read_line (FFTextReader *tr, char *buf, size_t size)
 Read a line of text. More...
 

Function Documentation

◆ ff_text_init_avio()

void ff_text_init_avio ( void *  s,
FFTextReader r,
AVIOContext pb 
)

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. If s is not NULL, the user will be warned if a UTF-16 conversion takes place.

The purpose of FFTextReader is to transparently convert read data to UTF-8 if the stream had a UTF-16 BOM.

Parameters
sPointer to provide av_log context
robject which will be initialized
pbstream to read from (referenced as long as FFTextReader is in use)

Definition at line 28 of file subtitles.c.

Referenced by ass_read_header(), ff_text_init_buf(), mcc_read_header(), realtext_read_header(), sami_read_header(), scc_read_header(), and srt_read_header().

◆ ff_text_init_buf()

void ff_text_init_buf ( FFTextReader r,
const void *  buf,
size_t  size 
)

Similar to ff_text_init_avio(), but sets it up to read from a bounded buffer.

Parameters
robject which will be initialized
bufbuffer to read from (referenced as long as FFTextReader is in use)
sizesize of buf

Definition at line 54 of file subtitles.c.

Referenced by ass_probe(), mcc_probe(), realtext_probe(), sami_probe(), scc_probe(), and srt_probe().

◆ ff_text_pos()

int64_t ff_text_pos ( FFTextReader r)

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().

Definition at line 60 of file subtitles.c.

Referenced by get_line(), mcc_read_header(), realtext_read_header(), sami_read_header(), scc_read_header(), and srt_read_header().

◆ ff_text_r8()

int ff_text_r8 ( 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.

Definition at line 65 of file subtitles.c.

Referenced by ass_probe(), ff_smil_extract_next_text_chunk(), ff_subtitles_read_line(), ff_subtitles_read_text_chunk(), ff_text_peek_r8(), ff_text_read(), get_line(), mcc_probe(), scc_probe(), and srt_probe().

◆ ff_text_read()

void ff_text_read ( FFTextReader r,
char *  buf,
size_t  size 
)

Read the given number of bytes (in UTF-8).

On error or EOF, \0 bytes are written.

Definition at line 86 of file subtitles.c.

Referenced by ass_probe(), mcc_probe(), realtext_probe(), sami_probe(), and scc_probe().

◆ ff_text_eof()

int ff_text_eof ( FFTextReader r)

Return non-zero if EOF was reached.

Definition at line 92 of file subtitles.c.

Referenced by ff_subtitles_read_line(), mcc_read_header(), realtext_read_header(), sami_read_header(), scc_read_header(), and srt_read_header().

◆ ff_text_peek_r8()

int ff_text_peek_r8 ( FFTextReader r)

Like ff_text_r8(), but don't remove the byte from the buffer.

Definition at line 97 of file subtitles.c.

Referenced by ass_probe(), ff_subtitles_read_line(), mcc_probe(), scc_probe(), and srt_probe().

◆ ff_subtitles_queue_insert()

AVPacket* ff_subtitles_queue_insert ( FFDemuxSubtitlesQueue q,
const uint8_t *  event,
size_t  len,
int  merge 
)

Insert a new subtitle event.

Parameters
eventthe subtitle line (not zero terminated) or NULL on not yet available event
lenthe length of the event (in strlen() sense, so without '\0')
mergeset to 1 if the current event should be concatenated with the previous one instead of adding a new entry, 0 otherwise

Definition at line 111 of file subtitles.c.

Referenced by aqt_read_header(), ff_subtitles_queue_insert_bprint(), jacosub_read_header(), lrc_read_header(), mcc_read_header(), microdvd_read_header(), mpl2_read_header(), pjs_read_header(), rcwt_read_header(), scc_read_header(), stl_read_header(), subviewer1_read_header(), subviewer_read_header(), vplayer_read_header(), and webvtt_read_header().

◆ ff_subtitles_queue_insert_bprint()

AVPacket* ff_subtitles_queue_insert_bprint ( FFDemuxSubtitlesQueue q,
const AVBPrint *  event,
int  merge 
)

Same as ff_subtitles_queue_insert but takes an AVBPrint input.

Avoids common errors like handling incomplete AVBPrint.

Definition at line 156 of file subtitles.c.

Referenced by add_event(), ass_read_header(), mpsub_read_header(), parse_file(), realtext_read_header(), and sami_read_header().

◆ cmp_pkt_sub_ts_pos()

static int cmp_pkt_sub_ts_pos ( const void *  a,
const void *  b 
)
static

Definition at line 164 of file subtitles.c.

Referenced by ff_subtitles_queue_finalize().

◆ cmp_pkt_sub_pos_ts()

static int cmp_pkt_sub_pos_ts ( const void *  a,
const void *  b 
)
static

Definition at line 173 of file subtitles.c.

Referenced by ff_subtitles_queue_finalize().

◆ drop_dups()

static void drop_dups ( void *  log_ctx,
FFDemuxSubtitlesQueue q 
)
static

Definition at line 185 of file subtitles.c.

Referenced by ff_subtitles_queue_finalize().

◆ ff_subtitles_queue_finalize()

void ff_subtitles_queue_finalize ( void *  log_ctx,
FFDemuxSubtitlesQueue q 
)

◆ ff_subtitles_queue_read_packet()

int ff_subtitles_queue_read_packet ( FFDemuxSubtitlesQueue q,
AVPacket pkt 
)

Generic read_packet() callback for subtitles demuxers using this queue system.

Definition at line 230 of file subtitles.c.

Referenced by aqt_read_packet(), ff_subtitles_read_packet(), microdvd_read_packet(), tedcaptions_read_packet(), and webvtt_read_packet().

◆ search_sub_ts()

static int search_sub_ts ( const FFDemuxSubtitlesQueue q,
int64_t  ts 
)
static

Definition at line 247 of file subtitles.c.

Referenced by ff_subtitles_queue_seek().

◆ ff_subtitles_queue_seek()

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 
)

Update current_sub_idx to emulate a seek.

Except the first parameter, it matches FFInputFormat->read_seek2 prototypes.

Definition at line 269 of file subtitles.c.

Referenced by aqt_read_seek(), ff_subtitles_read_seek(), microdvd_read_seek(), tedcaptions_read_seek(), and webvtt_read_seek().

◆ ff_subtitles_queue_clean()

void ff_subtitles_queue_clean ( FFDemuxSubtitlesQueue q)

Remove and destroy all the subtitles packets.

Definition at line 321 of file subtitles.c.

Referenced by aqt_read_close(), ff_subtitles_read_close(), microdvd_read_close(), tedcaptions_read_close(), and webvtt_read_close().

◆ ff_subtitles_read_packet()

int ff_subtitles_read_packet ( AVFormatContext s,
AVPacket pkt 
)

Definition at line 331 of file subtitles.c.

◆ ff_subtitles_read_seek()

int ff_subtitles_read_seek ( AVFormatContext s,
int  stream_index,
int64_t  min_ts,
int64_t  ts,
int64_t  max_ts,
int  flags 
)

Definition at line 337 of file subtitles.c.

◆ ff_subtitles_read_close()

int ff_subtitles_read_close ( AVFormatContext s)

Definition at line 345 of file subtitles.c.

◆ ff_smil_extract_next_text_chunk()

int ff_smil_extract_next_text_chunk ( FFTextReader tr,
AVBPrint *  buf,
char *  c 
)

SMIL helper to load next chunk ("<...>" or untagged content) in buf.

Parameters
ccached character, to avoid a backward seek
Returns
size of chunk or error, e.g. AVERROR(ENOMEM) on incomplete buf

Definition at line 352 of file subtitles.c.

Referenced by realtext_read_header(), and sami_read_header().

◆ ff_smil_get_attr_ptr()

const char* ff_smil_get_attr_ptr ( const char *  s,
const char *  attr 
)

SMIL helper to point on the value of an attribute in the given tag.

Parameters
sSMIL tag ("<...>")
attrthe attribute to look for

Definition at line 377 of file subtitles.c.

Referenced by realtext_read_header(), and sami_read_header().

◆ is_eol()

static int is_eol ( char  c)
inlinestatic

Definition at line 397 of file subtitles.c.

Referenced by ff_subtitles_read_text_chunk().

◆ ff_subtitles_read_text_chunk()

int ff_subtitles_read_text_chunk ( FFTextReader tr,
AVBPrint *  buf 
)

Read a subtitles chunk from FFTextReader.

A chunk is defined by a multiline "event", ending with a second line break. The trailing line breaks are trimmed. CRLF are supported. 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.

Parameters
trI/O context
bufan initialized buf where the chunk is written
Returns
0 on success, error value otherwise, e.g. AVERROR(ENOMEM) if buf is incomplete
Note
buf is cleared before writing into it.

Definition at line 402 of file subtitles.c.

Referenced by ff_subtitles_read_chunk().

◆ ff_subtitles_read_chunk()

int ff_subtitles_read_chunk ( AVIOContext pb,
AVBPrint *  buf 
)

Same as ff_subtitles_read_text_chunk(), but read from an AVIOContext.

Definition at line 445 of file subtitles.c.

Referenced by mpsub_read_header(), and webvtt_read_header().

◆ ff_subtitles_read_line()

ptrdiff_t ff_subtitles_read_line ( FFTextReader tr,
char *  buf,
size_t  size 
)

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

Definition at line 454 of file subtitles.c.

Referenced by mcc_read_header(), scc_read_header(), srt_probe(), and srt_read_header().