LCOV - code coverage report
Current view: top level - libavformat - srtdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 95 105 90.5 %
Date: 2017-12-11 20:48:03 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /*
       2             :  * SubRip subtitle demuxer
       3             :  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
       4             :  * Copyright (c) 2015  Clément Bœsch <u pkh me>
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : #include "avformat.h"
      24             : #include "internal.h"
      25             : #include "subtitles.h"
      26             : #include "libavutil/bprint.h"
      27             : #include "libavutil/intreadwrite.h"
      28             : 
      29             : typedef struct {
      30             :     FFDemuxSubtitlesQueue q;
      31             : } SRTContext;
      32             : 
      33        6130 : static int srt_probe(AVProbeData *p)
      34             : {
      35             :     int v;
      36             :     char buf[64], *pbuf;
      37             :     FFTextReader tr;
      38             : 
      39        6130 :     ff_text_init_buf(&tr, p->buf, p->buf_size);
      40             : 
      41       12267 :     while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
      42           7 :         ff_text_r8(&tr);
      43             : 
      44             :     /* Check if the first non-empty line is a number. We do not check what the
      45             :      * number is because in practice it can be anything.
      46             :      * Also, that number can be followed by random garbage, so we can not
      47             :      * unfortunately check that we only have a number. */
      48        7414 :     if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0 ||
      49        2568 :         strtol(buf, &pbuf, 10) < 0 || pbuf == buf)
      50        6121 :         return 0;
      51             : 
      52             :     /* Check if the next line matches a SRT timestamp */
      53           9 :     if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
      54           0 :         return 0;
      55           9 :     pbuf = buf;
      56           9 :     if (buf[0] == '-')
      57           0 :         pbuf++;
      58           9 :     if (pbuf[0] >= '0' && pbuf[0] <= '9' && strstr(buf, " --> ")
      59           7 :         && sscanf(buf, "%*d:%*d:%*d%*1[,.]%*d --> %*d:%*d:%*d%*1[,.]%d", &v) == 1)
      60           7 :         return AVPROBE_SCORE_MAX;
      61             : 
      62           2 :     return 0;
      63             : }
      64             : 
      65             : struct event_info {
      66             :     int32_t x1, x2, y1, y2;
      67             :     int duration;
      68             :     int64_t pts;
      69             :     int64_t pos;
      70             : };
      71             : 
      72        2281 : static int get_event_info(const char *line, struct event_info *ei)
      73             : {
      74             :     int hh1, mm1, ss1, ms1;
      75             :     int hh2, mm2, ss2, ms2;
      76             : 
      77        2281 :     ei->x1 = ei->x2 = ei->y1 = ei->y2 = ei->duration = -1;
      78        2281 :     ei->pts = AV_NOPTS_VALUE;
      79        2281 :     ei->pos = -1;
      80        2281 :     if (sscanf(line, "%d:%d:%d%*1[,.]%d --> %d:%d:%d%*1[,.]%d"
      81             :                "%*[ ]X1:%"PRId32" X2:%"PRId32" Y1:%"PRId32" Y2:%"PRId32,
      82             :                &hh1, &mm1, &ss1, &ms1,
      83             :                &hh2, &mm2, &ss2, &ms2,
      84             :                &ei->x1, &ei->x2, &ei->y1, &ei->y2) >= 8) {
      85         589 :         const int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
      86         589 :         const int64_t end   = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
      87         589 :         ei->duration = end - start;
      88         589 :         ei->pts = start;
      89         589 :         return 0;
      90             :     }
      91        1692 :     return -1;
      92             : }
      93             : 
      94         589 : static int add_event(FFDemuxSubtitlesQueue *q, AVBPrint *buf, char *line_cache,
      95             :                      const struct event_info *ei, int append_cache)
      96             : {
      97         589 :     if (append_cache && line_cache[0])
      98           2 :         av_bprintf(buf, "%s\n", line_cache);
      99         589 :     line_cache[0] = 0;
     100             : 
     101        1763 :     while (buf->len > 0 && buf->str[buf->len - 1] == '\n')
     102         585 :         buf->str[--buf->len] = 0;
     103             : 
     104         589 :     if (buf->len) {
     105         585 :         AVPacket *sub = ff_subtitles_queue_insert(q, buf->str, buf->len, 0);
     106         585 :         if (!sub)
     107           0 :             return AVERROR(ENOMEM);
     108         585 :         av_bprint_clear(buf);
     109         585 :         sub->pos = ei->pos;
     110         585 :         sub->pts = ei->pts;
     111         585 :         sub->duration = ei->duration;
     112         585 :         if (ei->x1 != -1) {
     113           3 :             uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
     114           3 :             if (p) {
     115           3 :                 AV_WL32(p,      ei->x1);
     116           3 :                 AV_WL32(p +  4, ei->y1);
     117           3 :                 AV_WL32(p +  8, ei->x2);
     118           3 :                 AV_WL32(p + 12, ei->y2);
     119             :             }
     120             :         }
     121             :     }
     122             : 
     123         589 :     return 0;
     124             : }
     125             : 
     126           7 : static int srt_read_header(AVFormatContext *s)
     127             : {
     128           7 :     SRTContext *srt = s->priv_data;
     129             :     AVBPrint buf;
     130           7 :     AVStream *st = avformat_new_stream(s, NULL);
     131           7 :     int res = 0;
     132             :     char line[4096], line_cache[4096];
     133           7 :     int has_event_info = 0;
     134             :     struct event_info ei;
     135             :     FFTextReader tr;
     136           7 :     ff_text_init_avio(s, &tr, s->pb);
     137             : 
     138           7 :     if (!st)
     139           0 :         return AVERROR(ENOMEM);
     140           7 :     avpriv_set_pts_info(st, 64, 1, 1000);
     141           7 :     st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
     142           7 :     st->codecpar->codec_id   = AV_CODEC_ID_SUBRIP;
     143             : 
     144           7 :     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
     145             : 
     146           7 :     line_cache[0] = 0;
     147             : 
     148        2904 :     while (!ff_text_eof(&tr)) {
     149             :         struct event_info tmp_ei;
     150        2890 :         const int64_t pos = ff_text_pos(&tr);
     151        2890 :         ptrdiff_t len = ff_subtitles_read_line(&tr, line, sizeof(line));
     152             : 
     153        2890 :         if (len < 0)
     154           0 :             break;
     155             : 
     156        2890 :         if (!len || !line[0])
     157        1225 :             continue;
     158             : 
     159        2281 :         if (get_event_info(line, &tmp_ei) < 0) {
     160             :             char *pline;
     161             : 
     162        1692 :             if (!has_event_info)
     163           7 :                 continue;
     164             : 
     165        1685 :             if (line_cache[0]) {
     166             :                 /* We got some cache and a new line so we assume the cached
     167             :                  * line was actually part of the payload */
     168          11 :                 av_bprintf(&buf, "%s\n", line_cache);
     169          11 :                 line_cache[0] = 0;
     170             :             }
     171             : 
     172             :             /* If the line doesn't start with a number, we assume it's part of
     173             :              * the payload, otherwise is likely an event number preceding the
     174             :              * timing information... but we can't be sure of this yet, so we
     175             :              * cache it */
     176        1685 :             if (strtol(line, &pline, 10) < 0 || line == pline)
     177        1092 :                 av_bprintf(&buf, "%s\n", line);
     178             :             else
     179         593 :                 strcpy(line_cache, line);
     180             :         } else {
     181         589 :             if (has_event_info) {
     182             :                 /* We have the information of previous event, append it to the
     183             :                  * queue. We insert the cached line if and only if the payload
     184             :                  * is empty and the cached line is not a standalone number. */
     185         582 :                 char *pline = NULL;
     186         582 :                 const int standalone_number = strtol(line_cache, &pline, 10) >= 0 && pline && !*pline;
     187         582 :                 res = add_event(&srt->q, &buf, line_cache, &ei, !buf.len && !standalone_number);
     188         582 :                 if (res < 0)
     189           0 :                     goto end;
     190             :             } else {
     191           7 :                 has_event_info = 1;
     192             :             }
     193         589 :             tmp_ei.pos = pos;
     194         589 :             ei = tmp_ei;
     195             :         }
     196             :     }
     197             : 
     198             :     /* Append the last event. Here we force the cache to be flushed, because a
     199             :      * trailing number is more likely to be geniune (for example a copyright
     200             :      * date) and not the event index of an inexistant event */
     201           7 :     if (has_event_info) {
     202           7 :         res = add_event(&srt->q, &buf, line_cache, &ei, 1);
     203           7 :         if (res < 0)
     204           0 :             goto end;
     205             :     }
     206             : 
     207           7 :     ff_subtitles_queue_finalize(s, &srt->q);
     208             : 
     209           7 : end:
     210           7 :     av_bprint_finalize(&buf, NULL);
     211           7 :     return res;
     212             : }
     213             : 
     214         592 : static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
     215             : {
     216         592 :     SRTContext *srt = s->priv_data;
     217         592 :     return ff_subtitles_queue_read_packet(&srt->q, pkt);
     218             : }
     219             : 
     220           0 : static int srt_read_seek(AVFormatContext *s, int stream_index,
     221             :                          int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
     222             : {
     223           0 :     SRTContext *srt = s->priv_data;
     224           0 :     return ff_subtitles_queue_seek(&srt->q, s, stream_index,
     225             :                                    min_ts, ts, max_ts, flags);
     226             : }
     227             : 
     228           7 : static int srt_read_close(AVFormatContext *s)
     229             : {
     230           7 :     SRTContext *srt = s->priv_data;
     231           7 :     ff_subtitles_queue_clean(&srt->q);
     232           7 :     return 0;
     233             : }
     234             : 
     235             : AVInputFormat ff_srt_demuxer = {
     236             :     .name        = "srt",
     237             :     .long_name   = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
     238             :     .priv_data_size = sizeof(SRTContext),
     239             :     .read_probe  = srt_probe,
     240             :     .read_header = srt_read_header,
     241             :     .read_packet = srt_read_packet,
     242             :     .read_seek2  = srt_read_seek,
     243             :     .read_close  = srt_read_close,
     244             : };

Generated by: LCOV version 1.13