FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lrcdec.c
Go to the documentation of this file.
1 /*
2  * LRC lyrics file format decoder
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 "internal.h"
28 #include "lrc.h"
29 #include "metadata.h"
30 #include "subtitles.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/dict.h"
33 
34 typedef struct LRCContext {
36  int64_t ts_offset; // offset metadata item
37 } LRCContext;
38 
39 static int64_t find_header(const char *p)
40 {
41  int64_t offset = 0;
42  while(p[offset] == ' ' || p[offset] == '\t') {
43  offset++;
44  }
45  if(p[offset] == '[' && p[offset + 1] >= 'a' && p[offset + 1] <= 'z') {
46  return offset;
47  } else {
48  return -1;
49  }
50 }
51 
52 static int64_t count_ts(const char *p)
53 {
54  int64_t offset = 0;
55  int in_brackets = 0;
56 
57  for(;;) {
58  if(p[offset] == ' ' || p[offset] == '\t') {
59  offset++;
60  } else if(p[offset] == '[') {
61  offset++;
62  in_brackets++;
63  } else if (p[offset] == ']' && in_brackets) {
64  offset++;
65  in_brackets--;
66  } else if(in_brackets &&
67  (p[offset] == ':' || p[offset] == '.' || p[offset] == '-' ||
68  (p[offset] >= '0' && p[offset] <= '9'))) {
69  offset++;
70  } else {
71  break;
72  }
73  }
74  return offset;
75 }
76 
77 static int64_t read_ts(const char *p, int64_t *start)
78 {
79  int64_t offset = 0;
80  uint64_t mm, ss, cs;
81 
82  while(p[offset] == ' ' || p[offset] == '\t') {
83  offset++;
84  }
85  if(p[offset] != '[') {
86  return 0;
87  }
88  if(sscanf(p, "[-%"SCNu64":%"SCNu64".%"SCNu64"]", &mm, &ss, &cs) == 3) {
89  /* Just in case negative pts, players may drop it but we won't. */
90  *start = -(int64_t) (mm*60000 + ss*1000 + cs*10);
91  } else if(sscanf(p, "[%"SCNu64":%"SCNu64".%"SCNu64"]", &mm, &ss, &cs) == 3) {
92  *start = mm*60000 + ss*1000 + cs*10;
93  } else {
94  return 0;
95  }
96  do {
97  offset++;
98  } while(p[offset] && p[offset-1] != ']');
99  return offset;
100 }
101 
102 static int64_t read_line(AVBPrint *buf, AVIOContext *pb)
103 {
104  int64_t pos = avio_tell(pb);
105 
106  av_bprint_clear(buf);
107  while(!avio_feof(pb)) {
108  int c = avio_r8(pb);
109  if(c != '\r') {
110  av_bprint_chars(buf, c, 1);
111  }
112  if(c == '\n') {
113  break;
114  }
115  }
116  return pos;
117 }
118 
119 static int lrc_probe(AVProbeData *p)
120 {
121  int64_t offset = 0;
122  int64_t mm;
123  uint64_t ss, cs;
124  const AVMetadataConv *metadata_item;
125 
126  if(!memcmp(p->buf, "\xef\xbb\xbf", 3)) { // Skip UTF-8 BOM header
127  offset += 3;
128  }
129  while(p->buf[offset] == '\n' || p->buf[offset] == '\r') {
130  offset++;
131  }
132  if(p->buf[offset] != '[') {
133  return 0;
134  }
135  offset++;
136  // Common metadata item but not exist in ff_lrc_metadata_conv
137  if(!memcmp(p->buf + offset, "offset:", 7)) {
138  return 40;
139  }
140  if(sscanf(p->buf + offset, "%"SCNd64":%"SCNu64".%"SCNu64"]",
141  &mm, &ss, &cs) == 3) {
142  return 50;
143  }
144  // Metadata items exist in ff_lrc_metadata_conv
145  for(metadata_item = ff_lrc_metadata_conv;
146  metadata_item->native; metadata_item++) {
147  size_t metadata_item_len = strlen(metadata_item->native);
148  if(p->buf[offset + metadata_item_len] == ':' &&
149  !memcmp(p->buf + offset, metadata_item->native, metadata_item_len)) {
150  return 40;
151  }
152  }
153  return 5; // Give it 5 scores since it starts with a bracket
154 }
155 
157 {
158  LRCContext *lrc = s->priv_data;
159  AVBPrint line;
160  AVStream *st;
161 
162  st = avformat_new_stream(s, NULL);
163  if(!st) {
164  return AVERROR(ENOMEM);
165  }
166  avpriv_set_pts_info(st, 64, 1, 1000);
167  lrc->ts_offset = 0;
171 
172  while(!avio_feof(s->pb)) {
173  int64_t pos = read_line(&line, s->pb);
174  int64_t header_offset = find_header(line.str);
175  if(header_offset >= 0) {
176  char *comma_offset = strchr(line.str, ':');
177  if(comma_offset) {
178  char *right_bracket_offset = strchr(line.str, ']');
179  if(!right_bracket_offset) {
180  continue;
181  }
182 
183  *right_bracket_offset = *comma_offset = '\0';
184  if(strcmp(line.str + 1, "offset") ||
185  sscanf(comma_offset + 1, "%"SCNd64, &lrc->ts_offset) != 1) {
186  av_dict_set(&s->metadata, line.str + 1, comma_offset + 1, 0);
187  }
188  *comma_offset = ':';
189  *right_bracket_offset = ']';
190  }
191 
192  } else {
193  AVPacket *sub;
194  int64_t ts_start = AV_NOPTS_VALUE;
195  int64_t ts_stroffset = 0;
196  int64_t ts_stroffset_incr = 0;
197  int64_t ts_strlength = count_ts(line.str);
198 
199  while((ts_stroffset_incr = read_ts(line.str + ts_stroffset,
200  &ts_start)) != 0) {
201  ts_stroffset += ts_stroffset_incr;
202  sub = ff_subtitles_queue_insert(&lrc->q, line.str + ts_strlength,
203  line.len - ts_strlength, 0);
204  if(!sub) {
205  return AVERROR(ENOMEM);
206  }
207  sub->pos = pos;
208  sub->pts = ts_start - lrc->ts_offset;
209  sub->duration = -1;
210  }
211  }
212  }
213  ff_subtitles_queue_finalize(s, &lrc->q);
215  av_bprint_finalize(&line, NULL);
216  return 0;
217 }
218 
220 {
221  LRCContext *lrc = s->priv_data;
222  return ff_subtitles_queue_read_packet(&lrc->q, pkt);
223 }
224 
225 static int lrc_read_seek(AVFormatContext *s, int stream_index,
226  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
227 {
228  LRCContext *lrc = s->priv_data;
229  return ff_subtitles_queue_seek(&lrc->q, s, stream_index,
230  min_ts, ts, max_ts, flags);
231 }
232 
234 {
235  LRCContext *lrc = s->priv_data;
237  return 0;
238 }
239 
241  .name = "lrc",
242  .long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"),
243  .priv_data_size = sizeof (LRCContext),
248  .read_seek2 = lrc_read_seek
249 };
#define NULL
Definition: coverity.c:32
static int64_t count_ts(const char *p)
Definition: lrcdec.c:52
Bytestream IO Context.
Definition: avio.h:161
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
AVInputFormat ff_lrc_demuxer
Definition: lrcdec.c:240
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1465
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:4882
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition: subtitles.c:297
static int64_t read_ts(const char *p, int64_t *start)
Definition: lrcdec.c:77
static AVPacket pkt
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
Format I/O context.
Definition: avformat.h:1351
internal metadata API header see avformat.h or the public API!
Public dictionary API.
int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
Generic read_packet() callback for subtitles demuxers using this queue system.
Definition: subtitles.c:208
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1463
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
static int lrc_read_close(AVFormatContext *s)
Definition: lrcdec.c:233
#define AV_BPRINT_SIZE_UNLIMITED
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
static int lrc_probe(AVProbeData *p)
Definition: lrcdec.c:119
Definition: graph2dot.c:48
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:639
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
#define s(width, name)
Definition: cbs_vp9.c:257
const AVMetadataConv ff_lrc_metadata_conv[]
Definition: lrc.c:25
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
Stream structure.
Definition: avformat.h:874
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
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:245
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
const char * native
Definition: metadata.h:35
void * buf
Definition: avisynth_c.h:690
static int64_t find_header(const char *p)
Definition: lrcdec.c:39
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:70
static int64_t read_line(AVBPrint *buf, AVIOContext *pb)
Definition: lrcdec.c:102
static int lrc_read_header(AVFormatContext *s)
Definition: lrcdec.c:156
FFDemuxSubtitlesQueue q
Definition: lrcdec.c:35
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
#define flags(name, subs,...)
Definition: cbs_av1.c:596
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
static int lrc_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: lrcdec.c:219
Main libavformat public API header.
raw UTF-8 text
Definition: avcodec.h:653
static double c[64]
void * priv_data
Format private data.
Definition: avformat.h:1379
void INT64 start
Definition: avisynth_c.h:690
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
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
int64_t ts_offset
Definition: lrcdec.c:36
This structure stores compressed data.
Definition: avcodec.h:1422
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:193
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
static int lrc_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: lrcdec.c:225
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140