Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2012 Clément Bœsch | ||
3 | * | ||
4 | * This file is part of FFmpeg. | ||
5 | * | ||
6 | * FFmpeg is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * FFmpeg is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with FFmpeg; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file | ||
23 | * SAMI subtitle demuxer | ||
24 | * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx | ||
25 | */ | ||
26 | |||
27 | #include "avformat.h" | ||
28 | #include "demux.h" | ||
29 | #include "internal.h" | ||
30 | #include "subtitles.h" | ||
31 | #include "libavutil/avstring.h" | ||
32 | #include "libavutil/bprint.h" | ||
33 | |||
34 | typedef struct { | ||
35 | FFDemuxSubtitlesQueue q; | ||
36 | } SAMIContext; | ||
37 | |||
38 | 7186 | static int sami_probe(const AVProbeData *p) | |
39 | { | ||
40 | char buf[6]; | ||
41 | FFTextReader tr; | ||
42 | 7186 | ff_text_init_buf(&tr, p->buf, p->buf_size); | |
43 | 7186 | ff_text_read(&tr, buf, sizeof(buf)); | |
44 | |||
45 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7184 times.
|
7186 | return !strncmp(buf, "<SAMI>", 6) ? AVPROBE_SCORE_MAX : 0; |
46 | } | ||
47 | |||
48 | 2 | static int sami_read_header(AVFormatContext *s) | |
49 | { | ||
50 | 2 | SAMIContext *sami = s->priv_data; | |
51 | 2 | AVStream *st = avformat_new_stream(s, NULL); | |
52 | AVBPrint buf, hdr_buf; | ||
53 | 2 | char c = 0; | |
54 | 2 | int res = 0, got_first_sync_point = 0; | |
55 | FFTextReader tr; | ||
56 | 2 | ff_text_init_avio(s, &tr, s->pb); | |
57 | |||
58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) |
59 | ✗ | return AVERROR(ENOMEM); | |
60 | 2 | avpriv_set_pts_info(st, 64, 1, 1000); | |
61 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
62 | 2 | st->codecpar->codec_id = AV_CODEC_ID_SAMI; | |
63 | |||
64 | 2 | av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); | |
65 | 2 | av_bprint_init(&hdr_buf, 0, AV_BPRINT_SIZE_UNLIMITED); | |
66 | |||
67 |
1/2✓ Branch 1 taken 1095 times.
✗ Branch 2 not taken.
|
1095 | while (!ff_text_eof(&tr)) { |
68 | AVPacket *sub; | ||
69 | 1095 | const int64_t pos = ff_text_pos(&tr) - (c != 0); | |
70 | 1095 | int is_sync, is_body, n = ff_smil_extract_next_text_chunk(&tr, &buf, &c); | |
71 | |||
72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1095 times.
|
1095 | if (n < 0) { |
73 | ✗ | res = n; | |
74 | ✗ | goto end; | |
75 | } | ||
76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1095 times.
|
1095 | if (n == 0) |
77 | ✗ | break; | |
78 | |||
79 | 1095 | is_body = !av_strncasecmp(buf.str, "</BODY", 6); | |
80 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1093 times.
|
1095 | if (is_body) { |
81 | 2 | av_bprint_clear(&buf); | |
82 | 2 | break; | |
83 | } | ||
84 | |||
85 | 1093 | is_sync = !av_strncasecmp(buf.str, "<SYNC", 5); | |
86 |
2/2✓ Branch 0 taken 167 times.
✓ Branch 1 taken 926 times.
|
1093 | if (is_sync) |
87 | 167 | got_first_sync_point = 1; | |
88 | |||
89 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1054 times.
|
1093 | if (!got_first_sync_point) { |
90 | 39 | av_bprintf(&hdr_buf, "%s", buf.str); | |
91 | } else { | ||
92 | 1054 | sub = ff_subtitles_queue_insert_bprint(&sami->q, &buf, !is_sync); | |
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1054 times.
|
1054 | if (!sub) { |
94 | ✗ | res = AVERROR(ENOMEM); | |
95 | ✗ | av_bprint_finalize(&hdr_buf, NULL); | |
96 | ✗ | goto end; | |
97 | } | ||
98 |
2/2✓ Branch 0 taken 167 times.
✓ Branch 1 taken 887 times.
|
1054 | if (is_sync) { |
99 | 167 | const char *p = ff_smil_get_attr_ptr(buf.str, "Start"); | |
100 | 167 | sub->pos = pos; | |
101 |
1/2✓ Branch 0 taken 167 times.
✗ Branch 1 not taken.
|
167 | sub->pts = p ? strtol(p, NULL, 10) : 0; |
102 |
2/4✓ Branch 0 taken 167 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 167 times.
|
167 | if (sub->pts <= INT64_MIN/2 || sub->pts >= INT64_MAX/2) { |
103 | ✗ | res = AVERROR_PATCHWELCOME; | |
104 | ✗ | av_bprint_finalize(&hdr_buf, NULL); | |
105 | ✗ | goto end; | |
106 | } | ||
107 | |||
108 | 167 | sub->duration = -1; | |
109 | } | ||
110 | } | ||
111 | 1093 | av_bprint_clear(&buf); | |
112 | } | ||
113 | |||
114 | 2 | res = ff_bprint_to_codecpar_extradata(st->codecpar, &hdr_buf); | |
115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (res < 0) |
116 | ✗ | goto end; | |
117 | |||
118 | 2 | ff_subtitles_queue_finalize(s, &sami->q); | |
119 | |||
120 | 2 | end: | |
121 | 2 | av_bprint_finalize(&buf, NULL); | |
122 | 2 | return res; | |
123 | } | ||
124 | |||
125 | const FFInputFormat ff_sami_demuxer = { | ||
126 | .p.name = "sami", | ||
127 | .p.long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle format"), | ||
128 | .p.extensions = "smi,sami", | ||
129 | .priv_data_size = sizeof(SAMIContext), | ||
130 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
131 | .read_probe = sami_probe, | ||
132 | .read_header = sami_read_header, | ||
133 | .read_packet = ff_subtitles_read_packet, | ||
134 | .read_seek2 = ff_subtitles_read_seek, | ||
135 | .read_close = ff_subtitles_read_close, | ||
136 | }; | ||
137 |