yading@10
|
1 /*
|
yading@10
|
2 * 3GPP TS 26.245 Timed Text encoder
|
yading@10
|
3 * Copyright (c) 2012 Philip Langdale <philipl@overt.org>
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 #include <stdarg.h>
|
yading@10
|
23 #include "avcodec.h"
|
yading@10
|
24 #include "libavutil/avassert.h"
|
yading@10
|
25 #include "libavutil/avstring.h"
|
yading@10
|
26 #include "libavutil/intreadwrite.h"
|
yading@10
|
27 #include "ass_split.h"
|
yading@10
|
28 #include "ass.h"
|
yading@10
|
29
|
yading@10
|
30 typedef struct {
|
yading@10
|
31 ASSSplitContext *ass_ctx;
|
yading@10
|
32 char buffer[2048];
|
yading@10
|
33 char *ptr;
|
yading@10
|
34 char *end;
|
yading@10
|
35 } MovTextContext;
|
yading@10
|
36
|
yading@10
|
37
|
yading@10
|
38 static av_cold int mov_text_encode_init(AVCodecContext *avctx)
|
yading@10
|
39 {
|
yading@10
|
40 /*
|
yading@10
|
41 * For now, we'll use a fixed default style. When we add styling
|
yading@10
|
42 * support, this will be generated from the ASS style.
|
yading@10
|
43 */
|
yading@10
|
44 static uint8_t text_sample_entry[] = {
|
yading@10
|
45 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
|
yading@10
|
46 0x01, // int8_t horizontal-justification
|
yading@10
|
47 0xFF, // int8_t vertical-justification
|
yading@10
|
48 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
|
yading@10
|
49 // BoxRecord {
|
yading@10
|
50 0x00, 0x00, // int16_t top
|
yading@10
|
51 0x00, 0x00, // int16_t left
|
yading@10
|
52 0x00, 0x00, // int16_t bottom
|
yading@10
|
53 0x00, 0x00, // int16_t right
|
yading@10
|
54 // };
|
yading@10
|
55 // StyleRecord {
|
yading@10
|
56 0x00, 0x00, // uint16_t startChar
|
yading@10
|
57 0x00, 0x00, // uint16_t endChar
|
yading@10
|
58 0x00, 0x01, // uint16_t font-ID
|
yading@10
|
59 0x00, // uint8_t face-style-flags
|
yading@10
|
60 0x12, // uint8_t font-size
|
yading@10
|
61 0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
|
yading@10
|
62 // };
|
yading@10
|
63 // FontTableBox {
|
yading@10
|
64 0x00, 0x00, 0x00, 0x12, // uint32_t size
|
yading@10
|
65 'f', 't', 'a', 'b', // uint8_t name[4]
|
yading@10
|
66 0x00, 0x01, // uint16_t entry-count
|
yading@10
|
67 // FontRecord {
|
yading@10
|
68 0x00, 0x01, // uint16_t font-ID
|
yading@10
|
69 0x05, // uint8_t font-name-length
|
yading@10
|
70 'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
|
yading@10
|
71 // };
|
yading@10
|
72 // };
|
yading@10
|
73 };
|
yading@10
|
74
|
yading@10
|
75 MovTextContext *s = avctx->priv_data;
|
yading@10
|
76
|
yading@10
|
77 avctx->extradata_size = sizeof text_sample_entry;
|
yading@10
|
78 avctx->extradata = av_mallocz(avctx->extradata_size);
|
yading@10
|
79 if (!avctx->extradata)
|
yading@10
|
80 return AVERROR(ENOMEM);
|
yading@10
|
81
|
yading@10
|
82 memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size);
|
yading@10
|
83
|
yading@10
|
84 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
|
yading@10
|
85 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
|
yading@10
|
86 }
|
yading@10
|
87
|
yading@10
|
88 static void mov_text_text_cb(void *priv, const char *text, int len)
|
yading@10
|
89 {
|
yading@10
|
90 MovTextContext *s = priv;
|
yading@10
|
91 av_assert0(s->end >= s->ptr);
|
yading@10
|
92 av_strlcpy(s->ptr, text, FFMIN(s->end - s->ptr, len + 1));
|
yading@10
|
93 s->ptr += FFMIN(s->end - s->ptr, len);
|
yading@10
|
94 }
|
yading@10
|
95
|
yading@10
|
96 static void mov_text_new_line_cb(void *priv, int forced)
|
yading@10
|
97 {
|
yading@10
|
98 MovTextContext *s = priv;
|
yading@10
|
99 av_assert0(s->end >= s->ptr);
|
yading@10
|
100 av_strlcpy(s->ptr, "\n", FFMIN(s->end - s->ptr, 2));
|
yading@10
|
101 if (s->end > s->ptr)
|
yading@10
|
102 s->ptr++;
|
yading@10
|
103 }
|
yading@10
|
104
|
yading@10
|
105 static const ASSCodesCallbacks mov_text_callbacks = {
|
yading@10
|
106 .text = mov_text_text_cb,
|
yading@10
|
107 .new_line = mov_text_new_line_cb,
|
yading@10
|
108 };
|
yading@10
|
109
|
yading@10
|
110 static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
|
yading@10
|
111 int bufsize, const AVSubtitle *sub)
|
yading@10
|
112 {
|
yading@10
|
113 MovTextContext *s = avctx->priv_data;
|
yading@10
|
114 ASSDialog *dialog;
|
yading@10
|
115 int i, len, num;
|
yading@10
|
116
|
yading@10
|
117 s->ptr = s->buffer;
|
yading@10
|
118 s->end = s->ptr + sizeof(s->buffer);
|
yading@10
|
119
|
yading@10
|
120 for (i = 0; i < sub->num_rects; i++) {
|
yading@10
|
121
|
yading@10
|
122 if (sub->rects[i]->type != SUBTITLE_ASS) {
|
yading@10
|
123 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
|
yading@10
|
124 return AVERROR(ENOSYS);
|
yading@10
|
125 }
|
yading@10
|
126
|
yading@10
|
127 dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
|
yading@10
|
128 for (; dialog && num--; dialog++) {
|
yading@10
|
129 ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
|
yading@10
|
130 }
|
yading@10
|
131 }
|
yading@10
|
132
|
yading@10
|
133 if (s->ptr == s->buffer)
|
yading@10
|
134 return 0;
|
yading@10
|
135
|
yading@10
|
136 AV_WB16(buf, strlen(s->buffer));
|
yading@10
|
137 buf += 2;
|
yading@10
|
138
|
yading@10
|
139 len = av_strlcpy(buf, s->buffer, bufsize - 2);
|
yading@10
|
140
|
yading@10
|
141 if (len > bufsize-3) {
|
yading@10
|
142 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
|
yading@10
|
143 return AVERROR(EINVAL);
|
yading@10
|
144 }
|
yading@10
|
145
|
yading@10
|
146 return len + 2;
|
yading@10
|
147 }
|
yading@10
|
148
|
yading@10
|
149 static int mov_text_encode_close(AVCodecContext *avctx)
|
yading@10
|
150 {
|
yading@10
|
151 MovTextContext *s = avctx->priv_data;
|
yading@10
|
152 ff_ass_split_free(s->ass_ctx);
|
yading@10
|
153 return 0;
|
yading@10
|
154 }
|
yading@10
|
155
|
yading@10
|
156 AVCodec ff_movtext_encoder = {
|
yading@10
|
157 .name = "mov_text",
|
yading@10
|
158 .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
|
yading@10
|
159 .type = AVMEDIA_TYPE_SUBTITLE,
|
yading@10
|
160 .id = AV_CODEC_ID_MOV_TEXT,
|
yading@10
|
161 .priv_data_size = sizeof(MovTextContext),
|
yading@10
|
162 .init = mov_text_encode_init,
|
yading@10
|
163 .encode_sub = mov_text_encode_frame,
|
yading@10
|
164 .close = mov_text_encode_close,
|
yading@10
|
165 };
|