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