FFmpeg
Loading...
Searching...
No Matches
sccdec.c
Go to the documentation of this file.
1/*
2 * SCC subtitle demuxer
3 * Copyright (c) 2017 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avformat.h"
23#include "demux.h"
24#include "internal.h"
25#include "subtitles.h"
26#include "libavutil/avstring.h"
28
32
33static int scc_probe(const AVProbeData *p)
34{
35 char buf[18];
36 FFTextReader tr;
37
38 ff_text_init_buf(&tr, p->buf, p->buf_size);
39
40 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
41 ff_text_r8(&tr);
42
43 ff_text_read(&tr, buf, sizeof(buf));
44
45 if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
46 return AVPROBE_SCORE_MAX;
47
48 return 0;
49}
50
51static int convert(uint8_t x)
52{
53 if (x >= 'a')
54 x -= 'a' - 10;
55 else if (x >= 'A')
56 x -= 'A' - 10;
57 else
58 x -= '0';
59 return x;
60}
61
63{
64 SCCContext *scc = s->priv_data;
66 AVPacket *sub = NULL;
67 ptrdiff_t len;
68 uint8_t out[4096];
69 FFTextReader tr;
70
71 ff_text_init_avio(s, &tr, s->pb);
72
73 if (!st)
74 return AVERROR(ENOMEM);
75 avpriv_set_pts_info(st, 64, 1, 1000);
78
79 while (1) {
80 char *saveptr = NULL, *lline;
81 int hh, mm, ss, fs, i;
82 char line[4096];
83 int64_t pos, ts;
84
85 pos = ff_text_pos(&tr);
86 len = ff_subtitles_read_line(&tr, line, sizeof(line));
87 if (len <= 13) {
88 if (ff_text_eof(&tr))
89 break;
90 continue;
91 }
92 if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) != 4)
93 continue;
94
95 ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33LL;
96 if (sub)
97 sub->duration = ts - sub->pts;
98
99 lline = line;
100 lline += 12;
101
102 for (i = 0; i < 4095; i += 3) {
103 char *ptr = av_strtok(lline, " ", &saveptr);
104 char c1, c2, c3, c4;
105 uint8_t o1, o2;
106
107 if (!ptr)
108 break;
109
110 if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
111 break;
112 o1 = convert(c2) | (convert(c1) << 4);
113 o2 = convert(c4) | (convert(c3) << 4);
114
115 lline = NULL;
116
117 if (i > 12 && o1 == 0x94 && o2 == 0x20 && saveptr &&
118 (av_strncasecmp(saveptr, "942f", 4) && !av_strncasecmp(saveptr, "942c", 4))) {
119
120 sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
121 if (!sub)
122 return AVERROR(ENOMEM);
123
124 sub->pos = pos;
125 pos += i;
126 sub->pts = ts;
127 sub->duration = i * 11;
128 ts += sub->duration;
129 i = 0;
130 }
131
132 out[i+0] = 0xfc;
133 out[i+1] = o1;
134 out[i+2] = o2;
135 }
136
137 sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
138 if (!sub)
139 return AVERROR(ENOMEM);
140
141 sub->pos = pos;
142 sub->pts = ts;
143 }
144
146
147 return 0;
148}
149
151 .p.name = "scc",
152 .p.long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
153 .p.extensions = "scc",
154 .priv_data_size = sizeof(SCCContext),
155 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
159 .read_seek2 = ff_subtitles_read_seek,
161};
const FFInputFormat ff_scc_demuxer
Definition sccdec.c:150
static FILE * out
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
int av_sscanf(const char *string, const char *format,...)
Definition avsscanf.c:962
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define ss(width, name, subs,...)
Definition cbs_vp9.c:202
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_EIA_608
Definition codec_id.h:576
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition avstring.c:218
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static const uint64_t c2
Definition murmur3.c:53
static const uint64_t c1
Definition murmur3.c:52
static int scc_probe(const AVProbeData *p)
Definition sccdec.c:33
static int convert(uint8_t x)
Definition sccdec.c:51
static int scc_read_header(AVFormatContext *s)
Definition sccdec.c:62
unsigned int pos
Definition spdifenc.c:431
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
This structure stores compressed data.
Definition packet.h:580
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition packet.h:621
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:596
int64_t pos
byte position in stream, -1 if unknown
Definition packet.h:623
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
FFDemuxSubtitlesQueue q
Definition sccdec.c:30
ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
Read a line of text.
Definition subtitles.c:454
int64_t ff_text_pos(FFTextReader *r)
Return the byte position of the next byte returned by ff_text_r8().
Definition subtitles.c:60
int ff_subtitles_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition subtitles.c:331
AVPacket * ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, const uint8_t *event, size_t len, int merge)
Insert a new subtitle event.
Definition subtitles.c:111
void ff_text_read(FFTextReader *r, char *buf, size_t size)
Read the given number of bytes (in UTF-8).
Definition subtitles.c:86
int ff_subtitles_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition subtitles.c:337
int ff_subtitles_read_close(AVFormatContext *s)
Definition subtitles.c:345
int ff_text_eof(FFTextReader *r)
Return non-zero if EOF was reached.
Definition subtitles.c:92
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.
Definition subtitles.c:54
void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
Initialize the FFTextReader from the given AVIOContext.
Definition subtitles.c:28
int ff_text_r8(FFTextReader *r)
Return the next byte.
Definition subtitles.c:65
int ff_text_peek_r8(FFTextReader *r)
Like ff_text_r8(), but don't remove the byte from the buffer.
Definition subtitles.c:97
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.
Definition subtitles.c:212
int len