yading@10
|
1 /*
|
yading@10
|
2 * DivX (XSUB) subtitle encoder
|
yading@10
|
3 * Copyright (c) 2005 DivX, Inc.
|
yading@10
|
4 * Copyright (c) 2009 Bjorn Axelsson
|
yading@10
|
5 *
|
yading@10
|
6 * This file is part of FFmpeg.
|
yading@10
|
7 *
|
yading@10
|
8 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
9 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
10 * License as published by the Free Software Foundation; either
|
yading@10
|
11 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
12 *
|
yading@10
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
16 * Lesser General Public License for more details.
|
yading@10
|
17 *
|
yading@10
|
18 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
19 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
21 */
|
yading@10
|
22
|
yading@10
|
23 #include "avcodec.h"
|
yading@10
|
24 #include "bytestream.h"
|
yading@10
|
25 #include "put_bits.h"
|
yading@10
|
26
|
yading@10
|
27 /**
|
yading@10
|
28 * Number of pixels to pad left and right.
|
yading@10
|
29 *
|
yading@10
|
30 * The official encoder pads the subtitles with two pixels on either side,
|
yading@10
|
31 * but until we find out why, we won't do it (we will pad to have width
|
yading@10
|
32 * divisible by 2 though).
|
yading@10
|
33 */
|
yading@10
|
34 #define PADDING 0
|
yading@10
|
35 #define PADDING_COLOR 0
|
yading@10
|
36
|
yading@10
|
37 /**
|
yading@10
|
38 * Encode a single color run. At most 16 bits will be used.
|
yading@10
|
39 * @param len length of the run, values > 255 mean "until end of line", may not be < 0.
|
yading@10
|
40 * @param color color to encode, only the lowest two bits are used and all others must be 0.
|
yading@10
|
41 */
|
yading@10
|
42 static void put_xsub_rle(PutBitContext *pb, int len, int color)
|
yading@10
|
43 {
|
yading@10
|
44 if (len <= 255)
|
yading@10
|
45 put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len);
|
yading@10
|
46 else
|
yading@10
|
47 put_bits(pb, 14, 0);
|
yading@10
|
48 put_bits(pb, 2, color);
|
yading@10
|
49 }
|
yading@10
|
50
|
yading@10
|
51 /**
|
yading@10
|
52 * Encode a 4-color bitmap with XSUB rle.
|
yading@10
|
53 *
|
yading@10
|
54 * The encoded bitmap may be wider than the source bitmap due to padding.
|
yading@10
|
55 */
|
yading@10
|
56 static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap,
|
yading@10
|
57 int linesize, int w, int h)
|
yading@10
|
58 {
|
yading@10
|
59 int x0, x1, y, len, color = PADDING_COLOR;
|
yading@10
|
60
|
yading@10
|
61 for (y = 0; y < h; y++) {
|
yading@10
|
62 x0 = 0;
|
yading@10
|
63 while (x0 < w) {
|
yading@10
|
64 // Make sure we have enough room for at least one run and padding
|
yading@10
|
65 if (pb->size_in_bits - put_bits_count(pb) < 7*8)
|
yading@10
|
66 return -1;
|
yading@10
|
67
|
yading@10
|
68 x1 = x0;
|
yading@10
|
69 color = bitmap[x1++] & 3;
|
yading@10
|
70 while (x1 < w && (bitmap[x1] & 3) == color)
|
yading@10
|
71 x1++;
|
yading@10
|
72 len = x1 - x0;
|
yading@10
|
73 if (PADDING && x0 == 0) {
|
yading@10
|
74 if (color == PADDING_COLOR) {
|
yading@10
|
75 len += PADDING;
|
yading@10
|
76 x0 -= PADDING;
|
yading@10
|
77 } else
|
yading@10
|
78 put_xsub_rle(pb, PADDING, PADDING_COLOR);
|
yading@10
|
79 }
|
yading@10
|
80
|
yading@10
|
81 // Run can't be longer than 255, unless it is the rest of a row
|
yading@10
|
82 if (x1 == w && color == PADDING_COLOR) {
|
yading@10
|
83 len += PADDING + (w&1);
|
yading@10
|
84 } else
|
yading@10
|
85 len = FFMIN(len, 255);
|
yading@10
|
86 put_xsub_rle(pb, len, color);
|
yading@10
|
87
|
yading@10
|
88 x0 += len;
|
yading@10
|
89 }
|
yading@10
|
90 if (color != PADDING_COLOR && (PADDING + (w&1)))
|
yading@10
|
91 put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR);
|
yading@10
|
92
|
yading@10
|
93 avpriv_align_put_bits(pb);
|
yading@10
|
94
|
yading@10
|
95 bitmap += linesize;
|
yading@10
|
96 }
|
yading@10
|
97
|
yading@10
|
98 return 0;
|
yading@10
|
99 }
|
yading@10
|
100
|
yading@10
|
101 static int make_tc(uint64_t ms, int *tc)
|
yading@10
|
102 {
|
yading@10
|
103 static const int tc_divs[3] = { 1000, 60, 60 };
|
yading@10
|
104 int i;
|
yading@10
|
105 for (i=0; i<3; i++) {
|
yading@10
|
106 tc[i] = ms % tc_divs[i];
|
yading@10
|
107 ms /= tc_divs[i];
|
yading@10
|
108 }
|
yading@10
|
109 tc[3] = ms;
|
yading@10
|
110 return ms > 99;
|
yading@10
|
111 }
|
yading@10
|
112
|
yading@10
|
113 static int xsub_encode(AVCodecContext *avctx, unsigned char *buf,
|
yading@10
|
114 int bufsize, const AVSubtitle *h)
|
yading@10
|
115 {
|
yading@10
|
116 uint64_t startTime = h->pts / 1000; // FIXME: need better solution...
|
yading@10
|
117 uint64_t endTime = startTime + h->end_display_time - h->start_display_time;
|
yading@10
|
118 int start_tc[4], end_tc[4];
|
yading@10
|
119 uint8_t *hdr = buf + 27; // Point behind the timestamp
|
yading@10
|
120 uint8_t *rlelenptr;
|
yading@10
|
121 uint16_t width, height;
|
yading@10
|
122 int i;
|
yading@10
|
123 PutBitContext pb;
|
yading@10
|
124
|
yading@10
|
125 if (bufsize < 27 + 7*2 + 4*3) {
|
yading@10
|
126 av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n");
|
yading@10
|
127 return -1;
|
yading@10
|
128 }
|
yading@10
|
129
|
yading@10
|
130 // TODO: support multiple rects
|
yading@10
|
131 if (h->num_rects != 1)
|
yading@10
|
132 av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects);
|
yading@10
|
133
|
yading@10
|
134 // TODO: render text-based subtitles into bitmaps
|
yading@10
|
135 if (!h->rects[0]->pict.data[0] || !h->rects[0]->pict.data[1]) {
|
yading@10
|
136 av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n");
|
yading@10
|
137 return -1;
|
yading@10
|
138 }
|
yading@10
|
139
|
yading@10
|
140 // TODO: color reduction, similar to dvdsub encoder
|
yading@10
|
141 if (h->rects[0]->nb_colors > 4)
|
yading@10
|
142 av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors);
|
yading@10
|
143
|
yading@10
|
144 // TODO: Palette swapping if color zero is not transparent
|
yading@10
|
145 if (((uint32_t *)h->rects[0]->pict.data[1])[0] & 0xff)
|
yading@10
|
146 av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n");
|
yading@10
|
147
|
yading@10
|
148 if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) {
|
yading@10
|
149 av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n");
|
yading@10
|
150 return -1;
|
yading@10
|
151 }
|
yading@10
|
152
|
yading@10
|
153 snprintf(buf, 28,
|
yading@10
|
154 "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]",
|
yading@10
|
155 start_tc[3], start_tc[2], start_tc[1], start_tc[0],
|
yading@10
|
156 end_tc[3], end_tc[2], end_tc[1], end_tc[0]);
|
yading@10
|
157
|
yading@10
|
158 // Width and height must probably be multiples of 2.
|
yading@10
|
159 // 2 pixels required on either side of subtitle.
|
yading@10
|
160 // Possibly due to limitations of hardware renderers.
|
yading@10
|
161 // TODO: check if the bitmap is already padded
|
yading@10
|
162 width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2;
|
yading@10
|
163 height = FFALIGN(h->rects[0]->h, 2);
|
yading@10
|
164
|
yading@10
|
165 bytestream_put_le16(&hdr, width);
|
yading@10
|
166 bytestream_put_le16(&hdr, height);
|
yading@10
|
167 bytestream_put_le16(&hdr, h->rects[0]->x);
|
yading@10
|
168 bytestream_put_le16(&hdr, h->rects[0]->y);
|
yading@10
|
169 bytestream_put_le16(&hdr, h->rects[0]->x + width);
|
yading@10
|
170 bytestream_put_le16(&hdr, h->rects[0]->y + height);
|
yading@10
|
171
|
yading@10
|
172 rlelenptr = hdr; // Will store length of first field here later.
|
yading@10
|
173 hdr+=2;
|
yading@10
|
174
|
yading@10
|
175 // Palette
|
yading@10
|
176 for (i=0; i<4; i++)
|
yading@10
|
177 bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]);
|
yading@10
|
178
|
yading@10
|
179 // Bitmap
|
yading@10
|
180 // RLE buffer. Reserve 2 bytes for possible padding after the last row.
|
yading@10
|
181 init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
|
yading@10
|
182 if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0],
|
yading@10
|
183 h->rects[0]->pict.linesize[0]*2,
|
yading@10
|
184 h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
|
yading@10
|
185 return -1;
|
yading@10
|
186 bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field
|
yading@10
|
187
|
yading@10
|
188 if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0] + h->rects[0]->pict.linesize[0],
|
yading@10
|
189 h->rects[0]->pict.linesize[0]*2,
|
yading@10
|
190 h->rects[0]->w, h->rects[0]->h >> 1))
|
yading@10
|
191 return -1;
|
yading@10
|
192
|
yading@10
|
193 // Enforce total height to be be multiple of 2
|
yading@10
|
194 if (h->rects[0]->h & 1) {
|
yading@10
|
195 put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
|
yading@10
|
196 avpriv_align_put_bits(&pb);
|
yading@10
|
197 }
|
yading@10
|
198
|
yading@10
|
199 flush_put_bits(&pb);
|
yading@10
|
200
|
yading@10
|
201 return hdr - buf + put_bits_count(&pb)/8;
|
yading@10
|
202 }
|
yading@10
|
203
|
yading@10
|
204 static av_cold int xsub_encoder_init(AVCodecContext *avctx)
|
yading@10
|
205 {
|
yading@10
|
206 if (!avctx->codec_tag)
|
yading@10
|
207 avctx->codec_tag = MKTAG('D','X','S','B');
|
yading@10
|
208
|
yading@10
|
209 return 0;
|
yading@10
|
210 }
|
yading@10
|
211
|
yading@10
|
212 AVCodec ff_xsub_encoder = {
|
yading@10
|
213 .name = "xsub",
|
yading@10
|
214 .type = AVMEDIA_TYPE_SUBTITLE,
|
yading@10
|
215 .id = AV_CODEC_ID_XSUB,
|
yading@10
|
216 .init = xsub_encoder_init,
|
yading@10
|
217 .encode_sub = xsub_encode,
|
yading@10
|
218 .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
|
yading@10
|
219 };
|