aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/lrcenc.c
blob: bf295dfade0b592370fad3f7e50cd34419382db6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * LRC lyrics file format muxer
 * 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"
#include "mux.h"
#include "version.h"
#include "libavutil/dict.h"
#include "libavutil/log.h"
#include "libavutil/macros.h"
#include "libavutil/opt.h"

typedef struct LRCSubtitleContext {
    const AVClass *class;
    int precision; ///< precision of the fractional part of the timestamp, 2 for centiseconds
} LRCSubtitleContext;

static int lrc_write_header(AVFormatContext *s)
{
    const AVDictionaryEntry *metadata_item;

    if(s->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP &&
       s->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT) {
        av_log(s, AV_LOG_ERROR, "Unsupported subtitle codec: %s\n",
               avcodec_get_name(s->streams[0]->codecpar->codec_id));
        return AVERROR(EINVAL);
    }
    avpriv_set_pts_info(s->streams[0], 64, 1, AV_TIME_BASE);

    ff_standardize_creation_time(s);
    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_iterate(s->metadata, metadata_item));) {
        char *delim;
        if(!metadata_item->value[0]) {
            continue;
        }
        while((delim = strchr(metadata_item->value, '\n'))) {
            *delim = ' ';
        }
        while((delim = strchr(metadata_item->value, '\r'))) {
            *delim = ' ';
        }
        avio_printf(s->pb, "[%s:%s]\n",
                    metadata_item->key, metadata_item->value);
    }
    avio_w8(s->pb, '\n');
    return 0;
}

static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt)
{
    const LRCSubtitleContext *p = s->priv_data;

    if(pkt->pts != AV_NOPTS_VALUE) {
        const uint8_t *line = pkt->data;
        const uint8_t *end  = pkt->data + pkt->size;

        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
        }

        int frac_mult = 1;
        for (int i = 0; i < p->precision; ++i)
            frac_mult *= 10;

        while(line) {
            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++;
            }
            if (size && line[0] == '[') {
                av_log(s, AV_LOG_WARNING,
                       "Subtitle starts with '[', may cause problems with LRC format.\n");
            }

            /* Offset feature of LRC can easily make pts negative,
             * we just output it directly and let the player drop it. */
            uint64_t abs_pts = FFABS64U(pkt->pts);
            uint64_t minutes = abs_pts / (60 * AV_TIME_BASE);
            uint64_t seconds = (abs_pts / AV_TIME_BASE) % 60;
            uint64_t fraction = abs_pts % AV_TIME_BASE;
            uint64_t rescaled = av_rescale_q(fraction, AV_TIME_BASE_Q, (AVRational){1, frac_mult});
            avio_write(s->pb, "[-", 1 + (pkt->pts < 0));
            avio_printf(s->pb, "%02"PRIu64":%02"PRIu64".%0*"PRIu64"]",
                        minutes, seconds, p->precision, rescaled);

            avio_write(s->pb, line, size);
            avio_w8(s->pb, '\n');
            line = next_line;
        }
    }
    return 0;
}

#define OFFSET(x) offsetof(LRCSubtitleContext, x)
#define SE AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
    {"precision", "precision of the fractional part of the timestamp, 2 for centiseconds", OFFSET(precision), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 6, SE},
    { NULL },
};

static const AVClass lrcenc_class = {
    .class_name = "lrc",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

const FFOutputFormat ff_lrc_muxer = {
    .p.name           = "lrc",
    .p.long_name      = NULL_IF_CONFIG_SMALL("LRC lyrics"),
    .p.extensions     = "lrc",
    .p.flags          = AVFMT_VARIABLE_FPS | AVFMT_GLOBALHEADER |
                        AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT,
    .p.video_codec    = AV_CODEC_ID_NONE,
    .p.audio_codec    = AV_CODEC_ID_NONE,
    .p.subtitle_codec = AV_CODEC_ID_SUBRIP,
    .flags_internal   = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
    .priv_data_size = sizeof(LRCSubtitleContext),
    .write_header   = lrc_write_header,
    .write_packet   = lrc_write_packet,
    .p.priv_class   = &lrcenc_class,
};