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/avstring.h"
26 #include "libavutil/bprint.h"
27 #include "libavutil/intreadwrite.h"
28 
29 typedef struct SCCContext {
31 } SCCContext;
32 
33 static 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 
51 static int convert(uint8_t x)
52 {
53  if (x >= 'a')
54  x -= 87;
55  else if (x >= 'A')
56  x -= 55;
57  else
58  x -= '0';
59  return x;
60 }
61 
63 {
64  SCCContext *scc = s->priv_data;
66  char line2[4096], line[4096];
67  int64_t pos, ts, next_ts = AV_NOPTS_VALUE;
68  ptrdiff_t len;
69  uint8_t out[4096];
70  FFTextReader tr;
71 
72  ff_text_init_avio(s, &tr, s->pb);
73 
74  if (!st)
75  return AVERROR(ENOMEM);
76  avpriv_set_pts_info(st, 64, 1, 1000);
79 
80  while (!ff_text_eof(&tr) || next_ts == AV_NOPTS_VALUE || line2[0]) {
81  char *saveptr = NULL, *lline;
82  int hh, mm, ss, fs, i;
83  AVPacket *sub;
84 
85  if (next_ts == AV_NOPTS_VALUE) {
86  while (!ff_text_eof(&tr)) {
87  len = ff_subtitles_read_line(&tr, line, sizeof(line));
88  if (len <= 13)
89  continue;
90  if (!strncmp(line, "Scenarist_SCC V1.0", 18))
91  continue;
92  if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
93  break;
94  }
95 
96  ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33LL;
97 
98  while (!ff_text_eof(&tr)) {
99  len = ff_subtitles_read_line(&tr, line2, sizeof(line2));
100  if (len <= 13)
101  continue;
102 
103  if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
104  break;
105  }
106  } else {
107  memmove(line, line2, sizeof(line));
108  line2[0] = 0;
109 
110  while (!ff_text_eof(&tr)) {
111  len = ff_subtitles_read_line(&tr, line2, sizeof(line2));
112  if (len <= 13)
113  continue;
114 
115  if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
116  break;
117  }
118  }
119 
120  next_ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33LL;
121 
122  pos = ff_text_pos(&tr);
123  lline = (char *)&line;
124  lline += 12;
125 
126  for (i = 0; i < 4095; i += 3) {
127  char *ptr = av_strtok(lline, " ", &saveptr);
128  char c1, c2, c3, c4;
129  uint8_t o1, o2;
130 
131  if (!ptr)
132  break;
133 
134  if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
135  break;
136  o1 = convert(c2) | (convert(c1) << 4);
137  o2 = convert(c4) | (convert(c3) << 4);
138 
139  lline = NULL;
140 
141  if (i > 12 && o1 == 0x94 && o2 == 0x20 && saveptr &&
142  (av_strncasecmp(saveptr, "942f", 4) && !av_strncasecmp(saveptr, "942c", 4))) {
143 
144  out[i] = 0;
145 
146  sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
147  if (!sub)
148  return AVERROR(ENOMEM);
149 
150  sub->pos = pos;
151  pos += i;
152  sub->pts = ts;
153  sub->duration = i * 11;
154  ts += sub->duration;
155  i = 0;
156  }
157 
158  out[i+0] = 0xfc;
159  out[i+1] = o1;
160  out[i+2] = o2;
161  }
162 
163  out[i] = 0;
164 
165  sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
166  if (!sub)
167  return AVERROR(ENOMEM);
168 
169  sub->pos = pos;
170  sub->pts = ts;
171  sub->duration = next_ts - ts;
172  ts = next_ts;
173  }
174 
176 
177  return 0;
178 }
179 
181  .name = "scc",
182  .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
183  .priv_data_size = sizeof(SCCContext),
184  .flags_internal = FF_FMT_INIT_CLEANUP,
187  .extensions = "scc",
189  .read_seek2 = ff_subtitles_read_seek,
191 };
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:531
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
ff_subtitles_read_close
int ff_subtitles_read_close(AVFormatContext *s)
Definition: subtitles.c:329
ff_text_r8
int ff_text_r8(FFTextReader *r)
Return the next byte.
Definition: subtitles.c:63
FF_FMT_INIT_CLEANUP
#define FF_FMT_INIT_CLEANUP
For an AVInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: internal.h:48
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:724
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
out
FILE * out
Definition: movenc.c:54
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:31
FFTextReader
Definition: subtitles.h:41
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:51
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:435
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:141
SCCContext
Definition: sccdec.c:29
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
scc_probe
static int scc_probe(const AVProbeData *p)
Definition: sccdec.c:33
AVInputFormat
Definition: avformat.h:626
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:52
ff_subtitles_read_packet
int ff_subtitles_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: subtitles.c:315
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:631
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:186
scc_read_header
static int scc_read_header(AVFormatContext *s)
Definition: sccdec.c:62
AVFormatContext
Format I/O context.
Definition: avformat.h:1097
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:84
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:992
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:960
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
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:109
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:26
ff_scc_demuxer
const AVInputFormat ff_scc_demuxer
Definition: sccdec.c:180
av_strncasecmp
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:225
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:117
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
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:196
FFDemuxSubtitlesQueue
Definition: subtitles.h:103
line
Definition: graph2dot.c:48
bprint.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
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:58
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: subtitles.c:321
len
int len
Definition: vorbis_enc_data.h:426
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:845
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
SCCContext::q
FFDemuxSubtitlesQueue q
Definition: sccdec.c:30
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:95
c2
static const uint64_t c2
Definition: murmur3.c:52
subtitles.h
avpriv_set_pts_info
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: utils.c:1146
ff_text_eof
int ff_text_eof(FFTextReader *r)
Return non-zero if EOF was reached.
Definition: subtitles.c:90
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
convert
static int convert(uint8_t x)
Definition: sccdec.c:51
avstring.h