LCOV - code coverage report
Current view: top level - src/libavformat - assenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 101 115 87.8 %
Date: 2017-01-24 04:42:20 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * SSA/ASS muxer
       3             :  * Copyright (c) 2008 Michael Niedermayer
       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 "libavutil/avstring.h"
      23             : #include "avformat.h"
      24             : #include "internal.h"
      25             : 
      26             : #include "libavutil/opt.h"
      27             : 
      28             : typedef struct DialogueLine {
      29             :     int readorder;
      30             :     char *line;
      31             :     struct DialogueLine *prev, *next;
      32             : } DialogueLine;
      33             : 
      34             : typedef struct ASSContext {
      35             :     const AVClass *class;
      36             :     int expected_readorder;
      37             :     DialogueLine *dialogue_cache;
      38             :     DialogueLine *last_added_dialogue;
      39             :     int cache_size;
      40             :     int ssa_mode;
      41             :     int ignore_readorder;
      42             :     uint8_t *trailer;
      43             :     size_t trailer_size;
      44             : } ASSContext;
      45             : 
      46          23 : static int write_header(AVFormatContext *s)
      47             : {
      48          23 :     ASSContext *ass = s->priv_data;
      49          23 :     AVCodecParameters *par = s->streams[0]->codecpar;
      50             : 
      51          23 :     if (s->nb_streams != 1 || par->codec_id != AV_CODEC_ID_ASS) {
      52           0 :         av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
      53           0 :         return AVERROR(EINVAL);
      54             :     }
      55          23 :     avpriv_set_pts_info(s->streams[0], 64, 1, 100);
      56          23 :     if (par->extradata_size > 0) {
      57          23 :         size_t header_size = par->extradata_size;
      58          23 :         uint8_t *trailer = strstr(par->extradata, "\n[Events]");
      59             : 
      60          23 :         if (trailer)
      61          23 :             trailer = strstr(trailer, "Format:");
      62          23 :         if (trailer)
      63          23 :             trailer = strstr(trailer, "\n");
      64             : 
      65          23 :         if (trailer++) {
      66          23 :             header_size = (trailer - par->extradata);
      67          23 :             ass->trailer_size = par->extradata_size - header_size;
      68          23 :             if (ass->trailer_size)
      69           2 :                 ass->trailer = trailer;
      70             :         }
      71             : 
      72          23 :         avio_write(s->pb, par->extradata, header_size);
      73          23 :         if (par->extradata[header_size - 1] != '\n')
      74           0 :             avio_write(s->pb, "\r\n", 2);
      75          23 :         ass->ssa_mode = !strstr(par->extradata, "\n[V4+ Styles]");
      76          23 :         if (!strstr(par->extradata, "\n[Events]"))
      77           0 :             avio_printf(s->pb, "[Events]\r\nFormat: %s, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
      78           0 :                         ass->ssa_mode ? "Marked" : "Layer");
      79             :     }
      80          23 :     avio_flush(s->pb);
      81             : 
      82          23 :     return 0;
      83             : }
      84             : 
      85         425 : static void purge_dialogues(AVFormatContext *s, int force)
      86             : {
      87         425 :     int n = 0;
      88         425 :     ASSContext *ass = s->priv_data;
      89         425 :     DialogueLine *dialogue = ass->dialogue_cache;
      90             : 
      91        1252 :     while (dialogue && (dialogue->readorder == ass->expected_readorder || force)) {
      92         402 :         DialogueLine *next = dialogue->next;
      93         402 :         if (dialogue->readorder != ass->expected_readorder) {
      94           0 :             av_log(s, AV_LOG_WARNING, "ReadOrder gap found between %d and %d\n",
      95             :                    ass->expected_readorder, dialogue->readorder);
      96           0 :             ass->expected_readorder = dialogue->readorder;
      97             :         }
      98         402 :         avio_printf(s->pb, "Dialogue: %s\r\n", dialogue->line);
      99         402 :         if (dialogue == ass->last_added_dialogue)
     100         402 :             ass->last_added_dialogue = next;
     101         402 :         av_freep(&dialogue->line);
     102         402 :         av_free(dialogue);
     103         402 :         if (next)
     104          27 :             next->prev = NULL;
     105         402 :         dialogue = ass->dialogue_cache = next;
     106         402 :         ass->expected_readorder++;
     107         402 :         n++;
     108             :     }
     109         425 :     ass->cache_size -= n;
     110         425 :     if (n > 1)
     111           2 :         av_log(s, AV_LOG_DEBUG, "wrote %d ASS lines, cached dialogues: %d, waiting for event id %d\n",
     112             :                n, ass->cache_size, ass->expected_readorder);
     113         425 : }
     114             : 
     115         402 : static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
     116             : {
     117         402 :     DialogueLine *cur, *next = NULL, *prev = NULL;
     118             : 
     119             :     /* from the last added to the end of the list */
     120         402 :     if (ass->last_added_dialogue) {
     121          50 :         for (cur = ass->last_added_dialogue; cur; cur = cur->next) {
     122          36 :             if (cur->readorder > dialogue->readorder)
     123          13 :                 break;
     124          23 :             prev = cur;
     125          23 :             next = cur->next;
     126             :         }
     127             :     }
     128             : 
     129             :     /* from the beginning to the last one added */
     130         402 :     if (!prev) {
     131         388 :         next = ass->dialogue_cache;
     132         388 :         for (cur = next; cur != ass->last_added_dialogue; cur = cur->next) {
     133           4 :             if (cur->readorder > dialogue->readorder)
     134           4 :                 break;
     135           0 :             prev = cur;
     136           0 :             next = cur->next;
     137             :         }
     138             :     }
     139             : 
     140         402 :     if (prev) {
     141          14 :         prev->next = dialogue;
     142          14 :         dialogue->prev = prev;
     143             :     } else {
     144         388 :         dialogue->prev = ass->dialogue_cache;
     145         388 :         ass->dialogue_cache = dialogue;
     146             :     }
     147         402 :     if (next) {
     148          13 :         next->prev = dialogue;
     149          13 :         dialogue->next = next;
     150             :     }
     151         402 :     ass->cache_size++;
     152         402 :     ass->last_added_dialogue = dialogue;
     153         402 : }
     154             : 
     155         402 : static int write_packet(AVFormatContext *s, AVPacket *pkt)
     156             : {
     157         402 :     ASSContext *ass = s->priv_data;
     158             : 
     159             :     long int layer;
     160         402 :     char *p = pkt->data;
     161         402 :     int64_t start = pkt->pts;
     162         402 :     int64_t end   = start + pkt->duration;
     163             :     int hh1, mm1, ss1, ms1;
     164             :     int hh2, mm2, ss2, ms2;
     165         402 :     DialogueLine *dialogue = av_mallocz(sizeof(*dialogue));
     166             : 
     167         402 :     if (!dialogue)
     168           0 :         return AVERROR(ENOMEM);
     169             : 
     170         402 :     dialogue->readorder = strtol(p, &p, 10);
     171         402 :     if (dialogue->readorder < ass->expected_readorder)
     172           0 :         av_log(s, AV_LOG_WARNING, "Unexpected ReadOrder %d\n",
     173             :                dialogue->readorder);
     174         402 :     if (*p == ',')
     175         402 :         p++;
     176             : 
     177         402 :     if (ass->ssa_mode && !strncmp(p, "Marked=", 7))
     178           0 :         p += 7;
     179             : 
     180         402 :     layer = strtol(p, &p, 10);
     181         402 :     if (*p == ',')
     182         402 :         p++;
     183         402 :     hh1 = (int)(start / 360000);    mm1 = (int)(start / 6000) % 60;
     184         402 :     hh2 = (int)(end   / 360000);    mm2 = (int)(end   / 6000) % 60;
     185         402 :     ss1 = (int)(start / 100) % 60;  ms1 = (int)(start % 100);
     186         402 :     ss2 = (int)(end   / 100) % 60;  ms2 = (int)(end   % 100);
     187         402 :     if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
     188         402 :     if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
     189             : 
     190         804 :     dialogue->line = av_asprintf("%s%ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
     191         402 :                                  ass->ssa_mode ? "Marked=" : "",
     192             :                                  layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
     193         402 :     if (!dialogue->line) {
     194           0 :         av_free(dialogue);
     195           0 :         return AVERROR(ENOMEM);
     196             :     }
     197         402 :     insert_dialogue(ass, dialogue);
     198         402 :     purge_dialogues(s, ass->ignore_readorder);
     199             : 
     200         402 :     return 0;
     201             : }
     202             : 
     203          23 : static int write_trailer(AVFormatContext *s)
     204             : {
     205          23 :     ASSContext *ass = s->priv_data;
     206             : 
     207          23 :     purge_dialogues(s, 1);
     208             : 
     209          23 :     if (ass->trailer) {
     210           2 :         avio_write(s->pb, ass->trailer, ass->trailer_size);
     211             :     }
     212             : 
     213          23 :     return 0;
     214             : }
     215             : 
     216             : #define OFFSET(x) offsetof(ASSContext, x)
     217             : #define E AV_OPT_FLAG_ENCODING_PARAM
     218             : static const AVOption options[] = {
     219             :     { "ignore_readorder", "write events immediately, even if they're out-of-order", OFFSET(ignore_readorder), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
     220             :     { NULL },
     221             : };
     222             : 
     223             : static const AVClass ass_class = {
     224             :     .class_name = "ass muxer",
     225             :     .item_name  = av_default_item_name,
     226             :     .option     = options,
     227             :     .version    = LIBAVUTIL_VERSION_INT,
     228             : };
     229             : 
     230             : AVOutputFormat ff_ass_muxer = {
     231             :     .name           = "ass",
     232             :     .long_name      = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
     233             :     .mime_type      = "text/x-ass",
     234             :     .extensions     = "ass,ssa",
     235             :     .priv_data_size = sizeof(ASSContext),
     236             :     .subtitle_codec = AV_CODEC_ID_ASS,
     237             :     .write_header   = write_header,
     238             :     .write_packet   = write_packet,
     239             :     .write_trailer  = write_trailer,
     240             :     .flags          = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT,
     241             :     .priv_class     = &ass_class,
     242             : };

Generated by: LCOV version 1.12