| 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 | * MPlayer subtitles format demuxer | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include "avformat.h" | ||
| 27 | #include "demux.h" | ||
| 28 | #include "internal.h" | ||
| 29 | #include "subtitles.h" | ||
| 30 | |||
| 31 | #define TSBASE 10000000 | ||
| 32 | |||
| 33 | typedef struct { | ||
| 34 | FFDemuxSubtitlesQueue q; | ||
| 35 | } MPSubContext; | ||
| 36 | |||
| 37 | 7480 | static int mpsub_probe(const AVProbeData *p) | |
| 38 | { | ||
| 39 | 7480 | const char *ptr = p->buf; | |
| 40 | 7480 | const char *ptr_end = p->buf + p->buf_size; | |
| 41 | |||
| 42 |
2/2✓ Branch 0 taken 18560 times.
✓ Branch 1 taken 96 times.
|
18656 | while (ptr < ptr_end) { |
| 43 | int inc; | ||
| 44 | |||
| 45 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18559 times.
|
18560 | if (!memcmp(ptr, "FORMAT=TIME", 11)) |
| 46 | 1 | return AVPROBE_SCORE_EXTENSION; | |
| 47 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18557 times.
|
18559 | if (!memcmp(ptr, "FORMAT=", 7)) |
| 48 | 2 | return AVPROBE_SCORE_EXTENSION / 3; | |
| 49 | 18557 | inc = ff_subtitles_next_line(ptr); | |
| 50 |
2/2✓ Branch 0 taken 7381 times.
✓ Branch 1 taken 11176 times.
|
18557 | if (!inc) |
| 51 | 7381 | break; | |
| 52 | 11176 | ptr += inc; | |
| 53 | } | ||
| 54 | 7477 | return 0; | |
| 55 | } | ||
| 56 | |||
| 57 | 42 | static int parse_line(const char *line, int64_t *value, int64_t *value2) | |
| 58 | { | ||
| 59 | int vi, p1, p2; | ||
| 60 | |||
| 61 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 23 times.
|
88 | for (vi = 0; vi < 2; vi++) { |
| 62 | long long intval, fracval; | ||
| 63 | 65 | int n = av_sscanf(line, "%lld%n.%lld%n", &intval, &p1, &fracval, &p2); | |
| 64 |
4/6✓ Branch 0 taken 46 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 46 times.
|
65 | if (n <= 0 || intval < INT64_MIN / TSBASE || intval > INT64_MAX / TSBASE) |
| 65 | 19 | return AVERROR_INVALIDDATA; | |
| 66 | |||
| 67 | 46 | intval *= TSBASE; | |
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 41 times.
|
46 | if (n == 2) { |
| 70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (fracval < 0) |
| 71 | ✗ | return AVERROR_INVALIDDATA; | |
| 72 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 5 times.
|
35 | for (;p2 - p1 < 7 + 1; p1--) |
| 73 | 30 | fracval *= 10; | |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | for (;p2 - p1 > 7 + 1; p1++) |
| 75 | ✗ | fracval /= 10; | |
| 76 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (intval > 0) intval = av_sat_add64(intval, fracval); |
| 77 | ✗ | else intval = av_sat_sub64(intval, fracval); | |
| 78 | 5 | line += p2; | |
| 79 | } else | ||
| 80 | 41 | line += p1; | |
| 81 | |||
| 82 | 46 | *value = intval; | |
| 83 | |||
| 84 | 46 | value = value2; | |
| 85 | } | ||
| 86 | |||
| 87 | 23 | return 0; | |
| 88 | } | ||
| 89 | |||
| 90 | 2 | static int mpsub_read_header(AVFormatContext *s) | |
| 91 | { | ||
| 92 | 2 | MPSubContext *mpsub = s->priv_data; | |
| 93 | AVStream *st; | ||
| 94 | AVBPrint buf; | ||
| 95 | 2 | AVRational pts_info = (AVRational){ TSBASE, 1 }; // ts based by default | |
| 96 | 2 | int res = 0; | |
| 97 | 2 | int64_t current_pts = 0; | |
| 98 | int i; | ||
| 99 | 2 | int common_factor = 0; | |
| 100 | |||
| 101 | 2 | av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 102 | |||
| 103 |
2/2✓ Branch 1 taken 43 times.
✓ Branch 2 taken 2 times.
|
45 | while (!avio_feof(s->pb)) { |
| 104 | char line[1024]; | ||
| 105 | int64_t start, duration; | ||
| 106 | 43 | int fps, len = ff_get_line(s->pb, line, sizeof(line)); | |
| 107 | |||
| 108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (!len) |
| 109 | ✗ | break; | |
| 110 | |||
| 111 | 43 | line[strcspn(line, "\r\n")] = 0; | |
| 112 | |||
| 113 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
43 | if (sscanf(line, "FORMAT=%d", &fps) == 1 && fps > 3 && fps < 100) { |
| 114 | /* frame based timing */ | ||
| 115 | 1 | pts_info = (AVRational){ TSBASE * fps, 1 }; | |
| 116 |
2/2✓ Branch 1 taken 23 times.
✓ Branch 2 taken 19 times.
|
42 | } else if (parse_line(line, &start, &duration) >= 0) { |
| 117 | AVPacket *sub; | ||
| 118 | 23 | const int64_t pos = avio_tell(s->pb); | |
| 119 | |||
| 120 | 23 | res = ff_subtitles_read_chunk(s->pb, &buf); | |
| 121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (res < 0) goto end; |
| 122 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | if (buf.len) { |
| 123 | 23 | sub = ff_subtitles_queue_insert_bprint(&mpsub->q, &buf, 0); | |
| 124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (!sub) { |
| 125 | ✗ | res = AVERROR(ENOMEM); | |
| 126 | ✗ | goto end; | |
| 127 | } | ||
| 128 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
23 | if ( current_pts < 0 && start < INT64_MIN - current_pts |
| 129 |
3/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
|
23 | || current_pts > 0 && start > INT64_MAX - current_pts) { |
| 130 | ✗ | res = AVERROR_INVALIDDATA; | |
| 131 | ✗ | goto end; | |
| 132 | } | ||
| 133 | 23 | sub->pts = current_pts + start; | |
| 134 |
2/4✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
|
23 | if (duration < 0 || sub->pts > INT64_MAX - duration) { |
| 135 | ✗ | res = AVERROR_INVALIDDATA; | |
| 136 | ✗ | goto end; | |
| 137 | } | ||
| 138 | 23 | sub->duration = duration; | |
| 139 | |||
| 140 | 23 | common_factor = av_gcd(duration, common_factor); | |
| 141 | 23 | common_factor = av_gcd(sub->pts, common_factor); | |
| 142 | |||
| 143 | 23 | current_pts = sub->pts + duration; | |
| 144 | 23 | sub->pos = pos; | |
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (common_factor > 1) { |
| 150 | 2 | common_factor = av_gcd(pts_info.num, common_factor); | |
| 151 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 2 times.
|
25 | for (i = 0; i < mpsub->q.nb_subs; i++) { |
| 152 | 23 | mpsub->q.subs[i]->pts /= common_factor; | |
| 153 | 23 | mpsub->q.subs[i]->duration /= common_factor; | |
| 154 | } | ||
| 155 | 2 | pts_info.num /= common_factor; | |
| 156 | } | ||
| 157 | |||
| 158 | 2 | st = avformat_new_stream(s, NULL); | |
| 159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) { |
| 160 | ✗ | res = AVERROR(ENOMEM); | |
| 161 | ✗ | goto end; | |
| 162 | } | ||
| 163 | 2 | avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num); | |
| 164 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
| 165 | 2 | st->codecpar->codec_id = AV_CODEC_ID_TEXT; | |
| 166 | |||
| 167 | 2 | ff_subtitles_queue_finalize(s, &mpsub->q); | |
| 168 | |||
| 169 | 2 | end: | |
| 170 | 2 | av_bprint_finalize(&buf, NULL); | |
| 171 | 2 | return res; | |
| 172 | } | ||
| 173 | |||
| 174 | const FFInputFormat ff_mpsub_demuxer = { | ||
| 175 | .p.name = "mpsub", | ||
| 176 | .p.long_name = NULL_IF_CONFIG_SMALL("MPlayer subtitles"), | ||
| 177 | .p.extensions = "sub", | ||
| 178 | .priv_data_size = sizeof(MPSubContext), | ||
| 179 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 180 | .read_probe = mpsub_probe, | ||
| 181 | .read_header = mpsub_read_header, | ||
| 182 | .read_packet = ff_subtitles_read_packet, | ||
| 183 | .read_seek2 = ff_subtitles_read_seek, | ||
| 184 | .read_close = ff_subtitles_read_close, | ||
| 185 | }; | ||
| 186 |