LCOV - code coverage report
Current view: top level - libavformat - srtenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 42 71.4 %
Date: 2017-12-14 08:27:08 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * SubRip subtitle muxer
       3             :  * Copyright (c) 2012  Nicolas George <nicolas.george@normalesup.org>
       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 "libavutil/log.h"
      25             : #include "libavutil/intreadwrite.h"
      26             : 
      27             : /* TODO: add options for:
      28             :    - character encoding;
      29             :    - LF / CRLF;
      30             :    - byte order mark.
      31             :  */
      32             : 
      33             : typedef struct SRTContext{
      34             :     unsigned index;
      35             : } SRTContext;
      36             : 
      37           5 : static int srt_write_header(AVFormatContext *avf)
      38             : {
      39           5 :     SRTContext *srt = avf->priv_data;
      40             : 
      41          10 :     if (avf->nb_streams != 1 ||
      42           5 :         avf->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
      43           0 :         av_log(avf, AV_LOG_ERROR,
      44             :                "SRT supports only a single subtitles stream.\n");
      45           0 :         return AVERROR(EINVAL);
      46             :     }
      47           9 :     if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT &&
      48           4 :         avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP) {
      49           0 :         av_log(avf, AV_LOG_ERROR,
      50             :                "Unsupported subtitles codec: %s\n",
      51           0 :                avcodec_get_name(avf->streams[0]->codecpar->codec_id));
      52           0 :         return AVERROR(EINVAL);
      53             :     }
      54           5 :     avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
      55           5 :     srt->index = 1;
      56           5 :     return 0;
      57             : }
      58             : 
      59         504 : static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
      60             : {
      61         504 :     SRTContext *srt = avf->priv_data;
      62             : 
      63         504 :     int64_t s = pkt->pts, e, d = pkt->duration;
      64         504 :     int size, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
      65             :     const uint8_t *p;
      66             : 
      67         504 :     p = av_packet_get_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
      68         504 :     if (p && size == 16) {
      69           0 :         x1 = AV_RL32(p     );
      70           0 :         y1 = AV_RL32(p +  4);
      71           0 :         x2 = AV_RL32(p +  8);
      72           0 :         y2 = AV_RL32(p + 12);
      73             :     }
      74             : 
      75             : #if FF_API_CONVERGENCE_DURATION
      76             : FF_DISABLE_DEPRECATION_WARNINGS
      77         504 :     if (d <= 0)
      78             :         /* For backward compatibility, fallback to convergence_duration. */
      79           2 :         d = pkt->convergence_duration;
      80             : FF_ENABLE_DEPRECATION_WARNINGS
      81             : #endif
      82         504 :     if (s == AV_NOPTS_VALUE || d < 0) {
      83           0 :         av_log(avf, AV_LOG_WARNING,
      84             :                "Insufficient timestamps in event number %d.\n", srt->index);
      85           0 :         return 0;
      86             :     }
      87         504 :     e = s + d;
      88        4032 :     avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
      89             :                    srt->index,
      90        1008 :                    (int)(s / 3600000),      (int)(s / 60000) % 60,
      91        1008 :                    (int)(s /    1000) % 60, (int)(s %  1000),
      92        1008 :                    (int)(e / 3600000),      (int)(e / 60000) % 60,
      93        1008 :                    (int)(e /    1000) % 60, (int)(e %  1000));
      94         504 :     if (p)
      95           0 :         avio_printf(avf->pb, "  X1:%03d X2:%03d Y1:%03d Y2:%03d",
      96             :                     x1, x2, y1, y2);
      97         504 :     avio_printf(avf->pb, "\n");
      98             : 
      99         504 :     avio_write(avf->pb, pkt->data, pkt->size);
     100         504 :     avio_write(avf->pb, "\n\n", 2);
     101         504 :     srt->index++;
     102         504 :     return 0;
     103             : }
     104             : 
     105             : AVOutputFormat ff_srt_muxer = {
     106             :     .name           = "srt",
     107             :     .long_name      = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
     108             :     .mime_type      = "application/x-subrip",
     109             :     .extensions     = "srt",
     110             :     .priv_data_size = sizeof(SRTContext),
     111             :     .write_header   = srt_write_header,
     112             :     .write_packet   = srt_write_packet,
     113             :     .flags          = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
     114             :     .subtitle_codec = AV_CODEC_ID_SUBRIP,
     115             : };

Generated by: LCOV version 1.13