FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
samidec.c
Go to the documentation of this file.
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 decoder
24  * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx
25  */
26 
27 #include "ass.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/bprint.h"
30 #include "htmlsubtitles.h"
31 
32 typedef struct {
33  AVBPrint source;
34  AVBPrint content;
35  AVBPrint encoded_source;
36  AVBPrint encoded_content;
37  AVBPrint full;
38 } SAMIContext;
39 
40 static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
41 {
42  SAMIContext *sami = avctx->priv_data;
43  int ret = 0;
44  char *tag = NULL;
45  char *dupsrc = av_strdup(src);
46  char *p = dupsrc;
47  AVBPrint *dst_content = &sami->encoded_content;
48  AVBPrint *dst_source = &sami->encoded_source;
49 
51  av_bprint_clear(&sami->content);
53  for (;;) {
54  char *saveptr = NULL;
55  int prev_chr_is_space = 0;
56  AVBPrint *dst = &sami->content;
57 
58  /* parse & extract paragraph tag */
59  p = av_stristr(p, "<P");
60  if (!p)
61  break;
62  if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
63  p++;
64  continue;
65  }
66  if (dst->len) // add a separator with the previous paragraph if there was one
67  av_bprintf(dst, "\\N");
68  tag = av_strtok(p, ">", &saveptr);
69  if (!tag || !saveptr)
70  break;
71  p = saveptr;
72 
73  /* check if the current paragraph is the "source" (speaker name) */
74  if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
75  dst = &sami->source;
76  av_bprint_clear(dst);
77  }
78 
79  /* if empty event -> skip subtitle */
80  while (av_isspace(*p))
81  p++;
82  if (!strncmp(p, "&nbsp;", 6)) {
83  ret = -1;
84  goto end;
85  }
86 
87  /* extract the text, stripping most of the tags */
88  while (*p) {
89  if (*p == '<') {
90  if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
91  break;
92  }
93  if (!av_strncasecmp(p, "<BR", 3)) {
94  av_bprintf(dst, "\\N");
95  p++;
96  while (*p && *p != '>')
97  p++;
98  if (!*p)
99  break;
100  if (*p == '>')
101  p++;
102  continue;
103  }
104  if (!av_isspace(*p))
105  av_bprint_chars(dst, *p, 1);
106  else if (!prev_chr_is_space)
107  av_bprint_chars(dst, ' ', 1);
108  prev_chr_is_space = av_isspace(*p);
109  p++;
110  }
111  }
112 
113  av_bprint_clear(&sami->full);
114  if (sami->source.len) {
115  ff_htmlmarkup_to_ass(avctx, dst_source, sami->source.str);
116  av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str);
117  }
118  ff_htmlmarkup_to_ass(avctx, dst_content, sami->content.str);
119  av_bprintf(&sami->full, "%s", sami->encoded_content.str);
120 
121 end:
122  av_free(dupsrc);
123  return ret;
124 }
125 
127  void *data, int *got_sub_ptr, AVPacket *avpkt)
128 {
129  AVSubtitle *sub = data;
130  const char *ptr = avpkt->data;
131  SAMIContext *sami = avctx->priv_data;
132 
133  if (ptr && avpkt->size > 0 && !sami_paragraph_to_ass(avctx, ptr)) {
134  int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
135  int ts_duration = avpkt->duration != -1 ?
136  av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
137  int ret = ff_ass_add_rect_bprint(sub, &sami->full, ts_start, ts_duration);
138  if (ret < 0)
139  return ret;
140  }
141  *got_sub_ptr = sub->num_rects > 0;
142  return avpkt->size;
143 }
144 
145 static av_cold int sami_init(AVCodecContext *avctx)
146 {
147  SAMIContext *sami = avctx->priv_data;
148  av_bprint_init(&sami->source, 0, 2048);
149  av_bprint_init(&sami->content, 0, 2048);
150  av_bprint_init(&sami->encoded_source, 0, 2048);
151  av_bprint_init(&sami->encoded_content, 0, 2048);
152  av_bprint_init(&sami->full, 0, 2048);
153  return ff_ass_subtitle_header_default(avctx);
154 }
155 
157 {
158  SAMIContext *sami = avctx->priv_data;
159  av_bprint_finalize(&sami->source, NULL);
163  av_bprint_finalize(&sami->full, NULL);
164  return 0;
165 }
166 
168  .name = "sami",
169  .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
170  .type = AVMEDIA_TYPE_SUBTITLE,
171  .id = AV_CODEC_ID_SAMI,
172  .priv_data_size = sizeof(SAMIContext),
173  .init = sami_init,
174  .close = sami_close,
176 };
#define NULL
Definition: coverity.c:32
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
static int sami_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr, AVPacket *avpkt)
Definition: samidec.c:126
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
Definition: avstring.c:56
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1468
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:222
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:223
AVBPrint full
Definition: samidec.c:37
AVCodec.
Definition: avcodec.h:3392
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1661
#define av_cold
Definition: attributes.h:82
static av_cold int sami_init(AVCodecContext *avctx)
Definition: samidec.c:145
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1485
AVBPrint encoded_content
Definition: samidec.c:36
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition: ass.c:80
static av_cold int sami_close(AVCodecContext *avctx)
Definition: samidec.c:156
uint8_t * data
Definition: avcodec.h:1467
uint32_t tag
Definition: movenc.c:1348
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
#define src
Definition: vp9dsp.c:530
void ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
Definition: htmlsubtitles.c:53
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
int ff_ass_add_rect_bprint(AVSubtitle *sub, AVBPrint *buf, int ts_start, int duration)
Same as ff_ass_add_rect, but taking an AVBPrint buffer instead of a string, and assuming raw=0...
Definition: ass.c:181
main external API structure.
Definition: avcodec.h:1532
rational number numerator/denominator
Definition: rational.h:43
AVCodec ff_sami_decoder
Definition: samidec.c:167
AVBPrint source
Definition: samidec.c:33
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:184
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:572
void * priv_data
Definition: avcodec.h:1574
#define av_free(p)
AVBPrint encoded_source
Definition: samidec.c:35
static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
Definition: samidec.c:40
AVBPrint content
Definition: samidec.c:34
This structure stores compressed data.
Definition: avcodec.h:1444
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140