FFmpeg
mccdec.c
Go to the documentation of this file.
1 /*
2  * MCC subtitle demuxer
3  * Copyright (c) 2020 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"
27 #include "libavutil/bprint.h"
28 #include "libavutil/intreadwrite.h"
29 
30 typedef struct MCCContext {
32 } MCCContext;
33 
34 static int mcc_probe(const AVProbeData *p)
35 {
36  char buf[28];
37  FFTextReader tr;
38 
39  ff_text_init_buf(&tr, p->buf, p->buf_size);
40 
41  while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
42  ff_text_r8(&tr);
43 
44  ff_text_read(&tr, buf, sizeof(buf));
45 
46  if (!memcmp(buf, "File Format=MacCaption_MCC V", 28))
47  return AVPROBE_SCORE_MAX;
48 
49  return 0;
50 }
51 
52 static int convert(uint8_t x)
53 {
54  if (x >= 'a')
55  x -= 87;
56  else if (x >= 'A')
57  x -= 55;
58  else
59  x -= '0';
60  return x;
61 }
62 
63 typedef struct alias {
64  uint8_t key;
65  int len;
66  const char *value;
67 } alias;
68 
69 static const alias aliases[20] = {
70  { .key = 16, .len = 3, .value = "\xFA\x0\x0", },
71  { .key = 17, .len = 6, .value = "\xFA\x0\x0\xFA\x0\x0", },
72  { .key = 18, .len = 9, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
73  { .key = 19, .len = 12, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
74  { .key = 20, .len = 15, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
75  { .key = 21, .len = 18, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
76  { .key = 22, .len = 21, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
77  { .key = 23, .len = 24, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
78  { .key = 24, .len = 27, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", },
79  { .key = 25, .len = 3, .value = "\xFB\x80\x80", },
80  { .key = 26, .len = 3, .value = "\xFC\x80\x80", },
81  { .key = 27, .len = 3, .value = "\xFD\x80\x80", },
82  { .key = 28, .len = 2, .value = "\x96\x69", },
83  { .key = 29, .len = 2, .value = "\x61\x01", },
84  { .key = 30, .len = 3, .value = "\xFC\x80\x80", },
85  { .key = 31, .len = 3, .value = "\xFC\x80\x80", },
86  { .key = 32, .len = 4, .value = "\xE1\x00\x00\x00", },
87  { .key = 33, .len = 0, .value = NULL, },
88  { .key = 34, .len = 0, .value = NULL, },
89  { .key = 35, .len = 1, .value = "\x0", },
90 };
91 
93 {
94  MCCContext *mcc = s->priv_data;
96  AVRational rate = {0};
97  int64_t ts, pos;
98  uint8_t out[4096];
99  char line[4096];
100  FFTextReader tr;
101  int ret = 0;
102 
103  ff_text_init_avio(s, &tr, s->pb);
104 
105  if (!st)
106  return AVERROR(ENOMEM);
109  avpriv_set_pts_info(st, 64, 1, 30);
110 
111  while (!ff_text_eof(&tr)) {
112  int hh, mm, ss, fs, i = 0, j = 0;
113  int start = 12, count = 0;
114  AVPacket *sub;
115  char *lline;
116 
117  ff_subtitles_read_line(&tr, line, sizeof(line));
118  if (!strncmp(line, "File Format=MacCaption_MCC V", 28))
119  continue;
120  if (!strncmp(line, "//", 2))
121  continue;
122  if (!strncmp(line, "Time Code Rate=", 15)) {
123  char *rate_str = line + 15;
124  char *df = NULL;
125  int num = -1, den = -1;
126 
127  if (rate_str[0]) {
128  num = strtol(rate_str, &df, 10);
129  den = 1;
130  if (df && !av_strncasecmp(df, "DF", 2)) {
131  av_reduce(&num, &den, num * 1000LL, 1001, INT_MAX);
132  }
133  }
134 
135  if (num > 0 && den > 0) {
136  rate = av_make_q(num, den);
137  avpriv_set_pts_info(st, 64, rate.den, rate.num);
138  }
139  continue;
140  }
141 
142  if (av_sscanf(line, "%d:%d:%d:%d", &hh, &mm, &ss, &fs) != 4 || rate.den <= 0)
143  continue;
144 
145  ts = av_sat_add64(av_rescale(hh * 3600LL + mm * 60LL + ss, rate.num, rate.den), fs);
146 
147  lline = (char *)&line;
148  lline += 12;
149  pos = ff_text_pos(&tr);
150 
151  while (lline[i]) {
152  uint8_t v = convert(lline[i]);
153 
154  if (v >= 16 && v <= 35) {
155  int idx = v - 16;
156  if (aliases[idx].len) {
157  if (j >= sizeof(out) - 1 - aliases[idx].len) {
158  j = 0;
159  break;
160  }
161  memcpy(out + j, aliases[idx].value, aliases[idx].len);
162  j += aliases[idx].len;
163  }
164  } else {
165  uint8_t vv;
166 
167  if (i + 13 >= sizeof(line) - 1)
168  break;
169  vv = convert(lline[i + 1]);
170  if (j >= sizeof(out) - 1) {
171  j = 0;
172  break;
173  }
174  out[j++] = vv | (v << 4);
175  i++;
176  }
177 
178  i++;
179  }
180  out[j] = 0;
181 
182  if (out[7] & 0x80)
183  start += 4;
184  count = (out[11] & 0x1f) * 3;
185  if (j < start + count + 1)
186  continue;
187 
188  if (!count)
189  continue;
190  sub = ff_subtitles_queue_insert(&mcc->q, out + start, count, 0);
191  if (!sub)
192  return AVERROR(ENOMEM);
193 
194  sub->pos = pos;
195  sub->pts = ts;
196  sub->duration = 1;
197  }
198 
200 
201  return ret;
202 }
203 
205  .p.name = "mcc",
206  .p.long_name = NULL_IF_CONFIG_SMALL("MacCaption"),
207  .p.extensions = "mcc",
208  .priv_data_size = sizeof(MCCContext),
209  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
213  .read_seek2 = ff_subtitles_read_seek,
215 };
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:566
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
ff_subtitles_read_close
int ff_subtitles_read_close(AVFormatContext *s)
Definition: subtitles.c:345
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
out
FILE * out
Definition: movenc.c:55
df
#define df(A, B)
Definition: vf_xbr.c:91
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int64_t
long long int64_t
Definition: coverity.c:34
convert
static int convert(uint8_t x)
Definition: mccdec.c:52
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
FFTextReader
Definition: subtitles.h:41
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
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:454
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
MCCContext
Definition: mccdec.c:30
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: avformat.c:867
alias::len
int len
Definition: mccdec.c:65
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:202
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
MCCContext::q
FFDemuxSubtitlesQueue q
Definition: mccdec.c:31
ff_subtitles_read_packet
int ff_subtitles_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: subtitles.c:331
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
FF_INFMT_FLAG_INIT_CLEANUP
#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
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
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:771
av_sscanf
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
Definition: avsscanf.c:961
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
alias::key
uint8_t key
Definition: mccdec.c:64
NULL
#define NULL
Definition: coverity.c:32
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:200
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
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:28
av_strncasecmp
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:217
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:94
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
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:212
aliases
static const alias aliases[20]
Definition: mccdec.c:69
FFDemuxSubtitlesQueue
Definition: subtitles.h:103
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
line
Definition: graph2dot.c:48
mcc_probe
static int mcc_probe(const AVProbeData *p)
Definition: mccdec.c:34
bprint.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
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
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
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:337
ff_mcc_demuxer
const FFInputFormat ff_mcc_demuxer
Definition: mccdec.c:204
demux.h
len
int len
Definition: vorbis_enc_data.h:426
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
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
av_sat_add64
#define av_sat_add64
Definition: common.h:139
subtitles.h
AVRational::den
int den
Denominator.
Definition: rational.h:60
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.
Definition: subtitles.c:54
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
ff_text_eof
int ff_text_eof(FFTextReader *r)
Return non-zero if EOF was reached.
Definition: subtitles.c:92
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
alias
Definition: mccdec.c:63
avstring.h
alias::value
const char * value
Definition: mccdec.c:66
mcc_read_header
static int mcc_read_header(AVFormatContext *s)
Definition: mccdec.c:92