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  int readorder;
39 } SAMIContext;
40 
41 static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
42 {
43  SAMIContext *sami = avctx->priv_data;
44  int ret = 0;
45  char *tag = NULL;
46  char *dupsrc = av_strdup(src);
47  char *p = dupsrc;
48  AVBPrint *dst_content = &sami->encoded_content;
49  AVBPrint *dst_source = &sami->encoded_source;
50 
52  av_bprint_clear(&sami->content);
54  for (;;) {
55  char *saveptr = NULL;
56  int prev_chr_is_space = 0;
57  AVBPrint *dst = &sami->content;
58 
59  /* parse & extract paragraph tag */
60  p = av_stristr(p, "<P");
61  if (!p)
62  break;
63  if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
64  p++;
65  continue;
66  }
67  if (dst->len) // add a separator with the previous paragraph if there was one
68  av_bprintf(dst, "\\N");
69  tag = av_strtok(p, ">", &saveptr);
70  if (!tag || !saveptr)
71  break;
72  p = saveptr;
73 
74  /* check if the current paragraph is the "source" (speaker name) */
75  if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
76  dst = &sami->source;
77  av_bprint_clear(dst);
78  }
79 
80  /* if empty event -> skip subtitle */
81  while (av_isspace(*p))
82  p++;
83  if (!strncmp(p, "&nbsp;", 6)) {
84  ret = -1;
85  goto end;
86  }
87 
88  /* extract the text, stripping most of the tags */
89  while (*p) {
90  if (*p == '<') {
91  if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
92  break;
93  }
94  if (!av_strncasecmp(p, "<BR", 3)) {
95  av_bprintf(dst, "\\N");
96  p++;
97  while (*p && *p != '>')
98  p++;
99  if (!*p)
100  break;
101  if (*p == '>')
102  p++;
103  continue;
104  }
105  if (!av_isspace(*p))
106  av_bprint_chars(dst, *p, 1);
107  else if (!prev_chr_is_space)
108  av_bprint_chars(dst, ' ', 1);
109  prev_chr_is_space = av_isspace(*p);
110  p++;
111  }
112  }
113 
114  av_bprint_clear(&sami->full);
115  if (sami->source.len) {
116  ret = ff_htmlmarkup_to_ass(avctx, dst_source, sami->source.str);
117  if (ret < 0)
118  goto end;
119  av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str);
120  }
121  ret = ff_htmlmarkup_to_ass(avctx, dst_content, sami->content.str);
122  if (ret < 0)
123  goto end;
124  av_bprintf(&sami->full, "%s", sami->encoded_content.str);
125 
126 end:
127  av_free(dupsrc);
128  return ret;
129 }
130 
132  void *data, int *got_sub_ptr, AVPacket *avpkt)
133 {
134  AVSubtitle *sub = data;
135  const char *ptr = avpkt->data;
136  SAMIContext *sami = avctx->priv_data;
137 
138  if (ptr && avpkt->size > 0 && !sami_paragraph_to_ass(avctx, ptr)) {
139  // TODO: pass escaped sami->encoded_source.str as source
140  int ret = ff_ass_add_rect(sub, sami->full.str, sami->readorder++, 0, NULL, NULL);
141  if (ret < 0)
142  return ret;
143  }
144  *got_sub_ptr = sub->num_rects > 0;
145  return avpkt->size;
146 }
147 
148 static av_cold int sami_init(AVCodecContext *avctx)
149 {
150  SAMIContext *sami = avctx->priv_data;
151  av_bprint_init(&sami->source, 0, 2048);
152  av_bprint_init(&sami->content, 0, 2048);
153  av_bprint_init(&sami->encoded_source, 0, 2048);
154  av_bprint_init(&sami->encoded_content, 0, 2048);
155  av_bprint_init(&sami->full, 0, 2048);
156  return ff_ass_subtitle_header_default(avctx);
157 }
158 
160 {
161  SAMIContext *sami = avctx->priv_data;
162  av_bprint_finalize(&sami->source, NULL);
166  av_bprint_finalize(&sami->full, NULL);
167  return 0;
168 }
169 
170 static void sami_flush(AVCodecContext *avctx)
171 {
172  SAMIContext *sami = avctx->priv_data;
173  if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
174  sami->readorder = 0;
175 }
176 
178  .name = "sami",
179  .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
180  .type = AVMEDIA_TYPE_SUBTITLE,
181  .id = AV_CODEC_ID_SAMI,
182  .priv_data_size = sizeof(SAMIContext),
183  .init = sami_init,
184  .close = sami_close,
186  .flush = sami_flush,
187 };
#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:131
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
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:1680
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
unsigned num_rects
Definition: avcodec.h:4132
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int readorder, int layer, const char *style, const char *speaker)
Add an ASS dialog to a subtitle.
Definition: ass.c:101
AVBPrint full
Definition: samidec.c:37
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3739
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
#define av_cold
Definition: attributes.h:82
static av_cold int sami_init(AVCodecContext *avctx)
Definition: samidec.c:148
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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:159
uint8_t * data
Definition: avcodec.h:1679
static void sami_flush(AVCodecContext *avctx)
Definition: samidec.c:170
uint32_t tag
Definition: movenc.c:1409
#define AV_CODEC_FLAG2_RO_FLUSH_NOOP
Do not reset ASS ReadOrder field on flush (subtitles decoding)
Definition: avcodec.h:984
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
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:3746
int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
Definition: htmlsubtitles.c:82
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:237
main external API structure.
Definition: avcodec.h:1761
int readorder
Definition: samidec.c:38
AVCodec ff_sami_decoder
Definition: samidec.c:177
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
void * priv_data
Definition: avcodec.h:1803
#define av_free(p)
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1863
AVBPrint encoded_source
Definition: samidec.c:35
static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
Definition: samidec.c:41
AVBPrint content
Definition: samidec.c:34
This structure stores compressed data.
Definition: avcodec.h:1656
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140