FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/lrcenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 37 52 71.2%
Functions: 2 2 100.0%
Branches: 30 48 62.5%

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
36 1 static int lrc_write_header(AVFormatContext *s)
37 {
38 const AVDictionaryEntry *metadata_item;
39
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(s->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP &&
41 s->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT) {
42 av_log(s, AV_LOG_ERROR, "Unsupported subtitle codec: %s\n",
43 avcodec_get_name(s->streams[0]->codecpar->codec_id));
44 return AVERROR(EINVAL);
45 }
46 1 avpriv_set_pts_info(s->streams[0], 64, 1, 100);
47
48 1 ff_standardize_creation_time(s);
49 1 ff_metadata_conv_ctx(s, ff_lrc_metadata_conv, NULL);
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(!(s->flags & AVFMT_FLAG_BITEXACT)) { // avoid breaking regression tests
51 /* LRC provides a metadata slot for specifying encoder version
52 * in addition to encoder name. We will store LIBAVFORMAT_VERSION
53 * to it.
54 */
55 av_dict_set(&s->metadata, "ve", AV_STRINGIFY(LIBAVFORMAT_VERSION), 0);
56 } else {
57 1 av_dict_set(&s->metadata, "ve", NULL, 0);
58 }
59 1 for(metadata_item = NULL;
60
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 times.
6 (metadata_item = av_dict_iterate(s->metadata, metadata_item));) {
61 char *delim;
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if(!metadata_item->value[0]) {
63 continue;
64 }
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 while((delim = strchr(metadata_item->value, '\n'))) {
66 *delim = ' ';
67 }
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 while((delim = strchr(metadata_item->value, '\r'))) {
69 *delim = ' ';
70 }
71 5 avio_printf(s->pb, "[%s:%s]\n",
72 5 metadata_item->key, metadata_item->value);
73 }
74 1 avio_w8(s->pb, '\n');
75 1 return 0;
76 }
77
78 43 static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt)
79 {
80
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if(pkt->pts != AV_NOPTS_VALUE) {
81 43 const uint8_t *line = pkt->data;
82 43 const uint8_t *end = pkt->data + pkt->size;
83
84
4/6
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 41 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 41 times.
43 while (end > line && (end[-1] == '\n' || end[-1] == '\r'))
85 end--;
86
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 2 times.
43 if (line != end) {
87
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 41 times.
41 while (line[0] == '\n' || line[0] == '\r')
88 line++; // Skip first empty lines
89 }
90
91
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 43 times.
86 while(line) {
92 43 const uint8_t *next_line = memchr(line, '\n', end - line);
93 43 size_t size = end - line;
94
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (next_line) {
96 size = next_line - line;
97 if (next_line > line && next_line[-1] == '\r')
98 size--;
99 next_line++;
100 }
101
3/4
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 41 times.
43 if (size && line[0] == '[') {
102 av_log(s, AV_LOG_WARNING,
103 "Subtitle starts with '[', may cause problems with LRC format.\n");
104 }
105
106 /* Offset feature of LRC can easily make pts negative,
107 * we just output it directly and let the player drop it. */
108
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
43 avio_write(s->pb, "[-", 1 + (pkt->pts < 0));
109 43 avio_printf(s->pb, "%02"PRIu64":%02"PRIu64".%02"PRIu64"]",
110
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
43 (FFABS64U(pkt->pts) / 6000),
111
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
43 ((FFABS64U(pkt->pts) / 100) % 60),
112
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
43 (FFABS64U(pkt->pts) % 100));
113
114 43 avio_write(s->pb, line, size);
115 43 avio_w8(s->pb, '\n');
116 43 line = next_line;
117 }
118 }
119 43 return 0;
120 }
121
122 const FFOutputFormat ff_lrc_muxer = {
123 .p.name = "lrc",
124 .p.long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"),
125 .p.extensions = "lrc",
126 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_GLOBALHEADER |
127 AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT,
128 .p.video_codec = AV_CODEC_ID_NONE,
129 .p.audio_codec = AV_CODEC_ID_NONE,
130 .p.subtitle_codec = AV_CODEC_ID_SUBRIP,
131 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
132 .priv_data_size = 0,
133 .write_header = lrc_write_header,
134 .write_packet = lrc_write_packet,
135 };
136