FFmpeg
lrcenc.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 "mux.h"
31 #include "version.h"
32 #include "libavutil/dict.h"
33 #include "libavutil/log.h"
34 #include "libavutil/macros.h"
35 
37 {
38  const AVDictionaryEntry *metadata_item;
39 
40  if(s->nb_streams != 1 ||
41  s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
43  "LRC supports only a single subtitle stream.\n");
44  return AVERROR(EINVAL);
45  }
46  if(s->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP &&
47  s->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT) {
48  av_log(s, AV_LOG_ERROR, "Unsupported subtitle codec: %s\n",
49  avcodec_get_name(s->streams[0]->codecpar->codec_id));
50  return AVERROR(EINVAL);
51  }
52  avpriv_set_pts_info(s->streams[0], 64, 1, 100);
53 
56  if(!(s->flags & AVFMT_FLAG_BITEXACT)) { // avoid breaking regression tests
57  /* LRC provides a metadata slot for specifying encoder version
58  * in addition to encoder name. We will store LIBAVFORMAT_VERSION
59  * to it.
60  */
61  av_dict_set(&s->metadata, "ve", AV_STRINGIFY(LIBAVFORMAT_VERSION), 0);
62  } else {
63  av_dict_set(&s->metadata, "ve", NULL, 0);
64  }
65  for(metadata_item = NULL;
66  (metadata_item = av_dict_get(s->metadata, "", metadata_item,
68  char *delim;
69  if(!metadata_item->value[0]) {
70  continue;
71  }
72  while((delim = strchr(metadata_item->value, '\n'))) {
73  *delim = ' ';
74  }
75  while((delim = strchr(metadata_item->value, '\r'))) {
76  *delim = ' ';
77  }
78  avio_printf(s->pb, "[%s:%s]\n",
79  metadata_item->key, metadata_item->value);
80  }
81  avio_w8(s->pb, '\n');
82  return 0;
83 }
84 
86 {
87  if(pkt->pts != AV_NOPTS_VALUE) {
88  const uint8_t *line = pkt->data;
89  const uint8_t *end = pkt->data + pkt->size;
90 
91  while (end > line && (end[-1] == '\n' || end[-1] == '\r'))
92  end--;
93  if (line != end) {
94  while (line[0] == '\n' || line[0] == '\r')
95  line++; // Skip first empty lines
96  }
97 
98  while(line) {
99  const uint8_t *next_line = memchr(line, '\n', end - line);
100  size_t size = end - line;
101 
102  if (next_line) {
103  size = next_line - line;
104  if (next_line > line && next_line[-1] == '\r')
105  size--;
106  next_line++;
107  }
108  if(line[0] == '[') {
110  "Subtitle starts with '[', may cause problems with LRC format.\n");
111  }
112 
113  /* Offset feature of LRC can easily make pts negative,
114  * we just output it directly and let the player drop it. */
115  avio_write(s->pb, "[-", 1 + (pkt->pts < 0));
116  avio_printf(s->pb, "%02"PRIu64":%02"PRIu64".%02"PRIu64"]",
117  (FFABS64U(pkt->pts) / 6000),
118  ((FFABS64U(pkt->pts) / 100) % 60),
119  (FFABS64U(pkt->pts) % 100));
120 
121  avio_write(s->pb, line, size);
122  avio_w8(s->pb, '\n');
123  line = next_line;
124  }
125  }
126  return 0;
127 }
128 
130  .name = "lrc",
131  .long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"),
132  .extensions = "lrc",
133  .priv_data_size = 0,
134  .write_header = lrc_write_header,
135  .write_packet = lrc_write_packet,
138  .subtitle_codec = AV_CODEC_ID_SUBRIP
139 };
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVOutputFormat::name
const char * name
Definition: avformat.h:510
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
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:484
AVPacket::data
uint8_t * data
Definition: packet.h:374
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:68
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:697
macros.h
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
s
#define s(width, name)
Definition: cbs_vp9.c:256
lrc.h
AVDictionaryEntry::key
char * key
Definition: dict.h:80
lrc_write_packet
static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: lrcenc.c:85
ff_lrc_metadata_conv
const AVMetadataConv ff_lrc_metadata_conv[]
Definition: lrc.c:25
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
internal.h
ff_metadata_conv_ctx
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
NULL
#define NULL
Definition: coverity.c:32
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:210
AVPacket::size
int size
Definition: packet.h:375
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
ff_standardize_creation_time
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: mux_utils.c:169
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
line
Definition: graph2dot.c:48
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:447
log.h
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:480
AVOutputFormat
Definition: avformat.h:509
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AV_CODEC_ID_SUBRIP
@ AV_CODEC_ID_SUBRIP
Definition: codec_id.h:544
AV_STRINGIFY
#define AV_STRINGIFY(s)
Definition: macros.h:66
AVFMT_TS_NEGATIVE
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:494
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:491
version.h
LIBAVFORMAT_VERSION
#define LIBAVFORMAT_VERSION
Definition: version.h:40
AVFMT_FLAG_BITEXACT
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1348
metadata.h
avformat.h
ff_lrc_muxer
const AVOutputFormat ff_lrc_muxer
Definition: lrcenc.c:129
dict.h
AV_CODEC_ID_TEXT
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition: codec_id.h:529
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
AVDictionaryEntry
Definition: dict.h:79
AVPacket
This structure stores compressed data.
Definition: packet.h:351
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:70
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
FFABS64U
#define FFABS64U(a)
Definition: common.h:82
AVDictionaryEntry::value
char * value
Definition: dict.h:81
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
lrc_write_header
static int lrc_write_header(AVFormatContext *s)
Definition: lrcenc.c:36
mux.h