FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "subtitles.h"
31 #include "version.h"
32 #include "libavutil/bprint.h"
33 #include "libavutil/dict.h"
34 #include "libavutil/log.h"
35 #include "libavutil/macros.h"
36 
38 {
39  const AVDictionaryEntry *metadata_item;
40 
41  if(s->nb_streams != 1 ||
44  "LRC supports only a single subtitle stream.\n");
45  return AVERROR(EINVAL);
46  }
49  av_log(s, AV_LOG_ERROR, "Unsupported subtitle codec: %s\n",
51  return AVERROR(EINVAL);
52  }
53  avpriv_set_pts_info(s->streams[0], 64, 1, 100);
54 
57  if(!(s->flags & AVFMT_FLAG_BITEXACT)) { // avoid breaking regression tests
58  /* LRC provides a metadata slot for specifying encoder version
59  * in addition to encoder name. We will store LIBAVFORMAT_VERSION
60  * to it.
61  */
63  } else {
64  av_dict_set(&s->metadata, "ve", NULL, 0);
65  }
66  for(metadata_item = NULL;
67  (metadata_item = av_dict_get(s->metadata, "", metadata_item,
69  char *delim;
70  if(!metadata_item->value[0]) {
71  continue;
72  }
73  while((delim = strchr(metadata_item->value, '\n'))) {
74  *delim = ' ';
75  }
76  while((delim = strchr(metadata_item->value, '\r'))) {
77  *delim = ' ';
78  }
79  avio_printf(s->pb, "[%s:%s]\n",
80  metadata_item->key, metadata_item->value);
81  }
82  avio_printf(s->pb, "\n");
83  return 0;
84 }
85 
87 {
88  if(pkt->pts != AV_NOPTS_VALUE) {
89  char *data = av_malloc(pkt->size + 1);
90  char *line;
91  char *delim;
92 
93  if(!data) {
94  return AVERROR(ENOMEM);
95  }
96  memcpy(data, pkt->data, pkt->size);
97  data[pkt->size] = '\0';
98 
99  for(delim = data + pkt->size - 1;
100  delim >= data && (delim[0] == '\n' || delim[0] == '\r'); delim--) {
101  delim[0] = '\0'; // Strip last empty lines
102  }
103  line = data;
104  while(line[0] == '\n' || line[0] == '\r') {
105  line++; // Skip first empty lines
106  }
107 
108  while(line) {
109  delim = strchr(line, '\n');
110  if(delim) {
111  if(delim > line && delim[-1] == '\r') {
112  delim[-1] = '\0';
113  }
114  delim[0] = '\0';
115  delim++;
116  }
117  if(line[0] == '[') {
119  "Subtitle starts with '[', may cause problems with LRC format.\n");
120  }
121 
122  if(pkt->pts >= 0) {
123  avio_printf(s->pb, "[%02"PRId64":%02"PRId64".%02"PRId64"]",
124  (pkt->pts / 6000),
125  ((pkt->pts / 100) % 60),
126  (pkt->pts % 100));
127  } else {
128  /* Offset feature of LRC can easily make pts negative,
129  * we just output it directly and let the player drop it. */
130  avio_printf(s->pb, "[-%02"PRId64":%02"PRId64".%02"PRId64"]",
131  (-pkt->pts) / 6000,
132  ((-pkt->pts) / 100) % 60,
133  (-pkt->pts) % 100);
134  }
135  avio_printf(s->pb, "%s\n", line);
136  line = delim;
137  }
138  av_free(data);
139  }
140  return 0;
141 }
142 
144  .name = "lrc",
145  .long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"),
146  .extensions = "lrc",
147  .priv_data_size = 0,
148  .write_header = lrc_write_header,
149  .write_packet = lrc_write_packet,
152  .subtitle_codec = AV_CODEC_ID_SUBRIP
153 };
#define NULL
Definition: coverity.c:32
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
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
int size
Definition: avcodec.h:1446
static AVPacket pkt
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:479
Format I/O context.
Definition: avformat.h:1351
internal metadata API header see avformat.h or the public API!
Public dictionary API.
#define av_malloc(s)
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
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
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1482
uint8_t * data
Definition: avcodec.h:1445
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1499
#define av_log(a,...)
Utility Preprocessor macros.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
#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
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
Definition: graph2dot.c:48
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: utils.c:5699
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:468
const char * name
Definition: avformat.h:507
#define s(width, name)
Definition: cbs_vp9.c:257
const AVMetadataConv ff_lrc_metadata_conv[]
Definition: lrc.c:25
Libavformat version macros.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:1135
AVOutputFormat ff_lrc_muxer
Definition: lrcenc.c:143
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: lrcenc.c:86
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
#define AV_STRINGIFY(s)
Definition: macros.h:36
static int lrc_write_header(AVFormatContext *s)
Definition: lrcenc.c:37
#define LIBAVFORMAT_VERSION
Definition: version.h:41
Main libavformat public API header.
raw UTF-8 text
Definition: avcodec.h:653
char * key
Definition: dict.h:86
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:472
#define av_free(p)
char * value
Definition: dict.h:87
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
#define AVFMT_TS_NEGATIVE
Format allows muxing negative timestamps.
Definition: avformat.h:484
This structure stores compressed data.
Definition: avcodec.h:1422
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
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2