FFmpeg
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 #include "internal.h"
32 
33 typedef struct {
34  AVBPrint source;
35  AVBPrint content;
36  AVBPrint encoded_source;
37  AVBPrint encoded_content;
38  AVBPrint full;
39  int readorder;
40 } SAMIContext;
41 
42 static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
43 {
44  SAMIContext *sami = avctx->priv_data;
45  int ret = 0;
46  char *tag = NULL;
47  char *dupsrc = av_strdup(src);
48  char *p = dupsrc;
49  AVBPrint *dst_content = &sami->encoded_content;
50  AVBPrint *dst_source = &sami->encoded_source;
51 
52  if (!dupsrc)
53  return AVERROR(ENOMEM);
54 
56  av_bprint_clear(&sami->content);
58  for (;;) {
59  char *saveptr = NULL;
60  int prev_chr_is_space = 0;
61  AVBPrint *dst = &sami->content;
62 
63  /* parse & extract paragraph tag */
64  p = av_stristr(p, "<P");
65  if (!p)
66  break;
67  if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
68  p++;
69  continue;
70  }
71  if (dst->len) // add a separator with the previous paragraph if there was one
72  av_bprintf(dst, "\\N");
73  tag = av_strtok(p, ">", &saveptr);
74  if (!tag || !saveptr)
75  break;
76  p = saveptr;
77 
78  /* check if the current paragraph is the "source" (speaker name) */
79  if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
80  dst = &sami->source;
81  av_bprint_clear(dst);
82  }
83 
84  /* if empty event -> skip subtitle */
85  while (av_isspace(*p))
86  p++;
87  if (!strncmp(p, "&nbsp;", 6)) {
88  ret = -1;
89  goto end;
90  }
91 
92  /* extract the text, stripping most of the tags */
93  while (*p) {
94  if (*p == '<') {
95  if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
96  break;
97  }
98  if (!av_strncasecmp(p, "<BR", 3)) {
99  av_bprintf(dst, "\\N");
100  p++;
101  while (*p && *p != '>')
102  p++;
103  if (!*p)
104  break;
105  if (*p == '>')
106  p++;
107  continue;
108  }
109  if (!av_isspace(*p))
110  av_bprint_chars(dst, *p, 1);
111  else if (!prev_chr_is_space)
112  av_bprint_chars(dst, ' ', 1);
113  prev_chr_is_space = av_isspace(*p);
114  p++;
115  }
116  }
117 
118  av_bprint_clear(&sami->full);
119  if (sami->source.len) {
120  ret = ff_htmlmarkup_to_ass(avctx, dst_source, sami->source.str);
121  if (ret < 0)
122  goto end;
123  av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str);
124  }
125  ret = ff_htmlmarkup_to_ass(avctx, dst_content, sami->content.str);
126  if (ret < 0)
127  goto end;
128  av_bprintf(&sami->full, "%s", sami->encoded_content.str);
129 
130 end:
131  av_free(dupsrc);
132  return ret;
133 }
134 
136  void *data, int *got_sub_ptr, AVPacket *avpkt)
137 {
138  AVSubtitle *sub = data;
139  const char *ptr = avpkt->data;
140  SAMIContext *sami = avctx->priv_data;
141 
142  if (ptr && avpkt->size > 0) {
143  int ret = sami_paragraph_to_ass(avctx, ptr);
144  if (ret < 0)
145  return ret;
146  // TODO: pass escaped sami->encoded_source.str as source
147  ret = ff_ass_add_rect(sub, sami->full.str, sami->readorder++, 0, NULL, NULL);
148  if (ret < 0)
149  return ret;
150  }
151  *got_sub_ptr = sub->num_rects > 0;
152  return avpkt->size;
153 }
154 
155 static av_cold int sami_init(AVCodecContext *avctx)
156 {
157  SAMIContext *sami = avctx->priv_data;
158  av_bprint_init(&sami->source, 0, 2048);
159  av_bprint_init(&sami->content, 0, 2048);
160  av_bprint_init(&sami->encoded_source, 0, 2048);
161  av_bprint_init(&sami->encoded_content, 0, 2048);
162  av_bprint_init(&sami->full, 0, 2048);
163  return ff_ass_subtitle_header_default(avctx);
164 }
165 
167 {
168  SAMIContext *sami = avctx->priv_data;
169  av_bprint_finalize(&sami->source, NULL);
173  av_bprint_finalize(&sami->full, NULL);
174  return 0;
175 }
176 
177 static void sami_flush(AVCodecContext *avctx)
178 {
179  SAMIContext *sami = avctx->priv_data;
180  if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
181  sami->readorder = 0;
182 }
183 
185  .name = "sami",
186  .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
187  .type = AVMEDIA_TYPE_SUBTITLE,
188  .id = AV_CODEC_ID_SAMI,
189  .priv_data_size = sizeof(SAMIContext),
190  .init = sami_init,
191  .close = sami_close,
193  .flush = sami_flush,
194  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
195 };
AVSubtitle
Definition: avcodec.h:2289
AVCodec
AVCodec.
Definition: codec.h:202
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_ass_subtitle_header_default
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition: ass.c:96
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:234
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:68
av_stristr
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
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:31
ff_sami_decoder
const AVCodec ff_sami_decoder
Definition: samidec.c:184
htmlsubtitles.h
sami_decode_frame
static int sami_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr, AVPacket *avpkt)
Definition: samidec.c:135
av_isspace
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:227
ff_ass_add_rect
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:117
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
data
const char data[16]
Definition: mxf.c:143
SAMIContext::encoded_content
AVBPrint encoded_content
Definition: samidec.c:37
SAMIContext::source
AVBPrint source
Definition: samidec.c:34
SAMIContext::encoded_source
AVBPrint encoded_source
Definition: samidec.c:36
init
static int init
Definition: av_tx.c:47
sami_close
static av_cold int sami_close(AVCodecContext *avctx)
Definition: samidec.c:166
ass.h
sami_init
static av_cold int sami_init(AVCodecContext *avctx)
Definition: samidec.c:155
av_cold
#define av_cold
Definition: attributes.h:90
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
sami_flush
static void sami_flush(AVCodecContext *avctx)
Definition: samidec.c:177
av_strtok
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:186
SAMIContext::content
AVBPrint content
Definition: samidec.c:35
SAMIContext::full
AVBPrint full
Definition: samidec.c:38
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
NULL
#define NULL
Definition: coverity.c:32
src
#define src
Definition: vp8dsp.c:255
av_strncasecmp
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:225
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:470
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
bprint.h
AV_CODEC_ID_SAMI
@ AV_CODEC_ID_SAMI
Definition: codec_id.h:534
ff_htmlmarkup_to_ass
int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
Definition: htmlsubtitles.c:129
SAMIContext::readorder
int readorder
Definition: samidec.c:39
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
tag
uint32_t tag
Definition: movenc.c:1596
ret
ret
Definition: filter_design.txt:187
SAMIContext
Definition: samidec.c:33
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:93
sami_paragraph_to_ass
static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
Definition: samidec.c:42
AVCodecContext
main external API structure.
Definition: avcodec.h:383
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:226
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:279
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
avstring.h
AV_CODEC_FLAG2_RO_FLUSH_NOOP
#define AV_CODEC_FLAG2_RO_FLUSH_NOOP
Do not reset ASS ReadOrder field on flush (subtitles decoding)
Definition: avcodec.h:327
av_bprint_chars
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:139