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