2014-07-09 09:24:05 +03:00
|
|
|
/*
|
|
|
|
* LRC lyrics file format decoder
|
|
|
|
* Copyright (c) 2014 StarBrilliant <m13253@hotmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "lrc.h"
|
|
|
|
#include "metadata.h"
|
2022-05-06 16:40:53 +02:00
|
|
|
#include "mux.h"
|
2014-07-09 09:24:05 +03:00
|
|
|
#include "version.h"
|
|
|
|
#include "libavutil/dict.h"
|
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/macros.h"
|
|
|
|
|
|
|
|
static int lrc_write_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
const AVDictionaryEntry *metadata_item;
|
|
|
|
|
|
|
|
if(s->nb_streams != 1 ||
|
2016-04-10 21:58:15 +02:00
|
|
|
s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
|
2014-07-09 09:24:05 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"LRC supports only a single subtitle stream.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if(s->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP &&
|
|
|
|
s->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT) {
|
2014-07-09 09:24:05 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported subtitle codec: %s\n",
|
2016-04-10 21:58:15 +02:00
|
|
|
avcodec_get_name(s->streams[0]->codecpar->codec_id));
|
2014-07-09 09:24:05 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
avpriv_set_pts_info(s->streams[0], 64, 1, 100);
|
|
|
|
|
2016-02-28 04:11:34 +02:00
|
|
|
ff_standardize_creation_time(s);
|
2014-07-09 09:24:05 +03:00
|
|
|
ff_metadata_conv_ctx(s, ff_lrc_metadata_conv, NULL);
|
|
|
|
if(!(s->flags & AVFMT_FLAG_BITEXACT)) { // avoid breaking regression tests
|
|
|
|
/* LRC provides a metadata slot for specifying encoder version
|
|
|
|
* in addition to encoder name. We will store LIBAVFORMAT_VERSION
|
|
|
|
* to it.
|
|
|
|
*/
|
|
|
|
av_dict_set(&s->metadata, "ve", AV_STRINGIFY(LIBAVFORMAT_VERSION), 0);
|
|
|
|
} else {
|
|
|
|
av_dict_set(&s->metadata, "ve", NULL, 0);
|
|
|
|
}
|
|
|
|
for(metadata_item = NULL;
|
|
|
|
(metadata_item = av_dict_get(s->metadata, "", metadata_item,
|
2014-08-15 22:31:59 +03:00
|
|
|
AV_DICT_IGNORE_SUFFIX));) {
|
2014-07-09 09:24:05 +03:00
|
|
|
char *delim;
|
|
|
|
if(!metadata_item->value[0]) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-15 22:31:59 +03:00
|
|
|
while((delim = strchr(metadata_item->value, '\n'))) {
|
2014-07-09 09:24:05 +03:00
|
|
|
*delim = ' ';
|
|
|
|
}
|
2014-08-15 22:31:59 +03:00
|
|
|
while((delim = strchr(metadata_item->value, '\r'))) {
|
2014-07-09 09:24:05 +03:00
|
|
|
*delim = ' ';
|
|
|
|
}
|
|
|
|
avio_printf(s->pb, "[%s:%s]\n",
|
|
|
|
metadata_item->key, metadata_item->value);
|
|
|
|
}
|
2021-10-01 16:30:17 +02:00
|
|
|
avio_w8(s->pb, '\n');
|
2014-07-09 09:24:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
if(pkt->pts != AV_NOPTS_VALUE) {
|
2021-10-01 22:11:17 +02:00
|
|
|
const uint8_t *line = pkt->data;
|
|
|
|
const uint8_t *end = pkt->data + pkt->size;
|
2014-07-09 09:24:05 +03:00
|
|
|
|
2021-10-01 22:11:17 +02:00
|
|
|
while (end > line && (end[-1] == '\n' || end[-1] == '\r'))
|
|
|
|
end--;
|
|
|
|
if (line != end) {
|
|
|
|
while (line[0] == '\n' || line[0] == '\r')
|
|
|
|
line++; // Skip first empty lines
|
2014-07-09 09:24:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while(line) {
|
2021-10-01 22:11:17 +02:00
|
|
|
const uint8_t *next_line = memchr(line, '\n', end - line);
|
|
|
|
size_t size = end - line;
|
|
|
|
|
|
|
|
if (next_line) {
|
|
|
|
size = next_line - line;
|
|
|
|
if (next_line > line && next_line[-1] == '\r')
|
|
|
|
size--;
|
|
|
|
next_line++;
|
2014-07-09 09:24:05 +03:00
|
|
|
}
|
2022-07-23 18:55:08 +02:00
|
|
|
if (size && line[0] == '[') {
|
2014-07-09 09:24:05 +03:00
|
|
|
av_log(s, AV_LOG_WARNING,
|
|
|
|
"Subtitle starts with '[', may cause problems with LRC format.\n");
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:41:49 +02:00
|
|
|
/* Offset feature of LRC can easily make pts negative,
|
|
|
|
* we just output it directly and let the player drop it. */
|
|
|
|
avio_write(s->pb, "[-", 1 + (pkt->pts < 0));
|
|
|
|
avio_printf(s->pb, "%02"PRIu64":%02"PRIu64".%02"PRIu64"]",
|
|
|
|
(FFABS64U(pkt->pts) / 6000),
|
|
|
|
((FFABS64U(pkt->pts) / 100) % 60),
|
|
|
|
(FFABS64U(pkt->pts) % 100));
|
|
|
|
|
2021-10-01 22:11:17 +02:00
|
|
|
avio_write(s->pb, line, size);
|
|
|
|
avio_w8(s->pb, '\n');
|
|
|
|
line = next_line;
|
2014-07-09 09:24:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVOutputFormat ff_lrc_muxer = {
|
2014-07-09 09:24:05 +03:00
|
|
|
.name = "lrc",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"),
|
|
|
|
.extensions = "lrc",
|
|
|
|
.priv_data_size = 0,
|
|
|
|
.write_header = lrc_write_header,
|
|
|
|
.write_packet = lrc_write_packet,
|
|
|
|
.flags = AVFMT_VARIABLE_FPS | AVFMT_GLOBALHEADER |
|
|
|
|
AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT,
|
|
|
|
.subtitle_codec = AV_CODEC_ID_SUBRIP
|
|
|
|
};
|