FFmpeg
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 "internal.h"
24 #include "subtitles.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/intreadwrite.h"
27 
28 typedef struct SCCContext {
30 } SCCContext;
31 
32 static int scc_probe(const AVProbeData *p)
33 {
34  char buf[18];
35  FFTextReader tr;
36 
37  ff_text_init_buf(&tr, p->buf, p->buf_size);
38 
39  while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
40  ff_text_r8(&tr);
41 
42  ff_text_read(&tr, buf, sizeof(buf));
43 
44  if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
45  return AVPROBE_SCORE_MAX;
46 
47  return 0;
48 }
49 
50 static int convert(uint8_t x)
51 {
52  if (x >= 'a')
53  x -= 87;
54  else if (x >= 'A')
55  x -= 55;
56  else
57  x -= '0';
58  return x;
59 }
60 
62 {
63  SCCContext *scc = s->priv_data;
65  char line[4096], line2[4096];
66  int count = 0, ret = 0;
67  ptrdiff_t len2, 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 (!ff_text_eof(&tr)) {
80  const int64_t pos = ff_text_pos(&tr);
81  char *saveptr = NULL, *lline;
82  int hh1, mm1, ss1, fs1, i;
83  int hh2, mm2, ss2, fs2;
84  int64_t ts_start, ts_end;
85  AVPacket *sub;
86 
87  if (count == 0) {
88  while (!ff_text_eof(&tr)) {
89  len = ff_subtitles_read_line(&tr, line, sizeof(line));
90  if (len > 13)
91  break;
92  }
93  }
94 
95  if (!strncmp(line, "Scenarist_SCC V1.0", 18))
96  continue;
97  if (sscanf(line, "%d:%d:%d%*[:;]%d", &hh1, &mm1, &ss1, &fs1) != 4)
98  continue;
99 
100  ts_start = (hh1 * 3600LL + mm1 * 60LL + ss1) * 1000LL + fs1 * 33;
101 
102  while (!ff_text_eof(&tr)) {
103  len2 = ff_subtitles_read_line(&tr, line2, sizeof(line2));
104  if (len2 > 13)
105  break;
106  }
107  if (sscanf(line2, "%d:%d:%d%*[:;]%d", &hh2, &mm2, &ss2, &fs2) != 4)
108  continue;
109 
110  ts_end = (hh2 * 3600LL + mm2 * 60LL + ss2) * 1000LL + fs2 * 33;
111  count++;
112 
113 try_again:
114  lline = (char *)&line;
115  lline += 12;
116 
117  for (i = 0; i < 4095; i += 3) {
118  char *ptr = av_strtok(lline, " ", &saveptr);
119  char c1, c2, c3, c4;
120 
121  if (!ptr)
122  break;
123 
124  if (sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
125  break;
126 
127  lline = NULL;
128  out[i+0] = 0xfc;
129  out[i+1] = convert(c2) | (convert(c1) << 4);
130  out[i+2] = convert(c4) | (convert(c3) << 4);
131  }
132  out[i] = 0;
133 
134  sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
135  if (!sub)
136  goto fail;
137 
138  sub->pos = pos;
139  sub->pts = ts_start;
140  sub->duration = FFMAX(1200, ts_end - ts_start);
141  memmove(line, line2, sizeof(line));
142  line2[0] = 0;
143  FFSWAP(ptrdiff_t, len, len2);
144  }
145 
146  if (line[0])
147  goto try_again;
148 
150 
151  return ret;
152 fail:
154  return AVERROR(ENOMEM);
155 }
156 
158 {
159  SCCContext *scc = s->priv_data;
160  return ff_subtitles_queue_read_packet(&scc->q, pkt);
161 }
162 
163 static int scc_read_seek(AVFormatContext *s, int stream_index,
164  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
165 {
166  SCCContext *scc = s->priv_data;
167  return ff_subtitles_queue_seek(&scc->q, s, stream_index,
168  min_ts, ts, max_ts, flags);
169 }
170 
172 {
173  SCCContext *scc = s->priv_data;
175  return 0;
176 }
177 
179  .name = "scc",
180  .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
181  .priv_data_size = sizeof(SCCContext),
185  .read_seek2 = scc_read_seek,
187  .extensions = "scc",
188 };
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: avcodec.h:669
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
ff_text_r8
int ff_text_r8(FFTextReader *r)
Return the next byte.
Definition: subtitles.c:65
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
out
FILE * out
Definition: movenc.c:54
FFSWAP
#define FFSWAP(type, a, b)
Definition: common.h:99
count
void INT64 INT64 count
Definition: avisynth_c.h:767
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1495
FFTextReader
Definition: subtitles.h:40
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:449
c1
static const uint64_t c1
Definition: murmur3.c:49
ff_subtitles_read_line
ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
Read a line of text.
Definition: subtitles.c:410
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
fail
#define fail()
Definition: checkasm.h:120
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
SCCContext
Definition: sccdec.c:28
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.
Definition: subtitles.c:249
scc_probe
static int scc_probe(const AVProbeData *p)
Definition: sccdec.c:32
buf
void * buf
Definition: avisynth_c.h:766
AVInputFormat
Definition: avformat.h:640
ff_text_init_buf
void ff_text_init_buf(FFTextReader *r, void *buf, size_t size)
Similar to ff_text_init_avio(), but sets it up to read from a bounded buffer.
Definition: subtitles.c:53
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
av_strtok
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:184
scc_read_header
static int scc_read_header(AVFormatContext *s)
Definition: sccdec.c:61
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: subtitles.c:212
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
ff_text_read
void ff_text_read(FFTextReader *r, char *buf, size_t size)
Read the given number of bytes (in UTF-8).
Definition: subtitles.c:86
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
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.
Definition: subtitles.c:111
ff_text_init_avio
void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
Initialize the FFTextReader from the given AVIOContext.
Definition: subtitles.c:27
scc_read_close
static int scc_read_close(AVFormatContext *s)
Definition: sccdec.c:171
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
FFMAX
#define FFMAX(a, b)
Definition: common.h:94
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4910
ff_subtitles_queue_finalize
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:194
FFDemuxSubtitlesQueue
Definition: subtitles.h:102
ff_subtitles_queue_clean
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition: subtitles.c:301
line
Definition: graph2dot.c:48
bprint.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
ff_text_pos
int64_t ff_text_pos(FFTextReader *r)
Return the byte position of the next byte returned by ff_text_r8().
Definition: subtitles.c:60
uint8_t
uint8_t
Definition: audio_convert.c:194
len
int len
Definition: vorbis_enc_data.h:452
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:870
scc_read_packet
static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: sccdec.c:157
avformat.h
SCCContext::q
FFDemuxSubtitlesQueue q
Definition: sccdec.c:29
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: subtitles.c:97
c2
static const uint64_t c2
Definition: murmur3.c:50
subtitles.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
ff_scc_demuxer
AVInputFormat ff_scc_demuxer
Definition: sccdec.c:178
ff_text_eof
int ff_text_eof(FFTextReader *r)
Return non-zero if EOF was reached.
Definition: subtitles.c:92
scc_read_seek
static int scc_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: sccdec.c:163
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1497
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
convert
static int convert(uint8_t x)
Definition: sccdec.c:50