]> git.sesse.net Git - ffmpeg/blob - libavformat/subtitles.c
avformat/movenc: Use the rate from av_timecode_init_from_string() for tmcd
[ffmpeg] / libavformat / subtitles.c
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 #include "avformat.h"
22 #include "subtitles.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
25
26 AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
27                                     const uint8_t *event, int len, int merge)
28 {
29     AVPacket *subs, *sub;
30
31     if (merge && q->nb_subs > 0) {
32         /* merge with previous event */
33
34         int old_len;
35         sub = &q->subs[q->nb_subs - 1];
36         old_len = sub->size;
37         if (av_grow_packet(sub, len) < 0)
38             return NULL;
39         memcpy(sub->data + old_len, event, len);
40     } else {
41         /* new event */
42
43         if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
44             return NULL;
45         subs = av_fast_realloc(q->subs, &q->allocated_size,
46                                (q->nb_subs + 1) * sizeof(*q->subs));
47         if (!subs)
48             return NULL;
49         q->subs = subs;
50         sub = &subs[q->nb_subs++];
51         if (av_new_packet(sub, len) < 0)
52             return NULL;
53         sub->flags |= AV_PKT_FLAG_KEY;
54         sub->pts = sub->dts = 0;
55         memcpy(sub->data, event, len);
56     }
57     return sub;
58 }
59
60 static int cmp_pkt_sub(const void *a, const void *b)
61 {
62     const AVPacket *s1 = a;
63     const AVPacket *s2 = b;
64     if (s1->pts == s2->pts) {
65         if (s1->pos == s2->pos)
66             return 0;
67         return s1->pos > s2->pos ? 1 : -1;
68     }
69     return s1->pts > s2->pts ? 1 : -1;
70 }
71
72 void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
73 {
74     int i;
75
76     qsort(q->subs, q->nb_subs, sizeof(*q->subs), cmp_pkt_sub);
77     for (i = 0; i < q->nb_subs; i++)
78         if (q->subs[i].duration == -1 && i < q->nb_subs - 1)
79             q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
80 }
81
82 int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
83 {
84     AVPacket *sub = q->subs + q->current_sub_idx;
85
86     if (q->current_sub_idx == q->nb_subs)
87         return AVERROR_EOF;
88     if (av_copy_packet(pkt, sub) < 0) {
89         return AVERROR(ENOMEM);
90     }
91
92     pkt->dts = pkt->pts;
93     q->current_sub_idx++;
94     return 0;
95 }
96
97 int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
98                             int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
99 {
100     if (flags & AVSEEK_FLAG_BYTE) {
101         return AVERROR(ENOSYS);
102     } else if (flags & AVSEEK_FLAG_FRAME) {
103         if (ts < 0 || ts >= q->nb_subs)
104             return AVERROR(ERANGE);
105         q->current_sub_idx = ts;
106     } else {
107         int i, idx = -1;
108         int64_t min_ts_diff = INT64_MAX;
109         int64_t ts_selected;
110         /* TODO: q->subs[] is sorted by pts so we could do a binary search */
111         for (i = 0; i < q->nb_subs; i++) {
112             int64_t pts = q->subs[i].pts;
113             uint64_t ts_diff = FFABS(pts - ts);
114             if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
115                 min_ts_diff = ts_diff;
116                 idx = i;
117             }
118         }
119         if (idx < 0)
120             return AVERROR(ERANGE);
121         /* look back in the latest subtitles for overlapping subtitles */
122         ts_selected = q->subs[idx].pts;
123         for (i = idx - 1; i >= 0; i--) {
124             if (q->subs[i].duration <= 0)
125                 continue;
126             if (q->subs[i].pts > ts_selected - q->subs[i].duration)
127                 idx = i;
128             else
129                 break;
130         }
131         q->current_sub_idx = idx;
132     }
133     return 0;
134 }
135
136 void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
137 {
138     int i;
139
140     for (i = 0; i < q->nb_subs; i++)
141         av_free_packet(&q->subs[i]);
142     av_freep(&q->subs);
143     q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
144 }
145
146 int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c)
147 {
148     int i = 0;
149     char end_chr;
150
151     if (!*c) // cached char?
152         *c = avio_r8(pb);
153     if (!*c)
154         return 0;
155
156     end_chr = *c == '<' ? '>' : '<';
157     do {
158         av_bprint_chars(buf, *c, 1);
159         *c = avio_r8(pb);
160         i++;
161     } while (*c != end_chr && *c);
162     if (end_chr == '>') {
163         av_bprint_chars(buf, '>', 1);
164         *c = 0;
165     }
166     return i;
167 }
168
169 const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
170 {
171     int in_quotes = 0;
172     const int len = strlen(attr);
173
174     while (*s) {
175         while (*s) {
176             if (!in_quotes && av_isspace(*s))
177                 break;
178             in_quotes ^= *s == '"'; // XXX: support escaping?
179             s++;
180         }
181         while (av_isspace(*s))
182             s++;
183         if (!av_strncasecmp(s, attr, len) && s[len] == '=')
184             return s + len + 1 + (s[len + 1] == '"');
185     }
186     return NULL;
187 }
188
189 static inline int is_eol(char c)
190 {
191     return c == '\r' || c == '\n';
192 }
193
194 void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
195 {
196     char eol_buf[5];
197     int n = 0, i = 0, nb_eol = 0;
198
199     av_bprint_clear(buf);
200
201     for (;;) {
202         char c = avio_r8(pb);
203
204         if (!c)
205             break;
206
207         /* ignore all initial line breaks */
208         if (n == 0 && is_eol(c))
209             continue;
210
211         /* line break buffering: we don't want to add the trailing \r\n */
212         if (is_eol(c)) {
213             nb_eol += c == '\n';
214             if (nb_eol == 2)
215                 break;
216             eol_buf[i++] = c;
217             if (i == sizeof(eol_buf) - 1)
218                 break;
219             continue;
220         }
221
222         /* only one line break followed by data: we flush the line breaks
223          * buffer */
224         if (i) {
225             eol_buf[i] = 0;
226             av_bprintf(buf, "%s", eol_buf);
227             i = nb_eol = 0;
228         }
229
230         av_bprint_chars(buf, c, 1);
231         n++;
232     }
233 }