| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * LRC lyrics file format muxer | ||
| 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 | #include "libavutil/opt.h" | ||
| 36 | |||
| 37 | typedef struct LRCSubtitleContext { | ||
| 38 | const AVClass *class; | ||
| 39 | int precision; ///< precision of the fractional part of the timestamp, 2 for centiseconds | ||
| 40 | } LRCSubtitleContext; | ||
| 41 | |||
| 42 | 3 | static int lrc_write_header(AVFormatContext *s) | |
| 43 | { | ||
| 44 | const AVDictionaryEntry *metadata_item; | ||
| 45 | |||
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | 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 | 3 | avpriv_set_pts_info(s->streams[0], 64, 1, AV_TIME_BASE); | |
| 53 | |||
| 54 | 3 | ff_standardize_creation_time(s); | |
| 55 | 3 | ff_metadata_conv_ctx(s, ff_lrc_metadata_conv, NULL); | |
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | 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 | 3 | av_dict_set(&s->metadata, "ve", NULL, 0); | |
| 64 | } | ||
| 65 | 3 | for(metadata_item = NULL; | |
| 66 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 2 taken 3 times.
|
18 | (metadata_item = av_dict_iterate(s->metadata, metadata_item));) { |
| 67 | char *delim; | ||
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if(!metadata_item->value[0]) { |
| 69 | ✗ | continue; | |
| 70 | } | ||
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | while((delim = strchr(metadata_item->value, '\n'))) { |
| 72 | ✗ | *delim = ' '; | |
| 73 | } | ||
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | while((delim = strchr(metadata_item->value, '\r'))) { |
| 75 | ✗ | *delim = ' '; | |
| 76 | } | ||
| 77 | 15 | avio_printf(s->pb, "[%s:%s]\n", | |
| 78 | 15 | metadata_item->key, metadata_item->value); | |
| 79 | } | ||
| 80 | 3 | avio_w8(s->pb, '\n'); | |
| 81 | 3 | return 0; | |
| 82 | } | ||
| 83 | |||
| 84 | 129 | static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt) | |
| 85 | { | ||
| 86 | 129 | const LRCSubtitleContext *p = s->priv_data; | |
| 87 | |||
| 88 |
1/2✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
|
129 | if(pkt->pts != AV_NOPTS_VALUE) { |
| 89 | 129 | const uint8_t *line = pkt->data; | |
| 90 | 129 | const uint8_t *end = pkt->data + pkt->size; | |
| 91 | |||
| 92 |
4/6✓ Branch 0 taken 123 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 123 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 123 times.
|
129 | while (end > line && (end[-1] == '\n' || end[-1] == '\r')) |
| 93 | ✗ | end--; | |
| 94 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 6 times.
|
129 | if (line != end) { |
| 95 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 123 times.
|
123 | while (line[0] == '\n' || line[0] == '\r') |
| 96 | ✗ | line++; // Skip first empty lines | |
| 97 | } | ||
| 98 | |||
| 99 | 129 | int frac_mult = 1; | |
| 100 |
2/2✓ Branch 0 taken 301 times.
✓ Branch 1 taken 129 times.
|
430 | for (int i = 0; i < p->precision; ++i) |
| 101 | 301 | frac_mult *= 10; | |
| 102 | |||
| 103 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 129 times.
|
258 | while(line) { |
| 104 | 129 | const uint8_t *next_line = memchr(line, '\n', end - line); | |
| 105 | 129 | size_t size = end - line; | |
| 106 | |||
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
|
129 | if (next_line) { |
| 108 | ✗ | size = next_line - line; | |
| 109 | ✗ | if (next_line > line && next_line[-1] == '\r') | |
| 110 | ✗ | size--; | |
| 111 | ✗ | next_line++; | |
| 112 | } | ||
| 113 |
3/4✓ Branch 0 taken 123 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 123 times.
|
129 | if (size && line[0] == '[') { |
| 114 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 115 | "Subtitle starts with '[', may cause problems with LRC format.\n"); | ||
| 116 | } | ||
| 117 | |||
| 118 | /* Offset feature of LRC can easily make pts negative, | ||
| 119 | * we just output it directly and let the player drop it. */ | ||
| 120 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 126 times.
|
129 | uint64_t abs_pts = FFABS64U(pkt->pts); |
| 121 | 129 | uint64_t minutes = abs_pts / (60 * AV_TIME_BASE); | |
| 122 | 129 | uint64_t seconds = (abs_pts / AV_TIME_BASE) % 60; | |
| 123 | 129 | uint64_t fraction = abs_pts % AV_TIME_BASE; | |
| 124 | 129 | uint64_t rescaled = av_rescale_q(fraction, AV_TIME_BASE_Q, (AVRational){1, frac_mult}); | |
| 125 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 126 times.
|
129 | avio_write(s->pb, "[-", 1 + (pkt->pts < 0)); |
| 126 | 129 | avio_printf(s->pb, "%02"PRIu64":%02"PRIu64".%0*"PRIu64"]", | |
| 127 | 129 | minutes, seconds, p->precision, rescaled); | |
| 128 | |||
| 129 | 129 | avio_write(s->pb, line, size); | |
| 130 | 129 | avio_w8(s->pb, '\n'); | |
| 131 | 129 | line = next_line; | |
| 132 | } | ||
| 133 | } | ||
| 134 | 129 | return 0; | |
| 135 | } | ||
| 136 | |||
| 137 | #define OFFSET(x) offsetof(LRCSubtitleContext, x) | ||
| 138 | #define SE AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
| 139 | static const AVOption options[] = { | ||
| 140 | {"precision", "precision of the fractional part of the timestamp, 2 for centiseconds", OFFSET(precision), AV_OPT_TYPE_INT, {.i64 = 2}, 1, 6, SE}, | ||
| 141 | { NULL }, | ||
| 142 | }; | ||
| 143 | |||
| 144 | static const AVClass lrcenc_class = { | ||
| 145 | .class_name = "lrc", | ||
| 146 | .item_name = av_default_item_name, | ||
| 147 | .option = options, | ||
| 148 | .version = LIBAVUTIL_VERSION_INT, | ||
| 149 | }; | ||
| 150 | |||
| 151 | const FFOutputFormat ff_lrc_muxer = { | ||
| 152 | .p.name = "lrc", | ||
| 153 | .p.long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"), | ||
| 154 | .p.extensions = "lrc", | ||
| 155 | .p.flags = AVFMT_VARIABLE_FPS | AVFMT_GLOBALHEADER | | ||
| 156 | AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT, | ||
| 157 | .p.video_codec = AV_CODEC_ID_NONE, | ||
| 158 | .p.audio_codec = AV_CODEC_ID_NONE, | ||
| 159 | .p.subtitle_codec = AV_CODEC_ID_SUBRIP, | ||
| 160 | .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH, | ||
| 161 | .priv_data_size = sizeof(LRCSubtitleContext), | ||
| 162 | .write_header = lrc_write_header, | ||
| 163 | .write_packet = lrc_write_packet, | ||
| 164 | .p.priv_class = &lrcenc_class, | ||
| 165 | }; | ||
| 166 |