| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * SCC subtitle demuxer | ||
| 3 | * Copyright (c) 2017 Paul B Mahol | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "avformat.h" | ||
| 23 | #include "demux.h" | ||
| 24 | #include "internal.h" | ||
| 25 | #include "subtitles.h" | ||
| 26 | #include "libavutil/avstring.h" | ||
| 27 | #include "libavutil/intreadwrite.h" | ||
| 28 | |||
| 29 | typedef struct SCCContext { | ||
| 30 | FFDemuxSubtitlesQueue q; | ||
| 31 | } SCCContext; | ||
| 32 | |||
| 33 | 7474 | static int scc_probe(const AVProbeData *p) | |
| 34 | { | ||
| 35 | char buf[18]; | ||
| 36 | FFTextReader tr; | ||
| 37 | |||
| 38 | 7474 | ff_text_init_buf(&tr, p->buf, p->buf_size); | |
| 39 | |||
| 40 |
4/4✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7477 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 7474 times.
|
7481 | while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n') |
| 41 | 7 | ff_text_r8(&tr); | |
| 42 | |||
| 43 | 7474 | ff_text_read(&tr, buf, sizeof(buf)); | |
| 44 | |||
| 45 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7471 times.
|
7474 | if (!memcmp(buf, "Scenarist_SCC V1.0", 18)) |
| 46 | 3 | return AVPROBE_SCORE_MAX; | |
| 47 | |||
| 48 | 7471 | return 0; | |
| 49 | } | ||
| 50 | |||
| 51 | 34176 | static int convert(uint8_t x) | |
| 52 | { | ||
| 53 |
2/2✓ Branch 0 taken 8448 times.
✓ Branch 1 taken 25728 times.
|
34176 | if (x >= 'a') |
| 54 | 8448 | x -= 'a' - 10; | |
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25728 times.
|
25728 | else if (x >= 'A') |
| 56 | ✗ | x -= 'A' - 10; | |
| 57 | else | ||
| 58 | 25728 | x -= '0'; | |
| 59 | 34176 | return x; | |
| 60 | } | ||
| 61 | |||
| 62 | 3 | static int scc_read_header(AVFormatContext *s) | |
| 63 | { | ||
| 64 | 3 | SCCContext *scc = s->priv_data; | |
| 65 | 3 | AVStream *st = avformat_new_stream(s, NULL); | |
| 66 | 3 | AVPacket *sub = NULL; | |
| 67 | ptrdiff_t len; | ||
| 68 | uint8_t out[4096]; | ||
| 69 | FFTextReader tr; | ||
| 70 | |||
| 71 | 3 | ff_text_init_avio(s, &tr, s->pb); | |
| 72 | |||
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st) |
| 74 | ✗ | return AVERROR(ENOMEM); | |
| 75 | 3 | avpriv_set_pts_info(st, 64, 1, 1000); | |
| 76 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
| 77 | 3 | st->codecpar->codec_id = AV_CODEC_ID_EIA_608; | |
| 78 | |||
| 79 | 729 | while (1) { | |
| 80 | 732 | char *saveptr = NULL, *lline; | |
| 81 | int hh, mm, ss, fs, i; | ||
| 82 | char line[4096]; | ||
| 83 | int64_t pos, ts; | ||
| 84 | |||
| 85 | 732 | pos = ff_text_pos(&tr); | |
| 86 | 732 | len = ff_subtitles_read_line(&tr, line, sizeof(line)); | |
| 87 |
2/2✓ Branch 0 taken 366 times.
✓ Branch 1 taken 366 times.
|
732 | if (len <= 13) { |
| 88 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 363 times.
|
366 | if (ff_text_eof(&tr)) |
| 89 | 3 | break; | |
| 90 | 369 | continue; | |
| 91 | } | ||
| 92 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 360 times.
|
366 | if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) != 4) |
| 93 | 6 | continue; | |
| 94 | |||
| 95 | 360 | ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33LL; | |
| 96 |
2/2✓ Branch 0 taken 357 times.
✓ Branch 1 taken 3 times.
|
360 | if (sub) |
| 97 | 357 | sub->duration = ts - sub->pts; | |
| 98 | |||
| 99 | 360 | lline = line; | |
| 100 | 360 | lline += 12; | |
| 101 | |||
| 102 |
1/2✓ Branch 0 taken 8904 times.
✗ Branch 1 not taken.
|
8904 | for (i = 0; i < 4095; i += 3) { |
| 103 | 8904 | char *ptr = av_strtok(lline, " ", &saveptr); | |
| 104 | char c1, c2, c3, c4; | ||
| 105 | uint8_t o1, o2; | ||
| 106 | |||
| 107 |
2/2✓ Branch 0 taken 360 times.
✓ Branch 1 taken 8544 times.
|
8904 | if (!ptr) |
| 108 | 360 | break; | |
| 109 | |||
| 110 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8544 times.
|
8544 | if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4) |
| 111 | ✗ | break; | |
| 112 | 8544 | o1 = convert(c2) | (convert(c1) << 4); | |
| 113 | 8544 | o2 = convert(c4) | (convert(c3) << 4); | |
| 114 | |||
| 115 | 8544 | lline = NULL; | |
| 116 | |||
| 117 |
5/10✓ Branch 0 taken 6882 times.
✓ Branch 1 taken 1662 times.
✓ Branch 2 taken 1818 times.
✓ Branch 3 taken 5064 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1818 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
8544 | if (i > 12 && o1 == 0x94 && o2 == 0x20 && saveptr && |
| 118 | ✗ | (av_strncasecmp(saveptr, "942f", 4) && !av_strncasecmp(saveptr, "942c", 4))) { | |
| 119 | |||
| 120 | ✗ | sub = ff_subtitles_queue_insert(&scc->q, out, i, 0); | |
| 121 | ✗ | if (!sub) | |
| 122 | ✗ | return AVERROR(ENOMEM); | |
| 123 | |||
| 124 | ✗ | sub->pos = pos; | |
| 125 | ✗ | pos += i; | |
| 126 | ✗ | sub->pts = ts; | |
| 127 | ✗ | sub->duration = i * 11; | |
| 128 | ✗ | ts += sub->duration; | |
| 129 | ✗ | i = 0; | |
| 130 | } | ||
| 131 | |||
| 132 | 8544 | out[i+0] = 0xfc; | |
| 133 | 8544 | out[i+1] = o1; | |
| 134 | 8544 | out[i+2] = o2; | |
| 135 | } | ||
| 136 | |||
| 137 | 360 | sub = ff_subtitles_queue_insert(&scc->q, out, i, 0); | |
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
|
360 | if (!sub) |
| 139 | ✗ | return AVERROR(ENOMEM); | |
| 140 | |||
| 141 | 360 | sub->pos = pos; | |
| 142 | 360 | sub->pts = ts; | |
| 143 | } | ||
| 144 | |||
| 145 | 3 | ff_subtitles_queue_finalize(s, &scc->q); | |
| 146 | |||
| 147 | 3 | return 0; | |
| 148 | } | ||
| 149 | |||
| 150 | const FFInputFormat ff_scc_demuxer = { | ||
| 151 | .p.name = "scc", | ||
| 152 | .p.long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"), | ||
| 153 | .p.extensions = "scc", | ||
| 154 | .priv_data_size = sizeof(SCCContext), | ||
| 155 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 156 | .read_probe = scc_probe, | ||
| 157 | .read_header = scc_read_header, | ||
| 158 | .read_packet = ff_subtitles_read_packet, | ||
| 159 | .read_seek2 = ff_subtitles_read_seek, | ||
| 160 | .read_close = ff_subtitles_read_close, | ||
| 161 | }; | ||
| 162 |