Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 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 "internal.h" |
| 29 | #include "subtitles.h" |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 30 | #include "libavutil/avstring.h" |
| 31 | #include "libavutil/bprint.h" |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 32 | |
| 33 | typedef struct { |
| 34 | FFDemuxSubtitlesQueue q; |
| 35 | } SAMIContext; |
| 36 | |
Carl Eugen Hoyos | 4d8875e | 2019-03-21 00:18:37 | [diff] [blame] | 37 | static int sami_probe(const AVProbeData *p) |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 38 | { |
wm4 | 231a514 | 2014-09-02 18:53:08 | [diff] [blame] | 39 | char buf[6]; |
| 40 | FFTextReader tr; |
| 41 | ff_text_init_buf(&tr, p->buf, p->buf_size); |
| 42 | ff_text_read(&tr, buf, sizeof(buf)); |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 43 | |
wm4 | 231a514 | 2014-09-02 18:53:08 | [diff] [blame] | 44 | return !strncmp(buf, "<SAMI>", 6) ? AVPROBE_SCORE_MAX : 0; |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int sami_read_header(AVFormatContext *s) |
| 48 | { |
| 49 | SAMIContext *sami = s->priv_data; |
| 50 | AVStream *st = avformat_new_stream(s, NULL); |
| 51 | AVBPrint buf, hdr_buf; |
| 52 | char c = 0; |
| 53 | int res = 0, got_first_sync_point = 0; |
wm4 | 231a514 | 2014-09-02 18:53:08 | [diff] [blame] | 54 | FFTextReader tr; |
Carl Eugen Hoyos | 19a6431 | 2014-10-29 00:32:44 | [diff] [blame] | 55 | ff_text_init_avio(s, &tr, s->pb); |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 56 | |
| 57 | if (!st) |
| 58 | return AVERROR(ENOMEM); |
| 59 | avpriv_set_pts_info(st, 64, 1, 1000); |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 60 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; |
| 61 | st->codecpar->codec_id = AV_CODEC_ID_SAMI; |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 62 | |
| 63 | av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); |
| 64 | av_bprint_init(&hdr_buf, 0, AV_BPRINT_SIZE_UNLIMITED); |
| 65 | |
wm4 | 231a514 | 2014-09-02 18:53:08 | [diff] [blame] | 66 | while (!ff_text_eof(&tr)) { |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 67 | AVPacket *sub; |
wm4 | 231a514 | 2014-09-02 18:53:08 | [diff] [blame] | 68 | const int64_t pos = ff_text_pos(&tr) - (c != 0); |
Yayoi | 1bb8a53 | 2015-08-30 14:24:49 | [diff] [blame] | 69 | int is_sync, is_body, n = ff_smil_extract_next_text_chunk(&tr, &buf, &c); |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 70 | |
Reimar Döffinger | c0f867b | 2023-06-21 18:06:09 | [diff] [blame] | 71 | if (n < 0) { |
| 72 | res = n; |
| 73 | goto end; |
| 74 | } |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 75 | if (n == 0) |
| 76 | break; |
| 77 | |
Yayoi | 1bb8a53 | 2015-08-30 14:24:49 | [diff] [blame] | 78 | is_body = !av_strncasecmp(buf.str, "</BODY", 6); |
| 79 | if (is_body) { |
| 80 | av_bprint_clear(&buf); |
| 81 | break; |
| 82 | } |
| 83 | |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 84 | is_sync = !av_strncasecmp(buf.str, "<SYNC", 5); |
| 85 | if (is_sync) |
| 86 | got_first_sync_point = 1; |
| 87 | |
| 88 | if (!got_first_sync_point) { |
| 89 | av_bprintf(&hdr_buf, "%s", buf.str); |
| 90 | } else { |
Reimar Döffinger | c0f867b | 2023-06-21 18:06:09 | [diff] [blame] | 91 | sub = ff_subtitles_queue_insert_bprint(&sami->q, &buf, !is_sync); |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 92 | if (!sub) { |
| 93 | res = AVERROR(ENOMEM); |
Michael Niedermayer | 1c88098 | 2021-02-22 19:44:36 | [diff] [blame] | 94 | av_bprint_finalize(&hdr_buf, NULL); |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 95 | goto end; |
| 96 | } |
| 97 | if (is_sync) { |
| 98 | const char *p = ff_smil_get_attr_ptr(buf.str, "Start"); |
| 99 | sub->pos = pos; |
| 100 | sub->pts = p ? strtol(p, NULL, 10) : 0; |
Michael Niedermayer | 2014b01 | 2021-01-31 16:00:38 | [diff] [blame] | 101 | if (sub->pts <= INT64_MIN/2 || sub->pts >= INT64_MAX/2) { |
| 102 | res = AVERROR_PATCHWELCOME; |
Michael Niedermayer | 1c88098 | 2021-02-22 19:44:36 | [diff] [blame] | 103 | av_bprint_finalize(&hdr_buf, NULL); |
Michael Niedermayer | 2014b01 | 2021-01-31 16:00:38 | [diff] [blame] | 104 | goto end; |
| 105 | } |
| 106 | |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 107 | sub->duration = -1; |
| 108 | } |
| 109 | } |
| 110 | av_bprint_clear(&buf); |
| 111 | } |
| 112 | |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 113 | res = ff_bprint_to_codecpar_extradata(st->codecpar, &hdr_buf); |
Clément Bœsch | 36e61e2 | 2012-12-29 21:09:59 | [diff] [blame] | 114 | if (res < 0) |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 115 | goto end; |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 116 | |
Clément Bœsch | af924fd | 2015-09-10 19:40:07 | [diff] [blame] | 117 | ff_subtitles_queue_finalize(s, &sami->q); |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 118 | |
| 119 | end: |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 120 | av_bprint_finalize(&buf, NULL); |
| 121 | return res; |
| 122 | } |
| 123 | |
Andreas Rheinhardt | bc70684 | 2021-04-19 17:45:24 | [diff] [blame] | 124 | const AVInputFormat ff_sami_demuxer = { |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 125 | .name = "sami", |
| 126 | .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle format"), |
| 127 | .priv_data_size = sizeof(SAMIContext), |
Andreas Rheinhardt | 7ae7d93 | 2020-03-21 17:31:06 | [diff] [blame] | 128 | .flags_internal = FF_FMT_INIT_CLEANUP, |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 129 | .read_probe = sami_probe, |
| 130 | .read_header = sami_read_header, |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 131 | .extensions = "smi,sami", |
Andreas Rheinhardt | ea5bdc8 | 2021-07-08 21:05:08 | [diff] [blame] | 132 | .read_packet = ff_subtitles_read_packet, |
| 133 | .read_seek2 = ff_subtitles_read_seek, |
| 134 | .read_close = ff_subtitles_read_close, |
Clément Bœsch | 53640f4 | 2012-06-17 09:42:47 | [diff] [blame] | 135 | }; |