/src/ffmpeg/libavformat/samidec.c
Line | Count | 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 | | static int sami_probe(const AVProbeData *p) |
39 | 936k | { |
40 | 936k | char buf[6]; |
41 | 936k | FFTextReader tr; |
42 | 936k | ff_text_init_buf(&tr, p->buf, p->buf_size); |
43 | 936k | ff_text_read(&tr, buf, sizeof(buf)); |
44 | | |
45 | 936k | return !strncmp(buf, "<SAMI>", 6) ? AVPROBE_SCORE_MAX : 0; |
46 | 936k | } |
47 | | |
48 | | static int sami_read_header(AVFormatContext *s) |
49 | 2.22k | { |
50 | 2.22k | SAMIContext *sami = s->priv_data; |
51 | 2.22k | AVStream *st = avformat_new_stream(s, NULL); |
52 | 2.22k | AVBPrint buf, hdr_buf; |
53 | 2.22k | char c = 0; |
54 | 2.22k | int res = 0, got_first_sync_point = 0; |
55 | 2.22k | FFTextReader tr; |
56 | 2.22k | ff_text_init_avio(s, &tr, s->pb); |
57 | | |
58 | 2.22k | if (!st) |
59 | 0 | return AVERROR(ENOMEM); |
60 | 2.22k | avpriv_set_pts_info(st, 64, 1, 1000); |
61 | 2.22k | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; |
62 | 2.22k | st->codecpar->codec_id = AV_CODEC_ID_SAMI; |
63 | | |
64 | 2.22k | av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); |
65 | 2.22k | av_bprint_init(&hdr_buf, 0, AV_BPRINT_SIZE_UNLIMITED); |
66 | | |
67 | 3.05M | while (!ff_text_eof(&tr)) { |
68 | 3.05M | AVPacket *sub; |
69 | 3.05M | const int64_t pos = ff_text_pos(&tr) - (c != 0); |
70 | 3.05M | int is_sync, is_body, n = ff_smil_extract_next_text_chunk(&tr, &buf, &c); |
71 | | |
72 | 3.05M | if (n < 0) { |
73 | 0 | res = n; |
74 | 0 | goto end; |
75 | 0 | } |
76 | 3.05M | if (n == 0) |
77 | 410 | break; |
78 | | |
79 | 3.05M | is_body = !av_strncasecmp(buf.str, "</BODY", 6); |
80 | 3.05M | if (is_body) { |
81 | 5 | av_bprint_clear(&buf); |
82 | 5 | break; |
83 | 5 | } |
84 | | |
85 | 3.05M | is_sync = !av_strncasecmp(buf.str, "<SYNC", 5); |
86 | 3.05M | if (is_sync) |
87 | 1.64M | got_first_sync_point = 1; |
88 | | |
89 | 3.05M | if (!got_first_sync_point) { |
90 | 142k | av_bprintf(&hdr_buf, "%s", buf.str); |
91 | 2.90M | } else { |
92 | 2.90M | sub = ff_subtitles_queue_insert_bprint(&sami->q, &buf, !is_sync); |
93 | 2.90M | if (!sub) { |
94 | 0 | res = AVERROR(ENOMEM); |
95 | 0 | av_bprint_finalize(&hdr_buf, NULL); |
96 | 0 | goto end; |
97 | 0 | } |
98 | 2.90M | if (is_sync) { |
99 | 1.64M | const char *p = ff_smil_get_attr_ptr(buf.str, "Start"); |
100 | 1.64M | sub->pos = pos; |
101 | 1.64M | sub->pts = p ? strtol(p, NULL, 10) : 0; |
102 | 1.64M | if (sub->pts <= INT64_MIN/2 || sub->pts >= INT64_MAX/2) { |
103 | 64 | res = AVERROR_PATCHWELCOME; |
104 | 64 | av_bprint_finalize(&hdr_buf, NULL); |
105 | 64 | goto end; |
106 | 64 | } |
107 | | |
108 | 1.64M | sub->duration = -1; |
109 | 1.64M | } |
110 | 2.90M | } |
111 | 3.05M | av_bprint_clear(&buf); |
112 | 3.05M | } |
113 | | |
114 | 2.16k | res = ff_bprint_to_codecpar_extradata(st->codecpar, &hdr_buf); |
115 | 2.16k | if (res < 0) |
116 | 0 | goto end; |
117 | | |
118 | 2.16k | ff_subtitles_queue_finalize(s, &sami->q); |
119 | | |
120 | 2.22k | end: |
121 | | av_bprint_finalize(&buf, NULL); |
122 | 2.22k | return res; |
123 | 2.16k | } |
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 | | }; |