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 
31 typedef struct {
32  AVBPrint source;
33  AVBPrint content;
34  AVBPrint full;
35 } SAMIContext;
36 
37 static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
38 {
39  SAMIContext *sami = avctx->priv_data;
40  int ret = 0;
41  char *tag = NULL;
42  char *dupsrc = av_strdup(src);
43  char *p = dupsrc;
44 
45  av_bprint_clear(&sami->content);
46  for (;;) {
47  char *saveptr = NULL;
48  int prev_chr_is_space = 0;
49  AVBPrint *dst = &sami->content;
50 
51  /* parse & extract paragraph tag */
52  p = av_stristr(p, "<P");
53  if (!p)
54  break;
55  if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
56  p++;
57  continue;
58  }
59  if (dst->len) // add a separator with the previous paragraph if there was one
60  av_bprintf(dst, "\\N");
61  tag = av_strtok(p, ">", &saveptr);
62  if (!tag || !saveptr)
63  break;
64  p = saveptr;
65 
66  /* check if the current paragraph is the "source" (speaker name) */
67  if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
68  dst = &sami->source;
69  av_bprint_clear(dst);
70  }
71 
72  /* if empty event -> skip subtitle */
73  while (av_isspace(*p))
74  p++;
75  if (!strncmp(p, "&nbsp;", 6)) {
76  ret = -1;
77  goto end;
78  }
79 
80  /* extract the text, stripping most of the tags */
81  while (*p) {
82  if (*p == '<') {
83  if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
84  break;
85  if (!av_strncasecmp(p, "<BR", 3))
86  av_bprintf(dst, "\\N");
87  p++;
88  while (*p && *p != '>')
89  p++;
90  if (!*p)
91  break;
92  if (*p == '>')
93  p++;
94  continue;
95  }
96  if (!av_isspace(*p))
97  av_bprint_chars(dst, *p, 1);
98  else if (!prev_chr_is_space)
99  av_bprint_chars(dst, ' ', 1);
100  prev_chr_is_space = av_isspace(*p);
101  p++;
102  }
103  }
104 
105  av_bprint_clear(&sami->full);
106  if (sami->source.len)
107  av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->source.str);
108  av_bprintf(&sami->full, "%s", sami->content.str);
109 
110 end:
111  av_free(dupsrc);
112  return ret;
113 }
114 
116  void *data, int *got_sub_ptr, AVPacket *avpkt)
117 {
118  AVSubtitle *sub = data;
119  const char *ptr = avpkt->data;
120  SAMIContext *sami = avctx->priv_data;
121 
122  if (ptr && avpkt->size > 0 && !sami_paragraph_to_ass(avctx, ptr)) {
123  int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
124  int ts_duration = avpkt->duration != -1 ?
125  av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
126  int ret = ff_ass_add_rect_bprint(sub, &sami->full, ts_start, ts_duration);
127  if (ret < 0)
128  return ret;
129  }
130  *got_sub_ptr = sub->num_rects > 0;
131  return avpkt->size;
132 }
133 
134 static av_cold int sami_init(AVCodecContext *avctx)
135 {
136  SAMIContext *sami = avctx->priv_data;
137  av_bprint_init(&sami->source, 0, 2048);
138  av_bprint_init(&sami->content, 0, 2048);
139  av_bprint_init(&sami->full, 0, 2048);
140  return ff_ass_subtitle_header_default(avctx);
141 }
142 
144 {
145  SAMIContext *sami = avctx->priv_data;
146  av_bprint_finalize(&sami->source, NULL);
148  av_bprint_finalize(&sami->full, NULL);
149  return 0;
150 }
151 
153  .name = "sami",
154  .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
155  .type = AVMEDIA_TYPE_SUBTITLE,
156  .id = AV_CODEC_ID_SAMI,
157  .priv_data_size = sizeof(SAMIContext),
158  .init = sami_init,
159  .close = sami_close,
161 };
#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:115
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:1163
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:34
AVCodec.
Definition: avcodec.h:3181
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:1369
#define av_cold
Definition: attributes.h:74
static av_cold int sami_init(AVCodecContext *avctx)
Definition: samidec.c:134
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.c:330
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
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:143
uint8_t * data
Definition: avcodec.h:1162
uint32_t tag
Definition: movenc.c:1333
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1180
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
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:3188
ret
Definition: avfilter.c:974
AVS_Value src
Definition: avisynth_c.h:482
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:265
int ff_ass_add_rect_bprint(AVSubtitle *sub, AVBPrint *buf, int ts_start, int duration)
Same as ff_ass_add_rect_bprint, but taking an AVBPrint buffer instead of a string, and assuming raw=0.
Definition: ass.c:178
main external API structure.
Definition: avcodec.h:1241
rational number numerator/denominator
Definition: rational.h:43
AVCodec ff_sami_decoder
Definition: samidec.c:152
AVBPrint source
Definition: samidec.c:32
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:522
void * priv_data
Definition: avcodec.h:1283
#define av_free(p)
static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
Definition: samidec.c:37
AVBPrint content
Definition: samidec.c:33
This structure stores compressed data.
Definition: avcodec.h:1139
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140