FFmpeg
lrcdec.c
Go to the documentation of this file.
1 /*
2  * LRC lyrics file format demuxer
3  * Copyright (c) 2014 StarBrilliant <m13253@hotmail.com>
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 <inttypes.h>
23 #include <stdint.h>
24 #include <string.h>
25 
26 #include "avformat.h"
27 #include "demux.h"
28 #include "internal.h"
29 #include "lrc.h"
30 #include "metadata.h"
31 #include "subtitles.h"
32 #include "libavutil/bprint.h"
33 #include "libavutil/dict.h"
34 
35 typedef struct LRCContext {
37  int64_t ts_offset; // offset metadata item
38 } LRCContext;
39 
40 static int64_t find_header(const char *p)
41 {
42  int64_t offset = 0;
43  while(p[offset] == ' ' || p[offset] == '\t') {
44  offset++;
45  }
46  if(p[offset] == '[' && p[offset + 1] >= 'a' && p[offset + 1] <= 'z') {
47  return offset;
48  } else {
49  return -1;
50  }
51 }
52 
53 static int64_t count_ts(const char *p)
54 {
55  int64_t offset = 0;
56  int in_brackets = 0;
57 
58  for(;;) {
59  if(p[offset] == ' ' || p[offset] == '\t') {
60  offset++;
61  } else if(p[offset] == '[') {
62  offset++;
63  in_brackets++;
64  } else if (p[offset] == ']' && in_brackets) {
65  offset++;
66  in_brackets--;
67  } else if(in_brackets &&
68  (p[offset] == ':' || p[offset] == '.' || p[offset] == '-' ||
69  (p[offset] >= '0' && p[offset] <= '9'))) {
70  offset++;
71  } else {
72  break;
73  }
74  }
75  return offset;
76 }
77 
78 static int64_t read_ts(const char *p, int64_t *start)
79 {
80  int64_t offset = 0;
81  uint64_t mm, ss, cs;
82 
83  while(p[offset] == ' ' || p[offset] == '\t') {
84  offset++;
85  }
86  if(p[offset] != '[') {
87  return 0;
88  }
89  if(sscanf(p, "[-%"SCNu64":%"SCNu64".%"SCNu64"]", &mm, &ss, &cs) == 3) {
90  /* Just in case negative pts, players may drop it but we won't. */
91  *start = -(int64_t) (mm*60000 + ss*1000 + cs*10);
92  } else if(sscanf(p, "[%"SCNu64":%"SCNu64".%"SCNu64"]", &mm, &ss, &cs) == 3) {
93  *start = mm*60000 + ss*1000 + cs*10;
94  } else {
95  return 0;
96  }
97  do {
98  offset++;
99  } while(p[offset] && p[offset-1] != ']');
100  return offset;
101 }
102 
103 static int64_t read_line(AVBPrint *buf, AVIOContext *pb)
104 {
105  int64_t pos = avio_tell(pb);
106 
107  av_bprint_clear(buf);
108  while(!avio_feof(pb)) {
109  int c = avio_r8(pb);
110  if(c != '\r') {
111  av_bprint_chars(buf, c, 1);
112  }
113  if(c == '\n') {
114  break;
115  }
116  }
117  return pos;
118 }
119 
120 static int lrc_probe(const AVProbeData *p)
121 {
122  int64_t offset = 0;
123  int64_t mm;
124  uint64_t ss, cs;
125  const AVMetadataConv *metadata_item;
126 
127  if(!memcmp(p->buf, "\xef\xbb\xbf", 3)) { // Skip UTF-8 BOM header
128  offset += 3;
129  }
130  while(p->buf[offset] == '\n' || p->buf[offset] == '\r') {
131  offset++;
132  }
133  if(p->buf[offset] != '[') {
134  return 0;
135  }
136  offset++;
137  // Common metadata item but not exist in ff_lrc_metadata_conv
138  if(!memcmp(p->buf + offset, "offset:", 7)) {
139  return 40;
140  }
141  if(sscanf(p->buf + offset, "%"SCNd64":%"SCNu64".%"SCNu64"]",
142  &mm, &ss, &cs) == 3) {
143  return 50;
144  }
145  // Metadata items exist in ff_lrc_metadata_conv
146  for(metadata_item = ff_lrc_metadata_conv;
147  metadata_item->native; metadata_item++) {
148  size_t metadata_item_len = strlen(metadata_item->native);
149  if(p->buf[offset + metadata_item_len] == ':' &&
150  !memcmp(p->buf + offset, metadata_item->native, metadata_item_len)) {
151  return 40;
152  }
153  }
154  return 5; // Give it 5 scores since it starts with a bracket
155 }
156 
158 {
159  LRCContext *lrc = s->priv_data;
160  AVBPrint line;
161  AVStream *st;
162 
163  st = avformat_new_stream(s, NULL);
164  if(!st) {
165  return AVERROR(ENOMEM);
166  }
167  avpriv_set_pts_info(st, 64, 1, 1000);
168  lrc->ts_offset = 0;
172 
173  while(!avio_feof(s->pb)) {
174  int64_t header_offset, pos = read_line(&line, s->pb);
175 
177  goto err_nomem_out;
178  header_offset = find_header(line.str);
179  if(header_offset >= 0) {
180  char *comma_offset = strchr(line.str, ':');
181  if(comma_offset) {
182  char *right_bracket_offset = strchr(line.str, ']');
183  if(!right_bracket_offset) {
184  continue;
185  }
186 
187  *right_bracket_offset = *comma_offset = '\0';
188  if(strcmp(line.str + 1, "offset") ||
189  sscanf(comma_offset + 1, "%"SCNd64, &lrc->ts_offset) != 1) {
190  av_dict_set(&s->metadata, line.str + 1, comma_offset + 1, 0);
191  }
192  lrc->ts_offset = av_clip64(lrc->ts_offset, INT64_MIN/4, INT64_MAX/4);
193 
194  *comma_offset = ':';
195  *right_bracket_offset = ']';
196  }
197 
198  } else {
199  AVPacket *sub;
200  int64_t ts_start = AV_NOPTS_VALUE;
201  int64_t ts_stroffset = 0;
202  int64_t ts_stroffset_incr = 0;
203  int64_t ts_strlength = count_ts(line.str);
204 
205  while((ts_stroffset_incr = read_ts(line.str + ts_stroffset,
206  &ts_start)) != 0) {
207  ts_start = av_clip64(ts_start, INT64_MIN/4, INT64_MAX/4);
208  ts_stroffset += ts_stroffset_incr;
209  sub = ff_subtitles_queue_insert(&lrc->q, line.str + ts_strlength,
210  line.len - ts_strlength, 0);
211  if (!sub)
212  goto err_nomem_out;
213  sub->pos = pos;
214  sub->pts = ts_start - lrc->ts_offset;
215  sub->duration = -1;
216  }
217  }
218  }
222  return 0;
223 err_nomem_out:
225  return AVERROR(ENOMEM);
226 }
227 
229  .p.name = "lrc",
230  .p.long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"),
231  .priv_data_size = sizeof (LRCContext),
232  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
237  .read_seek2 = ff_subtitles_read_seek
238 };
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
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
LRCContext::ts_offset
int64_t ts_offset
Definition: lrcdec.c:37
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:218
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
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
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
AVMetadataConv::native
const char * native
Definition: metadata.h:35
AVMetadataConv
Definition: metadata.h:34
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
lrc_read_header
static int lrc_read_header(AVFormatContext *s)
Definition: lrcdec.c:157
LRCContext
Definition: lrcdec.c:35
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:862
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:202
av_clip64
#define av_clip64
Definition: common.h:103
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
s
#define s(width, name)
Definition: cbs_vp9.c:198
lrc.h
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_lrc_metadata_conv
const AVMetadataConv ff_lrc_metadata_conv[]
Definition: lrc.c:25
find_header
static int64_t find_header(const char *p)
Definition: lrcdec.c:40
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
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
NULL
#define NULL
Definition: coverity.c:32
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_lrc_demuxer
const FFInputFormat ff_lrc_demuxer
Definition: lrcdec.c:228
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
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
lrc_probe
static int lrc_probe(const AVProbeData *p)
Definition: lrcdec.c:120
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
count_ts
static int64_t count_ts(const char *p)
Definition: lrcdec.c:53
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:212
LRCContext::q
FFDemuxSubtitlesQueue q
Definition: lrcdec.c:36
FFDemuxSubtitlesQueue
Definition: subtitles.h:103
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
offset
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 offset
Definition: writing_filters.txt:86
line
Definition: graph2dot.c:48
bprint.h
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_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
demux.h
AVStream
Stream structure.
Definition: avformat.h:748
metadata.h
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
dict.h
AV_CODEC_ID_TEXT
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition: codec_id.h:557
subtitles.h
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
read_line
static int64_t read_line(AVBPrint *buf, AVIOContext *pb)
Definition: lrcdec.c:103
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
read_ts
static int64_t read_ts(const char *p, int64_t *start)
Definition: lrcdec.c:78
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
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:37
av_bprint_chars
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:145
line
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:40
ff_metadata_conv_ctx
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346