| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2012-2013 Clément Bœsch <u pkh me> | ||
| 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 "avio_internal.h" | ||
| 24 | #include "libavutil/avassert.h" | ||
| 25 | #include "libavutil/avstring.h" | ||
| 26 | #include "libavutil/mem.h" | ||
| 27 | |||
| 28 | 44869 | void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb) | |
| 29 | { | ||
| 30 | int i; | ||
| 31 | 44869 | r->pb = pb; | |
| 32 | 44869 | r->buf_pos = r->buf_len = 0; | |
| 33 | 44869 | r->type = FF_UTF_8; | |
| 34 |
2/2✓ Branch 0 taken 89738 times.
✓ Branch 1 taken 44869 times.
|
134607 | for (i = 0; i < 2; i++) |
| 35 | 89738 | r->buf[r->buf_len++] = avio_r8(r->pb); | |
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44869 times.
|
44869 | if (strncmp("\xFF\xFE", r->buf, 2) == 0) { |
| 37 | ✗ | r->type = FF_UTF16LE; | |
| 38 | ✗ | r->buf_pos += 2; | |
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44869 times.
|
44869 | } else if (strncmp("\xFE\xFF", r->buf, 2) == 0) { |
| 40 | ✗ | r->type = FF_UTF16BE; | |
| 41 | ✗ | r->buf_pos += 2; | |
| 42 | } else { | ||
| 43 | 44869 | r->buf[r->buf_len++] = avio_r8(r->pb); | |
| 44 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 44805 times.
|
44869 | if (strncmp("\xEF\xBB\xBF", r->buf, 3) == 0) { |
| 45 | // UTF8 | ||
| 46 | 64 | r->buf_pos += 3; | |
| 47 | } | ||
| 48 | } | ||
| 49 |
4/6✓ Branch 0 taken 25 times.
✓ Branch 1 taken 44844 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 25 times.
|
44869 | if (s && (r->type == FF_UTF16LE || r->type == FF_UTF16BE)) |
| 50 | ✗ | av_log(s, AV_LOG_INFO, | |
| 51 | "UTF16 is automatically converted to UTF8, do not specify a character encoding\n"); | ||
| 52 | 44869 | } | |
| 53 | |||
| 54 | 44844 | void ff_text_init_buf(FFTextReader *r, const void *buf, size_t size) | |
| 55 | { | ||
| 56 | 44844 | ffio_init_read_context(&r->buf_pb, buf, size); | |
| 57 | 44844 | ff_text_init_avio(NULL, r, &r->buf_pb.pub); | |
| 58 | 44844 | } | |
| 59 | |||
| 60 | 6365 | int64_t ff_text_pos(FFTextReader *r) | |
| 61 | { | ||
| 62 | 6365 | return avio_tell(r->pb) - r->buf_len + r->buf_pos; | |
| 63 | } | ||
| 64 | |||
| 65 | 839855 | int ff_text_r8(FFTextReader *r) | |
| 66 | { | ||
| 67 | uint32_t val; | ||
| 68 | uint8_t tmp; | ||
| 69 |
2/2✓ Branch 0 taken 138524 times.
✓ Branch 1 taken 701331 times.
|
839855 | if (r->buf_pos < r->buf_len) |
| 70 | 138524 | return r->buf[r->buf_pos++]; | |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 701331 times.
|
701331 | if (r->type == FF_UTF16LE) { |
| 72 | ✗ | GET_UTF16(val, avio_rl16(r->pb), return 0;) | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 701331 times.
|
701331 | } else if (r->type == FF_UTF16BE) { |
| 74 | ✗ | GET_UTF16(val, avio_rb16(r->pb), return 0;) | |
| 75 | } else { | ||
| 76 | 701331 | return avio_r8(r->pb); | |
| 77 | } | ||
| 78 | ✗ | if (!val) | |
| 79 | ✗ | return 0; | |
| 80 | ✗ | r->buf_pos = 0; | |
| 81 | ✗ | r->buf_len = 0; | |
| 82 | ✗ | PUT_UTF8(val, tmp, r->buf[r->buf_len++] = tmp;) | |
| 83 | ✗ | return r->buf[r->buf_pos++]; // buf_len is at least 1 | |
| 84 | } | ||
| 85 | |||
| 86 | 37370 | void ff_text_read(FFTextReader *r, char *buf, size_t size) | |
| 87 | { | ||
| 88 |
2/2✓ Branch 0 taken 538128 times.
✓ Branch 1 taken 37370 times.
|
575498 | for ( ; size > 0; size--) |
| 89 | 538128 | *buf++ = ff_text_r8(r); | |
| 90 | 37370 | } | |
| 91 | |||
| 92 | 11669 | int ff_text_eof(FFTextReader *r) | |
| 93 | { | ||
| 94 |
4/4✓ Branch 0 taken 9587 times.
✓ Branch 1 taken 2082 times.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 9552 times.
|
11669 | return r->buf_pos >= r->buf_len && avio_feof(r->pb); |
| 95 | } | ||
| 96 | |||
| 97 | 75276 | int ff_text_peek_r8(FFTextReader *r) | |
| 98 | { | ||
| 99 | int c; | ||
| 100 |
2/2✓ Branch 0 taken 66444 times.
✓ Branch 1 taken 8832 times.
|
75276 | if (r->buf_pos < r->buf_len) |
| 101 | 66444 | return r->buf[r->buf_pos]; | |
| 102 | 8832 | c = ff_text_r8(r); | |
| 103 |
2/2✓ Branch 1 taken 8824 times.
✓ Branch 2 taken 8 times.
|
8832 | if (!avio_feof(r->pb)) { |
| 104 | 8824 | r->buf_pos = 0; | |
| 105 | 8824 | r->buf_len = 1; | |
| 106 | 8824 | r->buf[0] = c; | |
| 107 | } | ||
| 108 | 8832 | return c; | |
| 109 | } | ||
| 110 | |||
| 111 | 2972 | AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, | |
| 112 | const uint8_t *event, size_t len, int merge) | ||
| 113 | { | ||
| 114 | AVPacket **subs, *sub; | ||
| 115 | |||
| 116 | av_assert1(event || len == 0); | ||
| 117 | |||
| 118 |
4/4✓ Branch 0 taken 981 times.
✓ Branch 1 taken 1991 times.
✓ Branch 2 taken 980 times.
✓ Branch 3 taken 1 times.
|
3952 | if (merge && q->nb_subs > 0) { |
| 119 | /* merge with previous event */ | ||
| 120 | |||
| 121 | int old_len; | ||
| 122 | 980 | sub = q->subs[q->nb_subs - 1]; | |
| 123 | 980 | old_len = sub->size; | |
| 124 |
2/2✓ Branch 0 taken 948 times.
✓ Branch 1 taken 32 times.
|
980 | if (event) { |
| 125 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 948 times.
|
948 | if (av_grow_packet(sub, len) < 0) |
| 126 | ✗ | return NULL; | |
| 127 | 948 | memcpy(sub->data + old_len, event, len); | |
| 128 | } | ||
| 129 | } else { | ||
| 130 | /* new event */ | ||
| 131 | |||
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1992 times.
|
1992 | if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1) |
| 133 | ✗ | return NULL; | |
| 134 | 1992 | subs = av_fast_realloc(q->subs, &q->allocated_size, | |
| 135 | 1992 | (q->nb_subs + 1) * sizeof(*q->subs)); | |
| 136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1992 times.
|
1992 | if (!subs) |
| 137 | ✗ | return NULL; | |
| 138 | 1992 | q->subs = subs; | |
| 139 | 1992 | sub = av_packet_alloc(); | |
| 140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1992 times.
|
1992 | if (!sub) |
| 141 | ✗ | return NULL; | |
| 142 |
2/2✓ Branch 0 taken 1928 times.
✓ Branch 1 taken 64 times.
|
1992 | if (event) { |
| 143 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1928 times.
|
1928 | if (av_new_packet(sub, len) < 0) { |
| 144 | ✗ | av_packet_free(&sub); | |
| 145 | ✗ | return NULL; | |
| 146 | } | ||
| 147 | 1928 | memcpy(sub->data, event, len); | |
| 148 | } | ||
| 149 | 1992 | sub->flags |= AV_PKT_FLAG_KEY; | |
| 150 | 1992 | sub->pts = sub->dts = 0; | |
| 151 | 1992 | subs[q->nb_subs++] = sub; | |
| 152 | } | ||
| 153 | 2972 | return sub; | |
| 154 | } | ||
| 155 | |||
| 156 | 1913 | AVPacket *ff_subtitles_queue_insert_bprint(FFDemuxSubtitlesQueue *q, | |
| 157 | const AVBPrint *event, int merge) | ||
| 158 | { | ||
| 159 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1913 times.
|
1913 | if (!av_bprint_is_complete(event)) |
| 160 | ✗ | return NULL; | |
| 161 | 1913 | return ff_subtitles_queue_insert(q, event->str, event->len, merge); | |
| 162 | } | ||
| 163 | |||
| 164 | 5980 | static int cmp_pkt_sub_ts_pos(const void *a, const void *b) | |
| 165 | { | ||
| 166 | 5980 | const AVPacket *s1 = *(const AVPacket **)a; | |
| 167 | 5980 | const AVPacket *s2 = *(const AVPacket **)b; | |
| 168 |
2/2✓ Branch 0 taken 185 times.
✓ Branch 1 taken 5795 times.
|
5980 | if (s1->pts == s2->pts) |
| 169 | 185 | return FFDIFFSIGN(s1->pos, s2->pos); | |
| 170 | 5795 | return FFDIFFSIGN(s1->pts , s2->pts); | |
| 171 | } | ||
| 172 | |||
| 173 | 369 | static int cmp_pkt_sub_pos_ts(const void *a, const void *b) | |
| 174 | { | ||
| 175 | 369 | const AVPacket *s1 = *(const AVPacket **)a; | |
| 176 | 369 | const AVPacket *s2 = *(const AVPacket **)b; | |
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
|
369 | if (s1->pos == s2->pos) { |
| 178 | ✗ | if (s1->pts == s2->pts) | |
| 179 | ✗ | return 0; | |
| 180 | ✗ | return s1->pts > s2->pts ? 1 : -1; | |
| 181 | } | ||
| 182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
|
369 | return s1->pos > s2->pos ? 1 : -1; |
| 183 | } | ||
| 184 | |||
| 185 | 43 | static void drop_dups(void *log_ctx, FFDemuxSubtitlesQueue *q) | |
| 186 | { | ||
| 187 | 43 | int i, drop = 0; | |
| 188 | |||
| 189 |
2/2✓ Branch 0 taken 1757 times.
✓ Branch 1 taken 43 times.
|
1800 | for (i = 1; i < q->nb_subs; i++) { |
| 190 | 1757 | const int last_id = i - 1 - drop; | |
| 191 | 1757 | const AVPacket *last = q->subs[last_id]; | |
| 192 | |||
| 193 |
2/2✓ Branch 0 taken 145 times.
✓ Branch 1 taken 1612 times.
|
1757 | if (q->subs[i]->pts == last->pts && |
| 194 |
2/2✓ Branch 0 taken 59 times.
✓ Branch 1 taken 86 times.
|
145 | q->subs[i]->duration == last->duration && |
| 195 |
1/2✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
|
59 | q->subs[i]->stream_index == last->stream_index && |
| 196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
|
59 | !strcmp(q->subs[i]->data, last->data)) { |
| 197 | |||
| 198 | ✗ | av_packet_free(&q->subs[i]); | |
| 199 | ✗ | drop++; | |
| 200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1757 times.
|
1757 | } else if (drop) { |
| 201 | ✗ | q->subs[last_id + 1] = q->subs[i]; | |
| 202 | ✗ | q->subs[i] = NULL; | |
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (drop) { |
| 207 | ✗ | q->nb_subs -= drop; | |
| 208 | ✗ | av_log(log_ctx, AV_LOG_WARNING, "Dropping %d duplicated subtitle events\n", drop); | |
| 209 | } | ||
| 210 | 43 | } | |
| 211 | |||
| 212 | 49 | void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q) | |
| 213 | { | ||
| 214 | int i; | ||
| 215 | |||
| 216 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (!q->nb_subs) |
| 217 | ✗ | return; | |
| 218 | |||
| 219 | 49 | qsort(q->subs, q->nb_subs, sizeof(*q->subs), | |
| 220 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 3 times.
|
49 | q->sort == SUB_SORT_TS_POS ? cmp_pkt_sub_ts_pos |
| 221 | : cmp_pkt_sub_pos_ts); | ||
| 222 |
2/2✓ Branch 0 taken 1992 times.
✓ Branch 1 taken 49 times.
|
2041 | for (i = 0; i < q->nb_subs; i++) |
| 223 |
5/6✓ Branch 0 taken 315 times.
✓ Branch 1 taken 1677 times.
✓ Branch 2 taken 306 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 306 times.
✗ Branch 5 not taken.
|
1992 | if (q->subs[i]->duration < 0 && i < q->nb_subs - 1 && q->subs[i + 1]->pts - (uint64_t)q->subs[i]->pts <= INT64_MAX) |
| 224 | 306 | q->subs[i]->duration = q->subs[i + 1]->pts - q->subs[i]->pts; | |
| 225 | |||
| 226 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 6 times.
|
49 | if (!q->keep_duplicates) |
| 227 | 43 | drop_dups(log_ctx, q); | |
| 228 | } | ||
| 229 | |||
| 230 | 2022 | int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt) | |
| 231 | { | ||
| 232 | AVPacket *sub; | ||
| 233 | int ret; | ||
| 234 | |||
| 235 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1976 times.
|
2022 | if (q->current_sub_idx == q->nb_subs) |
| 236 | 46 | return AVERROR_EOF; | |
| 237 | 1976 | sub = q->subs[q->current_sub_idx]; | |
| 238 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1976 times.
|
1976 | if ((ret = av_packet_ref(pkt, sub)) < 0) { |
| 239 | ✗ | return ret; | |
| 240 | } | ||
| 241 | |||
| 242 | 1976 | pkt->dts = pkt->pts; | |
| 243 | 1976 | q->current_sub_idx++; | |
| 244 | 1976 | return 0; | |
| 245 | } | ||
| 246 | |||
| 247 | 2 | static int search_sub_ts(const FFDemuxSubtitlesQueue *q, int64_t ts) | |
| 248 | { | ||
| 249 | 2 | int s1 = 0, s2 = q->nb_subs - 1; | |
| 250 | |||
| 251 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (s2 < s1) |
| 252 | ✗ | return AVERROR(ERANGE); | |
| 253 | |||
| 254 | 12 | for (;;) { | |
| 255 | int mid; | ||
| 256 | |||
| 257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (s1 == s2) |
| 258 | ✗ | return s1; | |
| 259 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 | if (s1 == s2 - 1) |
| 260 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | return q->subs[s1]->pts <= q->subs[s2]->pts ? s1 : s2; |
| 261 | 12 | mid = (s1 + s2) / 2; | |
| 262 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
|
12 | if (q->subs[mid]->pts <= ts) |
| 263 | 3 | s1 = mid; | |
| 264 | else | ||
| 265 | 9 | s2 = mid; | |
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | 2 | int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index, | |
| 270 | int64_t min_ts, int64_t ts, int64_t max_ts, int flags) | ||
| 271 | { | ||
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (flags & AVSEEK_FLAG_BYTE) { |
| 273 | ✗ | return AVERROR(ENOSYS); | |
| 274 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | } else if (flags & AVSEEK_FLAG_FRAME) { |
| 275 | ✗ | if (ts < 0 || ts >= q->nb_subs) | |
| 276 | ✗ | return AVERROR(ERANGE); | |
| 277 | ✗ | q->current_sub_idx = ts; | |
| 278 | } else { | ||
| 279 | 2 | int i, idx = search_sub_ts(q, ts); | |
| 280 | int64_t ts_selected; | ||
| 281 | |||
| 282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (idx < 0) |
| 283 | ✗ | return idx; | |
| 284 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | for (i = idx; i < q->nb_subs && q->subs[i]->pts < min_ts; i++) |
| 285 | ✗ | if (stream_index == -1 || q->subs[i]->stream_index == stream_index) | |
| 286 | ✗ | idx = i; | |
| 287 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
2 | for (i = idx; i > 0 && q->subs[i]->pts > max_ts; i--) |
| 288 | ✗ | if (stream_index == -1 || q->subs[i]->stream_index == stream_index) | |
| 289 | ✗ | idx = i; | |
| 290 | |||
| 291 | 2 | ts_selected = q->subs[idx]->pts; | |
| 292 |
3/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | if (ts_selected < min_ts || ts_selected > max_ts) |
| 293 | 1 | return AVERROR(ERANGE); | |
| 294 | |||
| 295 | /* look back in the latest subtitles for overlapping subtitles */ | ||
| 296 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (i = idx - 1; i >= 0; i--) { |
| 297 | 1 | int64_t pts = q->subs[i]->pts; | |
| 298 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (q->subs[i]->duration <= 0 || |
| 299 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | (stream_index != -1 && q->subs[i]->stream_index != stream_index)) |
| 300 | ✗ | continue; | |
| 301 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (pts >= min_ts && pts > ts_selected - q->subs[i]->duration) |
| 302 | ✗ | idx = i; | |
| 303 | else | ||
| 304 | break; | ||
| 305 | } | ||
| 306 | |||
| 307 | /* If the queue is used to store multiple subtitles streams (like with | ||
| 308 | * VobSub) and the stream index is not specified, we need to make sure | ||
| 309 | * to focus on the smallest file position offset for a same timestamp; | ||
| 310 | * queue is ordered by pts and then filepos, so we can take the first | ||
| 311 | * entry for a given timestamp. */ | ||
| 312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (stream_index == -1) |
| 313 | ✗ | while (idx > 0 && q->subs[idx - 1]->pts == q->subs[idx]->pts) | |
| 314 | ✗ | idx--; | |
| 315 | |||
| 316 | 1 | q->current_sub_idx = idx; | |
| 317 | } | ||
| 318 | 1 | return 0; | |
| 319 | } | ||
| 320 | |||
| 321 | 49 | void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q) | |
| 322 | { | ||
| 323 | int i; | ||
| 324 | |||
| 325 |
2/2✓ Branch 0 taken 1992 times.
✓ Branch 1 taken 49 times.
|
2041 | for (i = 0; i < q->nb_subs; i++) |
| 326 | 1992 | av_packet_free(&q->subs[i]); | |
| 327 | 49 | av_freep(&q->subs); | |
| 328 | 49 | q->nb_subs = q->allocated_size = q->current_sub_idx = 0; | |
| 329 | 49 | } | |
| 330 | |||
| 331 | 1597 | int ff_subtitles_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 332 | { | ||
| 333 | 1597 | FFDemuxSubtitlesQueue *q = s->priv_data; | |
| 334 | 1597 | return ff_subtitles_queue_read_packet(q, pkt); | |
| 335 | } | ||
| 336 | |||
| 337 | 1 | int ff_subtitles_read_seek(AVFormatContext *s, int stream_index, | |
| 338 | int64_t min_ts, int64_t ts, int64_t max_ts, int flags) | ||
| 339 | { | ||
| 340 | 1 | FFDemuxSubtitlesQueue *q = s->priv_data; | |
| 341 | 1 | return ff_subtitles_queue_seek(q, s, stream_index, | |
| 342 | min_ts, ts, max_ts, flags); | ||
| 343 | } | ||
| 344 | |||
| 345 | 35 | int ff_subtitles_read_close(AVFormatContext *s) | |
| 346 | { | ||
| 347 | 35 | FFDemuxSubtitlesQueue *q = s->priv_data; | |
| 348 | 35 | ff_subtitles_queue_clean(q); | |
| 349 | 35 | return 0; | |
| 350 | } | ||
| 351 | |||
| 352 | 1111 | int ff_smil_extract_next_text_chunk(FFTextReader *tr, AVBPrint *buf, char *c) | |
| 353 | { | ||
| 354 | 1111 | int i = 0; | |
| 355 | char end_chr; | ||
| 356 | |||
| 357 |
2/2✓ Branch 0 taken 683 times.
✓ Branch 1 taken 428 times.
|
1111 | if (!*c) // cached char? |
| 358 | 683 | *c = ff_text_r8(tr); | |
| 359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1111 times.
|
1111 | if (!*c) |
| 360 | ✗ | return 0; | |
| 361 | |||
| 362 |
2/2✓ Branch 0 taken 682 times.
✓ Branch 1 taken 429 times.
|
1111 | end_chr = *c == '<' ? '>' : '<'; |
| 363 | do { | ||
| 364 | 14499 | av_bprint_chars(buf, *c, 1); | |
| 365 | 14499 | *c = ff_text_r8(tr); | |
| 366 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14499 times.
|
14499 | if (i == INT_MAX) |
| 367 | ✗ | return AVERROR_INVALIDDATA; | |
| 368 | 14499 | i++; | |
| 369 |
4/4✓ Branch 0 taken 13389 times.
✓ Branch 1 taken 1110 times.
✓ Branch 2 taken 13388 times.
✓ Branch 3 taken 1 times.
|
14499 | } while (*c != end_chr && *c); |
| 370 |
2/2✓ Branch 0 taken 682 times.
✓ Branch 1 taken 429 times.
|
1111 | if (end_chr == '>') { |
| 371 | 682 | av_bprint_chars(buf, '>', 1); | |
| 372 | 682 | *c = 0; | |
| 373 | } | ||
| 374 |
1/2✓ Branch 1 taken 1111 times.
✗ Branch 2 not taken.
|
1111 | return av_bprint_is_complete(buf) ? i : AVERROR(ENOMEM); |
| 375 | } | ||
| 376 | |||
| 377 | 176 | const char *ff_smil_get_attr_ptr(const char *s, const char *attr) | |
| 378 | { | ||
| 379 | 176 | int in_quotes = 0; | |
| 380 | 176 | const size_t len = strlen(attr); | |
| 381 | |||
| 382 |
2/2✓ Branch 0 taken 182 times.
✓ Branch 1 taken 3 times.
|
185 | while (*s) { |
| 383 |
2/2✓ Branch 0 taken 1142 times.
✓ Branch 1 taken 3 times.
|
1145 | while (*s) { |
| 384 |
4/4✓ Branch 0 taken 1110 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 179 times.
✓ Branch 3 taken 931 times.
|
1142 | if (!in_quotes && av_isspace(*s)) |
| 385 | 179 | break; | |
| 386 | 963 | in_quotes ^= *s == '"'; // XXX: support escaping? | |
| 387 | 963 | s++; | |
| 388 | } | ||
| 389 |
2/2✓ Branch 0 taken 179 times.
✓ Branch 1 taken 182 times.
|
361 | while (av_isspace(*s)) |
| 390 | 179 | s++; | |
| 391 |
3/4✓ Branch 1 taken 173 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 173 times.
✗ Branch 4 not taken.
|
182 | if (!av_strncasecmp(s, attr, len) && s[len] == '=') |
| 392 | 173 | return s + len + 1 + (s[len + 1] == '"'); | |
| 393 | } | ||
| 394 | 3 | return NULL; | |
| 395 | } | ||
| 396 | |||
| 397 | 5315 | static inline int is_eol(char c) | |
| 398 | { | ||
| 399 |
4/4✓ Branch 0 taken 5303 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 5080 times.
|
5315 | return c == '\r' || c == '\n'; |
| 400 | } | ||
| 401 | |||
| 402 | 73 | int ff_subtitles_read_text_chunk(FFTextReader *tr, AVBPrint *buf) | |
| 403 | { | ||
| 404 | 73 | char eol_buf[5], last_was_cr = 0; | |
| 405 | 73 | int n = 0, i = 0, nb_eol = 0; | |
| 406 | |||
| 407 | 73 | av_bprint_clear(buf); | |
| 408 | |||
| 409 | 5180 | for (;;) { | |
| 410 | 5253 | char c = ff_text_r8(tr); | |
| 411 | |||
| 412 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5245 times.
|
5253 | if (!c) |
| 413 | 8 | break; | |
| 414 | |||
| 415 | /* ignore all initial line breaks */ | ||
| 416 |
3/4✓ Branch 0 taken 70 times.
✓ Branch 1 taken 5175 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 70 times.
|
5245 | if (n == 0 && is_eol(c)) |
| 417 | ✗ | continue; | |
| 418 | |||
| 419 | /* line break buffering: we don't want to add the trailing \r\n */ | ||
| 420 |
2/2✓ Branch 1 taken 235 times.
✓ Branch 2 taken 5010 times.
|
5245 | if (is_eol(c)) { |
| 421 |
3/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 223 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
|
235 | nb_eol += c == '\n' || last_was_cr; |
| 422 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 170 times.
|
235 | if (nb_eol == 2) |
| 423 | 65 | break; | |
| 424 | 170 | eol_buf[i++] = c; | |
| 425 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
|
170 | if (i == sizeof(eol_buf) - 1) |
| 426 | ✗ | break; | |
| 427 | 170 | last_was_cr = c == '\r'; | |
| 428 | 170 | continue; | |
| 429 | } | ||
| 430 | |||
| 431 | /* only one line break followed by data: we flush the line breaks | ||
| 432 | * buffer */ | ||
| 433 |
2/2✓ Branch 0 taken 89 times.
✓ Branch 1 taken 4921 times.
|
5010 | if (i) { |
| 434 | 89 | eol_buf[i] = 0; | |
| 435 | 89 | av_bprintf(buf, "%s", eol_buf); | |
| 436 | 89 | i = nb_eol = 0; | |
| 437 | } | ||
| 438 | |||
| 439 | 5010 | av_bprint_chars(buf, c, 1); | |
| 440 | 5010 | n++; | |
| 441 | } | ||
| 442 |
1/2✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
|
73 | return av_bprint_is_complete(buf) ? 0 : AVERROR(ENOMEM); |
| 443 | } | ||
| 444 | |||
| 445 | 73 | int ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf) | |
| 446 | { | ||
| 447 | FFTextReader tr; | ||
| 448 | 73 | tr.buf_pos = tr.buf_len = 0; | |
| 449 | 73 | tr.type = 0; | |
| 450 | 73 | tr.pb = pb; | |
| 451 | 73 | return ff_subtitles_read_text_chunk(&tr, buf); | |
| 452 | } | ||
| 453 | |||
| 454 | 12527 | ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size) | |
| 455 | { | ||
| 456 | 12527 | size_t cur = 0; | |
| 457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12527 times.
|
12527 | if (!size) |
| 458 | ✗ | return 0; | |
| 459 | 12527 | buf[0] = '\0'; | |
| 460 |
2/2✓ Branch 0 taken 249034 times.
✓ Branch 1 taken 268 times.
|
249302 | while (cur + 1 < size) { |
| 461 | 249034 | unsigned char c = ff_text_r8(tr); | |
| 462 |
2/2✓ Branch 0 taken 5864 times.
✓ Branch 1 taken 243170 times.
|
249034 | if (!c) |
| 463 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 2 taken 5849 times.
|
5864 | return ff_text_eof(tr) ? cur : AVERROR_INVALIDDATA; |
| 464 |
4/4✓ Branch 0 taken 238041 times.
✓ Branch 1 taken 5129 times.
✓ Branch 2 taken 236775 times.
✓ Branch 3 taken 1266 times.
|
243170 | if (c == '\r' || c == '\n') |
| 465 | break; | ||
| 466 | 236775 | buf[cur++] = c; | |
| 467 | 236775 | buf[cur] = '\0'; | |
| 468 | } | ||
| 469 |
2/2✓ Branch 1 taken 2118 times.
✓ Branch 2 taken 6663 times.
|
8781 | while (ff_text_peek_r8(tr) == '\r') |
| 470 | 2118 | ff_text_r8(tr); | |
| 471 |
2/2✓ Branch 1 taken 4715 times.
✓ Branch 2 taken 1948 times.
|
6663 | if (ff_text_peek_r8(tr) == '\n') |
| 472 | 4715 | ff_text_r8(tr); | |
| 473 | 6663 | return cur; | |
| 474 | } | ||
| 475 |