Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * DVB subtitle encoding | ||
3 | * Copyright (c) 2005 Fabrice Bellard | ||
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 | #include "avcodec.h" | ||
22 | #include "bytestream.h" | ||
23 | #include "codec_internal.h" | ||
24 | #include "libavutil/colorspace.h" | ||
25 | #include "libavutil/opt.h" | ||
26 | |||
27 | typedef struct DVBSubtitleContext { | ||
28 | AVClass * class; | ||
29 | int object_version; | ||
30 | int min_bpp; | ||
31 | } DVBSubtitleContext; | ||
32 | |||
33 | #define PUTBITS2(val)\ | ||
34 | {\ | ||
35 | bitbuf |= (val) << bitcnt;\ | ||
36 | bitcnt -= 2;\ | ||
37 | if (bitcnt < 0) {\ | ||
38 | bitcnt = 6;\ | ||
39 | *q++ = bitbuf;\ | ||
40 | bitbuf = 0;\ | ||
41 | }\ | ||
42 | } | ||
43 | |||
44 | ✗ | static int dvb_encode_rle2(uint8_t **pq, int buf_size, | |
45 | const uint8_t *bitmap, int linesize, | ||
46 | int w, int h) | ||
47 | { | ||
48 | uint8_t *q, *line_begin; | ||
49 | unsigned int bitbuf; | ||
50 | int bitcnt; | ||
51 | int x, y, len, x1, v, color; | ||
52 | |||
53 | ✗ | q = *pq; | |
54 | |||
55 | ✗ | for(y = 0; y < h; y++) { | |
56 | // Worst case line is 3 bits per value + 4 bytes overhead | ||
57 | ✗ | if (buf_size * 8 < w * 3 + 32) | |
58 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
59 | ✗ | line_begin = q; | |
60 | ✗ | *q++ = 0x10; | |
61 | ✗ | bitbuf = 0; | |
62 | ✗ | bitcnt = 6; | |
63 | |||
64 | ✗ | x = 0; | |
65 | ✗ | while (x < w) { | |
66 | ✗ | x1 = x; | |
67 | ✗ | color = bitmap[x1++]; | |
68 | ✗ | while (x1 < w && bitmap[x1] == color) | |
69 | ✗ | x1++; | |
70 | ✗ | len = x1 - x; | |
71 | ✗ | if (color == 0 && len == 2) { | |
72 | ✗ | PUTBITS2(0); | |
73 | ✗ | PUTBITS2(0); | |
74 | ✗ | PUTBITS2(1); | |
75 | ✗ | } else if (len >= 3 && len <= 10) { | |
76 | ✗ | v = len - 3; | |
77 | ✗ | PUTBITS2(0); | |
78 | ✗ | PUTBITS2((v >> 2) | 2); | |
79 | ✗ | PUTBITS2(v & 3); | |
80 | ✗ | PUTBITS2(color); | |
81 | ✗ | } else if (len >= 12 && len <= 27) { | |
82 | ✗ | v = len - 12; | |
83 | ✗ | PUTBITS2(0); | |
84 | ✗ | PUTBITS2(0); | |
85 | ✗ | PUTBITS2(2); | |
86 | ✗ | PUTBITS2(v >> 2); | |
87 | ✗ | PUTBITS2(v & 3); | |
88 | ✗ | PUTBITS2(color); | |
89 | ✗ | } else if (len >= 29) { | |
90 | /* length = 29 ... 284 */ | ||
91 | ✗ | if (len > 284) | |
92 | ✗ | len = 284; | |
93 | ✗ | v = len - 29; | |
94 | ✗ | PUTBITS2(0); | |
95 | ✗ | PUTBITS2(0); | |
96 | ✗ | PUTBITS2(3); | |
97 | ✗ | PUTBITS2((v >> 6)); | |
98 | ✗ | PUTBITS2((v >> 4) & 3); | |
99 | ✗ | PUTBITS2((v >> 2) & 3); | |
100 | ✗ | PUTBITS2(v & 3); | |
101 | ✗ | PUTBITS2(color); | |
102 | } else { | ||
103 | ✗ | PUTBITS2(color); | |
104 | ✗ | if (color == 0) { | |
105 | ✗ | PUTBITS2(1); | |
106 | } | ||
107 | ✗ | len = 1; | |
108 | } | ||
109 | ✗ | x += len; | |
110 | } | ||
111 | /* end of line */ | ||
112 | ✗ | PUTBITS2(0); | |
113 | ✗ | PUTBITS2(0); | |
114 | ✗ | PUTBITS2(0); | |
115 | ✗ | if (bitcnt != 6) { | |
116 | ✗ | *q++ = bitbuf; | |
117 | } | ||
118 | ✗ | *q++ = 0xf0; | |
119 | ✗ | bitmap += linesize; | |
120 | ✗ | buf_size -= q - line_begin; | |
121 | } | ||
122 | ✗ | len = q - *pq; | |
123 | ✗ | *pq = q; | |
124 | ✗ | return len; | |
125 | } | ||
126 | |||
127 | #define PUTBITS4(val)\ | ||
128 | {\ | ||
129 | bitbuf |= (val) << bitcnt;\ | ||
130 | bitcnt -= 4;\ | ||
131 | if (bitcnt < 0) {\ | ||
132 | bitcnt = 4;\ | ||
133 | *q++ = bitbuf;\ | ||
134 | bitbuf = 0;\ | ||
135 | }\ | ||
136 | } | ||
137 | |||
138 | /* some DVB decoders only implement 4 bits/pixel */ | ||
139 | 132 | static int dvb_encode_rle4(uint8_t **pq, int buf_size, | |
140 | const uint8_t *bitmap, int linesize, | ||
141 | int w, int h) | ||
142 | { | ||
143 | uint8_t *q, *line_begin; | ||
144 | unsigned int bitbuf; | ||
145 | int bitcnt; | ||
146 | int x, y, len, x1, v, color; | ||
147 | |||
148 | 132 | q = *pq; | |
149 | |||
150 |
2/2✓ Branch 0 taken 2376 times.
✓ Branch 1 taken 132 times.
|
2508 | for(y = 0; y < h; y++) { |
151 | // Worst case line is 6 bits per value, + 4 bytes overhead | ||
152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2376 times.
|
2376 | if (buf_size * 8 < w * 6 + 32) |
153 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
154 | 2376 | line_begin = q; | |
155 | 2376 | *q++ = 0x11; | |
156 | 2376 | bitbuf = 0; | |
157 | 2376 | bitcnt = 4; | |
158 | |||
159 | 2376 | x = 0; | |
160 |
2/2✓ Branch 0 taken 254154 times.
✓ Branch 1 taken 2376 times.
|
256530 | while (x < w) { |
161 | 254154 | x1 = x; | |
162 | 254154 | color = bitmap[x1++]; | |
163 |
4/4✓ Branch 0 taken 1989019 times.
✓ Branch 1 taken 3492 times.
✓ Branch 2 taken 1738357 times.
✓ Branch 3 taken 250662 times.
|
1992511 | while (x1 < w && bitmap[x1] == color) |
164 | 1738357 | x1++; | |
165 | 254154 | len = x1 - x; | |
166 |
3/4✓ Branch 0 taken 5868 times.
✓ Branch 1 taken 248286 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5868 times.
|
254154 | if (color == 0 && len == 2) { |
167 | ✗ | PUTBITS4(0); | |
168 | ✗ | PUTBITS4(0xd); | |
169 |
5/6✓ Branch 0 taken 5868 times.
✓ Branch 1 taken 248286 times.
✓ Branch 2 taken 5868 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✓ Branch 5 taken 5832 times.
|
254154 | } else if (color == 0 && (len >= 3 && len <= 9)) { |
170 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | PUTBITS4(0); |
171 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | PUTBITS4(len - 2); |
172 |
4/4✓ Branch 0 taken 36714 times.
✓ Branch 1 taken 217404 times.
✓ Branch 2 taken 18199 times.
✓ Branch 3 taken 18515 times.
|
254118 | } else if (len >= 4 && len <= 7) { |
173 |
2/2✓ Branch 0 taken 9378 times.
✓ Branch 1 taken 8821 times.
|
18199 | PUTBITS4(0); |
174 |
2/2✓ Branch 0 taken 8821 times.
✓ Branch 1 taken 9378 times.
|
18199 | PUTBITS4(8 + len - 4); |
175 |
2/2✓ Branch 0 taken 9378 times.
✓ Branch 1 taken 8821 times.
|
18199 | PUTBITS4(color); |
176 |
4/4✓ Branch 0 taken 17158 times.
✓ Branch 1 taken 218761 times.
✓ Branch 2 taken 7926 times.
✓ Branch 3 taken 9232 times.
|
235919 | } else if (len >= 9 && len <= 24) { |
177 |
2/2✓ Branch 0 taken 4414 times.
✓ Branch 1 taken 3512 times.
|
7926 | PUTBITS4(0); |
178 |
2/2✓ Branch 0 taken 3512 times.
✓ Branch 1 taken 4414 times.
|
7926 | PUTBITS4(0xe); |
179 |
2/2✓ Branch 0 taken 4414 times.
✓ Branch 1 taken 3512 times.
|
7926 | PUTBITS4(len - 9); |
180 |
2/2✓ Branch 0 taken 3512 times.
✓ Branch 1 taken 4414 times.
|
7926 | PUTBITS4(color); |
181 |
2/2✓ Branch 0 taken 9232 times.
✓ Branch 1 taken 218761 times.
|
227993 | } else if (len >= 25) { |
182 |
2/2✓ Branch 0 taken 1469 times.
✓ Branch 1 taken 7763 times.
|
9232 | if (len > 280) |
183 | 1469 | len = 280; | |
184 | 9232 | v = len - 25; | |
185 |
2/2✓ Branch 0 taken 3950 times.
✓ Branch 1 taken 5282 times.
|
9232 | PUTBITS4(0); |
186 |
2/2✓ Branch 0 taken 5282 times.
✓ Branch 1 taken 3950 times.
|
9232 | PUTBITS4(0xf); |
187 |
2/2✓ Branch 0 taken 3950 times.
✓ Branch 1 taken 5282 times.
|
9232 | PUTBITS4(v >> 4); |
188 |
2/2✓ Branch 0 taken 5282 times.
✓ Branch 1 taken 3950 times.
|
9232 | PUTBITS4(v & 0xf); |
189 |
2/2✓ Branch 0 taken 3950 times.
✓ Branch 1 taken 5282 times.
|
9232 | PUTBITS4(color); |
190 | } else { | ||
191 |
2/2✓ Branch 0 taken 109256 times.
✓ Branch 1 taken 109505 times.
|
218761 | PUTBITS4(color); |
192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 218761 times.
|
218761 | if (color == 0) { |
193 | ✗ | PUTBITS4(0xc); | |
194 | } | ||
195 | 218761 | len = 1; | |
196 | } | ||
197 | 254154 | x += len; | |
198 | } | ||
199 | /* end of line */ | ||
200 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 1352 times.
|
2376 | PUTBITS4(0); |
201 |
2/2✓ Branch 0 taken 1352 times.
✓ Branch 1 taken 1024 times.
|
2376 | PUTBITS4(0); |
202 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 1352 times.
|
2376 | if (bitcnt != 4) { |
203 | 1024 | *q++ = bitbuf; | |
204 | } | ||
205 | 2376 | *q++ = 0xf0; | |
206 | 2376 | bitmap += linesize; | |
207 | 2376 | buf_size -= q - line_begin; | |
208 | } | ||
209 | 132 | len = q - *pq; | |
210 | 132 | *pq = q; | |
211 | 132 | return len; | |
212 | } | ||
213 | |||
214 | ✗ | static int dvb_encode_rle8(uint8_t **pq, int buf_size, | |
215 | const uint8_t *bitmap, int linesize, | ||
216 | int w, int h) | ||
217 | { | ||
218 | uint8_t *q, *line_begin; | ||
219 | int x, y, len, x1, color; | ||
220 | |||
221 | ✗ | q = *pq; | |
222 | |||
223 | ✗ | for (y = 0; y < h; y++) { | |
224 | // Worst case line is 12 bits per value, + 3 bytes overhead | ||
225 | ✗ | if (buf_size * 8 < w * 12 + 24) | |
226 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
227 | ✗ | line_begin = q; | |
228 | ✗ | *q++ = 0x12; | |
229 | |||
230 | ✗ | x = 0; | |
231 | ✗ | while (x < w) { | |
232 | ✗ | x1 = x; | |
233 | ✗ | color = bitmap[x1++]; | |
234 | ✗ | while (x1 < w && bitmap[x1] == color) | |
235 | ✗ | x1++; | |
236 | ✗ | len = x1 - x; | |
237 | ✗ | if (len == 1 && color) { | |
238 | // 00000001 to 11111111 1 pixel in colour x | ||
239 | ✗ | *q++ = color; | |
240 | } else { | ||
241 | ✗ | if (color == 0x00) { | |
242 | // 00000000 0LLLLLLL L pixels (1-127) in colour 0 (L > 0) | ||
243 | ✗ | len = FFMIN(len, 127); | |
244 | ✗ | *q++ = 0x00; | |
245 | ✗ | *q++ = len; | |
246 | ✗ | } else if (len > 2) { | |
247 | // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2) | ||
248 | ✗ | len = FFMIN(len, 127); | |
249 | ✗ | *q++ = 0x00; | |
250 | ✗ | *q++ = 0x80+len; | |
251 | ✗ | *q++ = color; | |
252 | } | ||
253 | ✗ | else if (len == 2) { | |
254 | ✗ | *q++ = color; | |
255 | ✗ | *q++ = color; | |
256 | } else { | ||
257 | ✗ | *q++ = color; | |
258 | ✗ | len = 1; | |
259 | } | ||
260 | } | ||
261 | ✗ | x += len; | |
262 | } | ||
263 | /* end of line */ | ||
264 | // 00000000 00000000 end of 8-bit/pixel_code_string | ||
265 | ✗ | *q++ = 0x00; | |
266 | ✗ | *q++ = 0x00; | |
267 | ✗ | *q++ = 0xf0; | |
268 | ✗ | bitmap += linesize; | |
269 | ✗ | buf_size -= q - line_begin; | |
270 | } | ||
271 | ✗ | len = q - *pq; | |
272 | ✗ | *pq = q; | |
273 | ✗ | return len; | |
274 | } | ||
275 | |||
276 | 72 | static int dvbsub_encode(AVCodecContext *avctx, uint8_t *outbuf, int buf_size, | |
277 | const AVSubtitle *h) | ||
278 | { | ||
279 | 72 | DVBSubtitleContext *s = avctx->priv_data; | |
280 | uint8_t *q, *pseg_len; | ||
281 | int page_id, region_id, clut_id, object_id, i, bpp_index, page_state, min_colors; | ||
282 | |||
283 | |||
284 | 72 | q = outbuf; | |
285 | |||
286 | 72 | page_id = 1; | |
287 | |||
288 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | switch(s->min_bpp) { |
289 | 72 | case 2: | |
290 | case 4: | ||
291 | case 8: | ||
292 | 72 | min_colors = 1 << s->min_bpp; | |
293 | 72 | break; | |
294 | ✗ | default: | |
295 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid min_bpp value %d.\n", s->min_bpp); | |
296 | ✗ | return AVERROR(EINVAL); | |
297 | } | ||
298 | |||
299 |
3/4✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
|
72 | if (h->num_rects && !h->rects) |
300 | ✗ | return AVERROR(EINVAL); | |
301 | |||
302 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (h->num_rects >= 256) |
303 | ✗ | return AVERROR(EINVAL); | |
304 | |||
305 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
72 | if (avctx->width > 0 && avctx->height > 0) { |
306 | ✗ | if (buf_size < 11) | |
307 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
308 | /* display definition segment */ | ||
309 | ✗ | *q++ = 0x0f; /* sync_byte */ | |
310 | ✗ | *q++ = 0x14; /* segment_type */ | |
311 | ✗ | bytestream_put_be16(&q, page_id); | |
312 | ✗ | pseg_len = q; | |
313 | ✗ | q += 2; /* segment length */ | |
314 | ✗ | *q++ = 0x00; /* dds version number & display window flag */ | |
315 | ✗ | bytestream_put_be16(&q, avctx->width - 1); /* display width */ | |
316 | ✗ | bytestream_put_be16(&q, avctx->height - 1); /* display height */ | |
317 | ✗ | bytestream_put_be16(&pseg_len, q - pseg_len - 2); | |
318 | ✗ | buf_size -= 11; | |
319 | } | ||
320 | |||
321 | /* page composition segment */ | ||
322 | |||
323 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (buf_size < 8 + h->num_rects * 6) |
324 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
325 | 72 | *q++ = 0x0f; /* sync_byte */ | |
326 | 72 | *q++ = 0x10; /* segment_type */ | |
327 | 72 | bytestream_put_be16(&q, page_id); | |
328 | 72 | pseg_len = q; | |
329 | 72 | q += 2; /* segment length */ | |
330 | 72 | *q++ = 30; /* page_timeout (seconds) */ | |
331 | 72 | page_state = 2; /* mode change */ | |
332 | /* page_version = 0 + page_state */ | ||
333 | 72 | *q++ = (s->object_version << 4) | (page_state << 2) | 3; | |
334 | |||
335 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 72 times.
|
138 | for (region_id = 0; region_id < h->num_rects; region_id++) { |
336 | 66 | *q++ = region_id; | |
337 | 66 | *q++ = 0xff; /* reserved */ | |
338 | 66 | bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */ | |
339 | 66 | bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */ | |
340 | } | ||
341 | |||
342 | 72 | bytestream_put_be16(&pseg_len, q - pseg_len - 2); | |
343 | 72 | buf_size -= 8 + h->num_rects * 6; | |
344 | |||
345 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
|
72 | if (h->num_rects) { |
346 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 36 times.
|
102 | for (clut_id = 0; clut_id < h->num_rects; clut_id++) { |
347 | /* CLUT segment */ | ||
348 | 66 | int nb_colors = FFMAX(min_colors, h->rects[clut_id]->nb_colors); | |
349 | |||
350 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (nb_colors <= 4U) { |
351 | /* 2 bpp, some decoders do not support it correctly */ | ||
352 | ✗ | bpp_index = 0; | |
353 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | } else if (nb_colors <= 16U) { |
354 | /* 4 bpp, standard encoding */ | ||
355 | 66 | bpp_index = 1; | |
356 | ✗ | } else if (nb_colors <= 256U) { | |
357 | /* 8 bpp, standard encoding */ | ||
358 | ✗ | bpp_index = 2; | |
359 | } else { | ||
360 | ✗ | return AVERROR(EINVAL); | |
361 | } | ||
362 | |||
363 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (buf_size < 6 + h->rects[clut_id]->nb_colors * 6) |
364 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
365 | |||
366 | /* CLUT segment */ | ||
367 | 66 | *q++ = 0x0f; /* sync byte */ | |
368 | 66 | *q++ = 0x12; /* CLUT definition segment */ | |
369 | 66 | bytestream_put_be16(&q, page_id); | |
370 | 66 | pseg_len = q; | |
371 | 66 | q += 2; /* segment length */ | |
372 | 66 | *q++ = clut_id; | |
373 | 66 | *q++ = (0 << 4) | 0xf; /* version = 0 */ | |
374 | |||
375 |
2/2✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 66 times.
|
1122 | for(i = 0; i < h->rects[clut_id]->nb_colors; i++) { |
376 | 1056 | *q++ = i; /* clut_entry_id */ | |
377 | 1056 | *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */ | |
378 | { | ||
379 | int a, r, g, b; | ||
380 | 1056 | uint32_t x= ((uint32_t*)h->rects[clut_id]->data[1])[i]; | |
381 | 1056 | a = (x >> 24) & 0xff; | |
382 | 1056 | r = (x >> 16) & 0xff; | |
383 | 1056 | g = (x >> 8) & 0xff; | |
384 | 1056 | b = (x >> 0) & 0xff; | |
385 | |||
386 | 1056 | *q++ = RGB_TO_Y_CCIR(r, g, b); | |
387 | 1056 | *q++ = RGB_TO_V_CCIR(r, g, b, 0); | |
388 | 1056 | *q++ = RGB_TO_U_CCIR(r, g, b, 0); | |
389 | 1056 | *q++ = 255 - a; | |
390 | } | ||
391 | } | ||
392 | |||
393 | 66 | bytestream_put_be16(&pseg_len, q - pseg_len - 2); | |
394 | 66 | buf_size -= 6 + h->rects[clut_id]->nb_colors * 6; | |
395 | } | ||
396 | |||
397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (buf_size < h->num_rects * 22) |
398 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
399 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 36 times.
|
102 | for (region_id = 0; region_id < h->num_rects; region_id++) { |
400 | |||
401 | /* region composition segment */ | ||
402 | 66 | int nb_colors = FFMAX(min_colors, h->rects[region_id]->nb_colors); | |
403 | |||
404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (nb_colors <= 4) { |
405 | /* 2 bpp, some decoders do not support it correctly */ | ||
406 | ✗ | bpp_index = 0; | |
407 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | } else if (nb_colors <= 16) { |
408 | /* 4 bpp, standard encoding */ | ||
409 | 66 | bpp_index = 1; | |
410 | ✗ | } else if (nb_colors <= 256) { | |
411 | /* 8 bpp, standard encoding */ | ||
412 | ✗ | bpp_index = 2; | |
413 | } else { | ||
414 | ✗ | return AVERROR(EINVAL); | |
415 | } | ||
416 | |||
417 | 66 | *q++ = 0x0f; /* sync_byte */ | |
418 | 66 | *q++ = 0x11; /* segment_type */ | |
419 | 66 | bytestream_put_be16(&q, page_id); | |
420 | 66 | pseg_len = q; | |
421 | 66 | q += 2; /* segment length */ | |
422 | 66 | *q++ = region_id; | |
423 | 66 | *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */ | |
424 | 66 | bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */ | |
425 | 66 | bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */ | |
426 | 66 | *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03; | |
427 | 66 | *q++ = region_id; /* clut_id == region_id */ | |
428 | 66 | *q++ = 0; /* 8 bit fill colors */ | |
429 | 66 | *q++ = 0x03; /* 4 bit and 2 bit fill colors */ | |
430 | |||
431 | 66 | bytestream_put_be16(&q, region_id); /* object_id == region_id */ | |
432 | 66 | *q++ = (0 << 6) | (0 << 4); | |
433 | 66 | *q++ = 0; | |
434 | 66 | *q++ = 0xf0; | |
435 | 66 | *q++ = 0; | |
436 | |||
437 | 66 | bytestream_put_be16(&pseg_len, q - pseg_len - 2); | |
438 | } | ||
439 | 36 | buf_size -= h->num_rects * 22; | |
440 | |||
441 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 36 times.
|
102 | for (object_id = 0; object_id < h->num_rects; object_id++) { |
442 | int (*dvb_encode_rle)(uint8_t **pq, int buf_size, | ||
443 | const uint8_t *bitmap, int linesize, | ||
444 | int w, int h); | ||
445 | |||
446 | 66 | int nb_colors = FFMAX(min_colors, h->rects[object_id]->nb_colors); | |
447 | |||
448 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (buf_size < 13) |
449 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
450 | |||
451 | /* bpp_index maths */ | ||
452 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (nb_colors <= 4) { |
453 | /* 2 bpp, some decoders do not support it correctly */ | ||
454 | ✗ | dvb_encode_rle = dvb_encode_rle2; | |
455 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
66 | } else if (nb_colors <= 16) { |
456 | /* 4 bpp, standard encoding */ | ||
457 | 66 | dvb_encode_rle = dvb_encode_rle4; | |
458 | ✗ | } else if (nb_colors <= 256) { | |
459 | /* 8 bpp, standard encoding */ | ||
460 | ✗ | dvb_encode_rle = dvb_encode_rle8; | |
461 | } else { | ||
462 | ✗ | return AVERROR(EINVAL); | |
463 | } | ||
464 | |||
465 | /* Object Data segment */ | ||
466 | 66 | *q++ = 0x0f; /* sync byte */ | |
467 | 66 | *q++ = 0x13; | |
468 | 66 | bytestream_put_be16(&q, page_id); | |
469 | 66 | pseg_len = q; | |
470 | 66 | q += 2; /* segment length */ | |
471 | |||
472 | 66 | bytestream_put_be16(&q, object_id); | |
473 | 66 | *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0, | |
474 | object_coding_method, | ||
475 | non_modifying_color_flag */ | ||
476 | { | ||
477 | uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr; | ||
478 | int ret; | ||
479 | |||
480 | 66 | ptop_field_len = q; | |
481 | 66 | q += 2; | |
482 | 66 | pbottom_field_len = q; | |
483 | 66 | q += 2; | |
484 | 66 | buf_size -= 13; | |
485 | |||
486 | 66 | top_ptr = q; | |
487 | 66 | ret = dvb_encode_rle(&q, buf_size, | |
488 | 66 | h->rects[object_id]->data[0], | |
489 | 66 | h->rects[object_id]->w * 2, | |
490 | 66 | h->rects[object_id]->w, | |
491 | 66 | h->rects[object_id]->h >> 1); | |
492 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (ret < 0) |
493 | ✗ | return ret; | |
494 | 66 | buf_size -= ret; | |
495 | 66 | bottom_ptr = q; | |
496 | 66 | ret = dvb_encode_rle(&q, buf_size, | |
497 | 66 | h->rects[object_id]->data[0] + h->rects[object_id]->w, | |
498 | 66 | h->rects[object_id]->w * 2, | |
499 | 66 | h->rects[object_id]->w, | |
500 | 66 | h->rects[object_id]->h >> 1); | |
501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (ret < 0) |
502 | ✗ | return ret; | |
503 | 66 | buf_size -= ret; | |
504 | |||
505 | 66 | bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr); | |
506 | 66 | bytestream_put_be16(&pbottom_field_len, q - bottom_ptr); | |
507 | } | ||
508 | |||
509 | 66 | bytestream_put_be16(&pseg_len, q - pseg_len - 2); | |
510 | } | ||
511 | } | ||
512 | |||
513 | /* end of display set segment */ | ||
514 | |||
515 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
|
72 | if (buf_size < 6) |
516 | ✗ | return AVERROR_BUFFER_TOO_SMALL; | |
517 | 72 | *q++ = 0x0f; /* sync_byte */ | |
518 | 72 | *q++ = 0x80; /* segment_type */ | |
519 | 72 | bytestream_put_be16(&q, page_id); | |
520 | 72 | pseg_len = q; | |
521 | 72 | q += 2; /* segment length */ | |
522 | |||
523 | 72 | bytestream_put_be16(&pseg_len, q - pseg_len - 2); | |
524 | 72 | buf_size -= 6; | |
525 | |||
526 | 72 | s->object_version = (s->object_version + 1) & 0xf; | |
527 | 72 | return q - outbuf; | |
528 | } | ||
529 | |||
530 | #define OFFSET(x) offsetof(DVBSubtitleContext, x) | ||
531 | #define SE AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
532 | static const AVOption options[] = { | ||
533 | {"min_bpp", "minimum bits-per-pixel for subtitle colors (2, 4 or 8)", OFFSET(min_bpp), AV_OPT_TYPE_INT, {.i64 = 4}, 2, 8, SE}, | ||
534 | { NULL }, | ||
535 | }; | ||
536 | |||
537 | static const AVClass dvbsubenc_class = { | ||
538 | .class_name = "DVBSUB subtitle encoder", | ||
539 | .item_name = av_default_item_name, | ||
540 | .option = options, | ||
541 | .version = LIBAVUTIL_VERSION_INT, | ||
542 | }; | ||
543 | |||
544 | const FFCodec ff_dvbsub_encoder = { | ||
545 | .p.name = "dvbsub", | ||
546 | CODEC_LONG_NAME("DVB subtitles"), | ||
547 | .p.type = AVMEDIA_TYPE_SUBTITLE, | ||
548 | .p.id = AV_CODEC_ID_DVB_SUBTITLE, | ||
549 | .priv_data_size = sizeof(DVBSubtitleContext), | ||
550 | FF_CODEC_ENCODE_SUB_CB(dvbsub_encode), | ||
551 | .p.priv_class = &dvbsubenc_class, | ||
552 | }; | ||
553 |