annotate dvbsub.c @ 2756:d8874c8749ec libavcodec

subtitle codec type support
author bellard
date Fri, 03 Jun 2005 13:59:38 +0000
parents
children 95c35706acbb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2756
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
1 /*
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
2 * DVB subtitle encoding for ffmpeg
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
3 * Copyright (c) 2005 Fabrice Bellard.
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
4 *
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
9 *
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
14 *
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
18 */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
19 #include "avcodec.h"
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
20
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
21 typedef struct DVBSubtitleContext {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
22 int hide_state;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
23 int object_version;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
24 } DVBSubtitleContext;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
25
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
26 #define PUTBITS2(val)\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
27 {\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
28 bitbuf |= (val) << bitcnt;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
29 bitcnt -= 2;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
30 if (bitcnt < 0) {\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
31 bitcnt = 6;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
32 *q++ = bitbuf;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
33 bitbuf = 0;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
34 }\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
35 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
36
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
37 static void dvb_encode_rle2(uint8_t **pq,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
38 const uint8_t *bitmap, int linesize,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
39 int w, int h)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
40 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
41 uint8_t *q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
42 unsigned int bitbuf;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
43 int bitcnt;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
44 int x, y, len, x1, v, color;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
45
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
46 q = *pq;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
47
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
48 for(y = 0; y < h; y++) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
49 *q++ = 0x10;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
50 bitbuf = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
51 bitcnt = 6;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
52
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
53 x = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
54 while (x < w) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
55 x1 = x;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
56 color = bitmap[x1++];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
57 while (x1 < w && bitmap[x1] == color)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
58 x1++;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
59 len = x1 - x;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
60 if (color == 0 && len == 2) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
61 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
62 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
63 PUTBITS2(1);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
64 } else if (len >= 3 && len <= 10) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
65 v = len - 3;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
66 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
67 PUTBITS2((v >> 2) | 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
68 PUTBITS2(v & 3);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
69 PUTBITS2(color);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
70 } else if (len >= 12 && len <= 27) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
71 v = len - 12;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
72 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
73 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
74 PUTBITS2(2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
75 PUTBITS2(v >> 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
76 PUTBITS2(v & 3);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
77 PUTBITS2(color);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
78 } else if (len >= 29) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
79 /* length = 29 ... 284 */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
80 if (len > 284)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
81 len = 284;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
82 v = len - 29;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
83 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
84 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
85 PUTBITS2(3);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
86 PUTBITS2((v >> 6));
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
87 PUTBITS2((v >> 4) & 3);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
88 PUTBITS2((v >> 2) & 3);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
89 PUTBITS2(v & 3);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
90 PUTBITS2(color);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
91 } else {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
92 PUTBITS2(color);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
93 if (color == 0) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
94 PUTBITS2(1);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
95 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
96 len = 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
97 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
98 x += len;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
99 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
100 /* end of line */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
101 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
102 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
103 PUTBITS2(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
104 if (bitcnt != 6) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
105 *q++ = bitbuf;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
106 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
107 *q++ = 0xf0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
108 bitmap += linesize;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
109 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
110 *pq = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
111 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
112
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
113 #define PUTBITS4(val)\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
114 {\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
115 bitbuf |= (val) << bitcnt;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
116 bitcnt -= 4;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
117 if (bitcnt < 0) {\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
118 bitcnt = 4;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
119 *q++ = bitbuf;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
120 bitbuf = 0;\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
121 }\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
122 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
123
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
124 /* some DVB decoders only implement 4 bits/pixel */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
125 static void dvb_encode_rle4(uint8_t **pq,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
126 const uint8_t *bitmap, int linesize,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
127 int w, int h)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
128 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
129 uint8_t *q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
130 unsigned int bitbuf;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
131 int bitcnt;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
132 int x, y, len, x1, v, color;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
133
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
134 q = *pq;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
135
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
136 for(y = 0; y < h; y++) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
137 *q++ = 0x11;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
138 bitbuf = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
139 bitcnt = 4;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
140
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
141 x = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
142 while (x < w) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
143 x1 = x;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
144 color = bitmap[x1++];
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
145 while (x1 < w && bitmap[x1] == color)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
146 x1++;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
147 len = x1 - x;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
148 if (color == 0 && len == 2) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
149 PUTBITS4(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
150 PUTBITS4(0xd);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
151 } else if (color == 0 && (len >= 3 && len <= 9)) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
152 PUTBITS4(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
153 PUTBITS4(len - 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
154 } else if (len >= 4 && len <= 7) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
155 PUTBITS4(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
156 PUTBITS4(8 + len - 4);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
157 PUTBITS4(color);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
158 } else if (len >= 9 && len <= 24) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
159 PUTBITS4(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
160 PUTBITS4(0xe);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
161 PUTBITS4(len - 9);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
162 PUTBITS4(color);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
163 } else if (len >= 25) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
164 if (len > 280)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
165 len = 280;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
166 v = len - 25;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
167 PUTBITS4(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
168 PUTBITS4(0xf);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
169 PUTBITS4(v >> 4);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
170 PUTBITS4(v & 0xf);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
171 PUTBITS4(color);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
172 } else {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
173 PUTBITS4(color);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
174 if (color == 0) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
175 PUTBITS4(0xc);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
176 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
177 len = 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
178 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
179 x += len;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
180 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
181 /* end of line */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
182 PUTBITS4(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
183 PUTBITS4(0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
184 if (bitcnt != 4) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
185 *q++ = bitbuf;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
186 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
187 *q++ = 0xf0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
188 bitmap += linesize;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
189 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
190 *pq = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
191 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
192
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
193 #define SCALEBITS 10
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
194 #define ONE_HALF (1 << (SCALEBITS - 1))
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
195 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
196
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
197 #define RGB_TO_Y_CCIR(r, g, b) \
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
198 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
199 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
200
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
201 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
202 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
203 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
204
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
205 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
206 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
207 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
208
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
209 static inline void putbe16(uint8_t **pq, uint16_t v)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
210 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
211 uint8_t *q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
212 q = *pq;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
213 *q++ = v >> 8;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
214 *q++ = v;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
215 *pq = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
216 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
217
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
218 static int encode_dvb_subtitles(DVBSubtitleContext *s,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
219 uint8_t *outbuf, AVSubtitle *h)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
220 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
221 uint8_t *q, *pseg_len;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
222 int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
223
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
224
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
225 q = outbuf;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
226
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
227 page_id = 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
228 region_id = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
229 clut_id = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
230 object_id = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
231 if (h->nb_colors <= 4) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
232 /* 2 bpp, some decoders do not support it correctly */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
233 bpp_index = 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
234 } else if (h->nb_colors <= 16) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
235 /* 4 bpp, standard encoding */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
236 bpp_index = 1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
237 } else {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
238 return -1;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
239 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
240
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
241 *q++ = 0x00; /* subtitle_stream_id */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
242
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
243 /* page composition segment */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
244
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
245 *q++ = 0x0f; /* sync_byte */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
246 *q++ = 0x10; /* segment_type */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
247 putbe16(&q, page_id);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
248 pseg_len = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
249 q += 2; /* segment length */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
250 *q++ = 30; /* page_timeout (seconds) */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
251 if (s->hide_state)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
252 page_state = 0; /* normal case */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
253 else
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
254 page_state = 2; /* mode change */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
255 /* page_version = 0 + page_state */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
256 *q++ = s->object_version | (page_state << 2) | 3;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
257 *q++ = region_id;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
258 *q++ = 0xff; /* reserved */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
259 putbe16(&q, 0); /* left pos */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
260 putbe16(&q, 0); /* top pos */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
261
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
262 putbe16(&pseg_len, q - pseg_len - 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
263
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
264 if (!s->hide_state) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
265 /* CLUT segment */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
266
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
267 *q++ = 0x0f; /* sync byte */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
268 *q++ = 0x12; /* CLUT definition segment */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
269 putbe16(&q, page_id);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
270 pseg_len = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
271 q += 2; /* segment length */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
272 *q++ = clut_id;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
273 *q++ = (0 << 4) | 0xf; /* version = 0 */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
274
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
275 for(i = 0; i < h->nb_colors; i++) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
276 *q++ = i; /* clut_entry_id */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
277 *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
278 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
279 int a, r, g, b;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
280 a = (h->rgba_palette[i] >> 24) & 0xff;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
281 r = (h->rgba_palette[i] >> 16) & 0xff;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
282 g = (h->rgba_palette[i] >> 8) & 0xff;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
283 b = (h->rgba_palette[i] >> 0) & 0xff;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
284
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
285 *q++ = RGB_TO_Y_CCIR(r, g, b);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
286 *q++ = RGB_TO_V_CCIR(r, g, b, 0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
287 *q++ = RGB_TO_U_CCIR(r, g, b, 0);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
288 *q++ = 255 - a;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
289 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
290 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
291
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
292 putbe16(&pseg_len, q - pseg_len - 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
293 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
294 /* region composition segment */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
295
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
296 *q++ = 0x0f; /* sync_byte */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
297 *q++ = 0x11; /* segment_type */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
298 putbe16(&q, page_id);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
299 pseg_len = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
300 q += 2; /* segment length */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
301 *q++ = region_id;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
302 *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
303 putbe16(&q, 720); /* region width */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
304 putbe16(&q, 576); /* region height */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
305 *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
306 *q++ = clut_id;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
307 *q++ = 0; /* 8 bit fill colors */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
308 *q++ = 0x03; /* 4 bit and 2 bit fill colors */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
309
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
310 if (!s->hide_state) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
311 putbe16(&q, object_id);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
312 *q++ = (0 << 6) | (0 << 4) | ((h->x >> 8) & 0xf);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
313 *q++ = h->x;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
314 *q++ = 0xf0 | ((h->y >> 8) & 0xf);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
315 *q++ = h->y;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
316 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
317
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
318 putbe16(&pseg_len, q - pseg_len - 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
319
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
320 if (!s->hide_state) {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
321
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
322 /* Object Data segment */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
323
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
324 *q++ = 0x0f; /* sync byte */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
325 *q++ = 0x13;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
326 putbe16(&q, page_id);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
327 pseg_len = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
328 q += 2; /* segment length */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
329
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
330 putbe16(&q, object_id);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
331 *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
332 onject_coding_method,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
333 non_modifying_color_flag */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
334 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
335 uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
336 void (*dvb_encode_rle)(uint8_t **pq,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
337 const uint8_t *bitmap, int linesize,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
338 int w, int h);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
339 ptop_field_len = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
340 q += 2;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
341 pbottom_field_len = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
342 q += 2;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
343
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
344 if (bpp_index == 0)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
345 dvb_encode_rle = dvb_encode_rle2;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
346 else
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
347 dvb_encode_rle = dvb_encode_rle4;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
348
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
349 top_ptr = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
350 dvb_encode_rle(&q, h->bitmap, h->w * 2, h->w, h->h >> 1);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
351 bottom_ptr = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
352 dvb_encode_rle(&q, h->bitmap + h->w, h->w * 2, h->w, h->h >> 1);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
353
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
354 putbe16(&ptop_field_len, bottom_ptr - top_ptr);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
355 putbe16(&pbottom_field_len, q - bottom_ptr);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
356 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
357
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
358 putbe16(&pseg_len, q - pseg_len - 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
359 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
360
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
361 /* end of display set segment */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
362
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
363 *q++ = 0x0f; /* sync_byte */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
364 *q++ = 0x80; /* segment_type */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
365 putbe16(&q, page_id);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
366 pseg_len = q;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
367 q += 2; /* segment length */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
368
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
369 putbe16(&pseg_len, q - pseg_len - 2);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
370
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
371 *q++ = 0xff; /* end of PES data */
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
372
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
373 s->object_version = (s->object_version + 1) & 0xf;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
374 s->hide_state = !s->hide_state;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
375 return q - outbuf;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
376 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
377
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
378 static int dvbsub_init_decoder(AVCodecContext *avctx)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
379 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
380 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
381 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
382
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
383 static int dvbsub_close_decoder(AVCodecContext *avctx)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
384 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
385 return 0;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
386 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
387
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
388 static int dvbsub_encode(AVCodecContext *avctx,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
389 unsigned char *buf, int buf_size, void *data)
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
390 {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
391 DVBSubtitleContext *s = avctx->priv_data;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
392 AVSubtitle *sub = data;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
393 int ret;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
394
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
395 ret = encode_dvb_subtitles(s, buf, sub);
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
396 return ret;
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
397 }
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
398
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
399 AVCodec dvbsub_encoder = {
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
400 "dvbsub",
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
401 CODEC_TYPE_SUBTITLE,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
402 CODEC_ID_DVB_SUBTITLE,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
403 sizeof(DVBSubtitleContext),
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
404 dvbsub_init_decoder,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
405 dvbsub_encode,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
406 dvbsub_close_decoder,
d8874c8749ec subtitle codec type support
bellard
parents:
diff changeset
407 };