FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/srtenc.c
Date: 2024-03-29 11:55:30
Exec Total Coverage
Lines: 26 36 72.2%
Functions: 2 2 100.0%
Branches: 7 14 50.0%

Line Branch Exec Source
1 /*
2 * SubRip subtitle muxer
3 * Copyright (c) 2012 Nicolas George <nicolas.george@normalesup.org>
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 "avformat.h"
23 #include "internal.h"
24 #include "mux.h"
25 #include "libavutil/log.h"
26 #include "libavutil/intreadwrite.h"
27
28 /* TODO: add options for:
29 - character encoding;
30 - LF / CRLF;
31 - byte order mark.
32 */
33
34 typedef struct SRTContext{
35 unsigned index;
36 } SRTContext;
37
38 7 static int srt_write_header(AVFormatContext *avf)
39 {
40 7 SRTContext *srt = avf->priv_data;
41
42
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT &&
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP) {
44 av_log(avf, AV_LOG_ERROR,
45 "Unsupported subtitles codec: %s\n",
46 avcodec_get_name(avf->streams[0]->codecpar->codec_id));
47 return AVERROR(EINVAL);
48 }
49 7 avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
50 7 srt->index = 1;
51 7 return 0;
52 }
53
54 519 static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
55 {
56 519 SRTContext *srt = avf->priv_data;
57
58 519 int64_t s = pkt->pts, e, d = pkt->duration;
59 size_t size;
60 519 int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
61 const uint8_t *p;
62
63 519 p = av_packet_get_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
64
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
519 if (p && size == 16) {
65 x1 = AV_RL32(p );
66 y1 = AV_RL32(p + 4);
67 x2 = AV_RL32(p + 8);
68 y2 = AV_RL32(p + 12);
69 }
70
71
2/4
✓ Branch 0 taken 519 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 519 times.
519 if (s == AV_NOPTS_VALUE || d < 0) {
72 av_log(avf, AV_LOG_WARNING,
73 "Insufficient timestamps in event number %d.\n", srt->index);
74 return 0;
75 }
76 519 e = s + d;
77 519 avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
78 srt->index,
79 519 (int)(s / 3600000), (int)(s / 60000) % 60,
80 519 (int)(s / 1000) % 60, (int)(s % 1000),
81 519 (int)(e / 3600000), (int)(e / 60000) % 60,
82 519 (int)(e / 1000) % 60, (int)(e % 1000));
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 519 times.
519 if (p)
84 avio_printf(avf->pb, " X1:%03d X2:%03d Y1:%03d Y2:%03d",
85 x1, x2, y1, y2);
86 519 avio_printf(avf->pb, "\n");
87
88 519 avio_write(avf->pb, pkt->data, pkt->size);
89 519 avio_write(avf->pb, "\n\n", 2);
90 519 srt->index++;
91 519 return 0;
92 }
93
94 const FFOutputFormat ff_srt_muxer = {
95 .p.name = "srt",
96 .p.long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
97 .p.mime_type = "application/x-subrip",
98 .p.extensions = "srt",
99 .p.flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
100 .p.video_codec = AV_CODEC_ID_NONE,
101 .p.audio_codec = AV_CODEC_ID_NONE,
102 .p.subtitle_codec = AV_CODEC_ID_SUBRIP,
103 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
104 .priv_data_size = sizeof(SRTContext),
105 .write_header = srt_write_header,
106 .write_packet = srt_write_packet,
107 };
108