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