LCOV - code coverage report
Current view: top level - libavformat - sccdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 74 79 93.7 %
Date: 2017-12-18 06:23:41 Functions: 6 6 100.0 %

          Line data    Source code
       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 "internal.h"
      24             : #include "subtitles.h"
      25             : #include "libavutil/bprint.h"
      26             : #include "libavutil/intreadwrite.h"
      27             : 
      28             : typedef struct SCCContext {
      29             :     FFDemuxSubtitlesQueue q;
      30             : } SCCContext;
      31             : 
      32        6130 : static int scc_probe(AVProbeData *p)
      33             : {
      34             :     char buf[18];
      35             :     FFTextReader tr;
      36             : 
      37        6130 :     ff_text_init_buf(&tr, p->buf, p->buf_size);
      38             : 
      39       12267 :     while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
      40           7 :         ff_text_r8(&tr);
      41             : 
      42        6130 :     ff_text_read(&tr, buf, sizeof(buf));
      43             : 
      44        6130 :     if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
      45           1 :         return AVPROBE_SCORE_MAX;
      46             : 
      47        6129 :     return 0;
      48             : }
      49             : 
      50       11392 : static int convert(uint8_t x)
      51             : {
      52       11392 :     if (x >= 'a')
      53        2816 :         x -= 87;
      54        8576 :     else if (x >= 'A')
      55           0 :         x -= 55;
      56             :     else
      57        8576 :         x -= '0';
      58       11392 :     return x;
      59             : }
      60             : 
      61           1 : static int scc_read_header(AVFormatContext *s)
      62             : {
      63           1 :     SCCContext *scc = s->priv_data;
      64           1 :     AVStream *st = avformat_new_stream(s, NULL);
      65             :     char line[4096], line2[4096];
      66           1 :     int count = 0, ret = 0;
      67             :     ptrdiff_t len2, len;
      68             :     uint8_t out[4096];
      69             :     FFTextReader tr;
      70             : 
      71           1 :     ff_text_init_avio(s, &tr, s->pb);
      72             : 
      73           1 :     if (!st)
      74           0 :         return AVERROR(ENOMEM);
      75           1 :     avpriv_set_pts_info(st, 64, 1, 1000);
      76           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
      77           1 :     st->codecpar->codec_id   = AV_CODEC_ID_EIA_608;
      78             : 
      79         124 :     while (!ff_text_eof(&tr)) {
      80         122 :         const int64_t pos = ff_text_pos(&tr);
      81         122 :         char *saveptr = NULL, *lline;
      82             :         int hh1, mm1, ss1, fs1, i;
      83             :         int hh2, mm2, ss2, fs2;
      84             :         int64_t ts_start, ts_end;
      85             :         AVPacket *sub;
      86             : 
      87         122 :         if (count == 0) {
      88           5 :             while (!ff_text_eof(&tr)) {
      89           3 :                 len = ff_subtitles_read_line(&tr, line, sizeof(line));
      90           3 :                 if (len > 13)
      91           2 :                     break;
      92             :             }
      93             :         }
      94             : 
      95         122 :         if (!strncmp(line, "Scenarist_SCC V1.0", 18))
      96           3 :             continue;
      97         121 :         if (sscanf(line, "%d:%d:%d%*[:;]%d", &hh1, &mm1, &ss1, &fs1) != 4)
      98           0 :             continue;
      99             : 
     100         121 :         ts_start = (hh1 * 3600LL + mm1 * 60LL + ss1) * 1000LL + fs1 * 33;
     101             : 
     102         363 :         while (!ff_text_eof(&tr)) {
     103         241 :             len2 = ff_subtitles_read_line(&tr, line2, sizeof(line2));
     104         241 :             if (len2 > 13)
     105         120 :                 break;
     106             :         }
     107         121 :         if (sscanf(line2, "%d:%d:%d%*[:;]%d", &hh2, &mm2, &ss2, &fs2) != 4)
     108           1 :             continue;
     109             : 
     110         120 :         ts_end = (hh2 * 3600LL + mm2 * 60LL + ss2) * 1000LL + fs2 * 33;
     111         120 :         count++;
     112             : 
     113         120 :         lline = (char *)&line;
     114         120 :         lline += 12;
     115             : 
     116        5936 :         for (i = 0; i < 4095; i += 3) {
     117        2968 :             char *ptr = av_strtok(lline, " ", &saveptr);
     118             :             char c1, c2, c3, c4;
     119             : 
     120        2968 :             if (!ptr)
     121         240 :                 break;
     122             : 
     123        2848 :             if (sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
     124           0 :                 break;
     125             : 
     126        2848 :             lline = NULL;
     127        2848 :             out[i+0] = 0xfc;
     128        2848 :             out[i+1] = convert(c2) | (convert(c1) << 4);
     129        2848 :             out[i+2] = convert(c4) | (convert(c3) << 4);
     130             :         }
     131         120 :         out[i] = 0;
     132             : 
     133         120 :         sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
     134         120 :         if (!sub)
     135           0 :             return AVERROR(ENOMEM);
     136             : 
     137         120 :         sub->pos = pos;
     138         120 :         sub->pts = ts_start;
     139         120 :         sub->duration = FFMAX(1200, ts_end - ts_start);
     140         120 :         memmove(line, line2, sizeof(line));
     141         120 :         FFSWAP(ptrdiff_t, len, len2);
     142             :     }
     143             : 
     144           1 :     ff_subtitles_queue_finalize(s, &scc->q);
     145             : 
     146           1 :     return ret;
     147             : }
     148             : 
     149         105 : static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
     150             : {
     151         105 :     SCCContext *scc = s->priv_data;
     152         105 :     return ff_subtitles_queue_read_packet(&scc->q, pkt);
     153             : }
     154             : 
     155           1 : static int scc_read_seek(AVFormatContext *s, int stream_index,
     156             :                          int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
     157             : {
     158           1 :     SCCContext *scc = s->priv_data;
     159           1 :     return ff_subtitles_queue_seek(&scc->q, s, stream_index,
     160             :                                    min_ts, ts, max_ts, flags);
     161             : }
     162             : 
     163           1 : static int scc_read_close(AVFormatContext *s)
     164             : {
     165           1 :     SCCContext *scc = s->priv_data;
     166           1 :     ff_subtitles_queue_clean(&scc->q);
     167           1 :     return 0;
     168             : }
     169             : 
     170             : AVInputFormat ff_scc_demuxer = {
     171             :     .name           = "scc",
     172             :     .long_name      = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
     173             :     .priv_data_size = sizeof(SCCContext),
     174             :     .read_probe     = scc_probe,
     175             :     .read_header    = scc_read_header,
     176             :     .read_packet    = scc_read_packet,
     177             :     .read_seek2     = scc_read_seek,
     178             :     .read_close     = scc_read_close,
     179             :     .extensions     = "scc",
     180             : };

Generated by: LCOV version 1.13