FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dvdsubdec.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 254 433 58.7%
Branches: 130 266 48.9%

Line Branch Exec Source
1 /*
2 * DVD subtitle decoding
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
22 #include "avcodec.h"
23 #include "get_bits.h"
24 #include "internal.h"
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/colorspace.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/bswap.h"
31
32 typedef struct DVDSubContext
33 {
34 AVClass *class;
35 uint32_t palette[16];
36 char *palette_str;
37 char *ifo_str;
38 int has_palette;
39 uint8_t colormap[4];
40 uint8_t alpha[256];
41 uint8_t buf[0x10000];
42 int buf_size;
43 int forced_subs_only;
44 uint8_t used_color[256];
45 #ifdef DEBUG
46 int sub_id;
47 #endif
48 } DVDSubContext;
49
50 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
51 {
52 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
53 uint8_t r, g, b;
54 int i, y, cb, cr;
55 int r_add, g_add, b_add;
56
57 for (i = num_values; i > 0; i--) {
58 y = *ycbcr++;
59 cr = *ycbcr++;
60 cb = *ycbcr++;
61 YUV_TO_RGB1_CCIR(cb, cr);
62 YUV_TO_RGB2_CCIR(r, g, b, y);
63 *rgba++ = ((unsigned)*alpha++ << 24) | (r << 16) | (g << 8) | b;
64 }
65 }
66
67 296727 static int decode_run_2bit(GetBitContext *gb, int *color)
68 {
69 unsigned int v, t;
70
71 296727 v = 0;
72
4/4
✓ Branch 0 taken 536316 times.
✓ Branch 1 taken 255145 times.
✓ Branch 2 taken 494734 times.
✓ Branch 3 taken 41582 times.
791461 for (t = 1; v < t && t <= 0x40; t <<= 2)
73 494734 v = (v << 4) | get_bits(gb, 4);
74 296727 *color = v & 3;
75
2/2
✓ Branch 0 taken 41582 times.
✓ Branch 1 taken 255145 times.
296727 if (v < 4) { /* Code for fill rest of line */
76 41582 return INT_MAX;
77 }
78 255145 return v >> 2;
79 }
80
81 static int decode_run_8bit(GetBitContext *gb, int *color)
82 {
83 int len;
84 int has_run = get_bits1(gb);
85 *color = get_bits(gb, 2 + 6*get_bits1(gb));
86 if (has_run) {
87 if (get_bits1(gb)) {
88 len = get_bits(gb, 7);
89 if (len == 0)
90 len = INT_MAX;
91 else
92 len += 9;
93 } else
94 len = get_bits(gb, 3) + 2;
95 } else
96 len = 1;
97 return len;
98 }
99
100 174 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, uint8_t used_color[256],
101 const uint8_t *buf, int start, int buf_size, int is_8bit)
102 {
103 GetBitContext gb;
104 int bit_len;
105 int x, y, len, color;
106 uint8_t *d;
107
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174 times.
174 if (start >= buf_size)
109 return -1;
110
111
2/4
✓ Branch 0 taken 174 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 174 times.
174 if (w <= 0 || h <= 0)
112 return -1;
113
114 174 bit_len = (buf_size - start) * 8;
115 174 init_get_bits(&gb, buf + start, bit_len);
116
117 174 x = 0;
118 174 y = 0;
119 174 d = bitmap;
120 for(;;) {
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 296727 times.
296727 if (get_bits_count(&gb) > bit_len)
122 return -1;
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296727 times.
296727 if (is_8bit)
124 len = decode_run_8bit(&gb, &color);
125 else
126 296727 len = decode_run_2bit(&gb, &color);
127
3/4
✓ Branch 0 taken 255145 times.
✓ Branch 1 taken 41582 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 255145 times.
296727 if (len != INT_MAX && len > w - x)
128 return AVERROR_INVALIDDATA;
129 296727 len = FFMIN(len, w - x);
130 296727 memset(d + x, color, len);
131 296727 used_color[color] = 1;
132 296727 x += len;
133
2/2
✓ Branch 0 taken 41582 times.
✓ Branch 1 taken 255145 times.
296727 if (x >= w) {
134 41582 y++;
135
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 41408 times.
41582 if (y >= h)
136 174 break;
137 41408 d += linesize;
138 41408 x = 0;
139 /* byte align */
140 41408 align_get_bits(&gb);
141 }
142 }
143 174 return 0;
144 }
145
146 87 static void guess_palette(DVDSubContext* ctx,
147 uint32_t *rgba_palette,
148 uint32_t subtitle_color)
149 {
150 static const uint8_t level_map[4][4] = {
151 // this configuration (full range, lowest to highest) in tests
152 // seemed most common, so assume this
153 {0xff},
154 {0x00, 0xff},
155 {0x00, 0x80, 0xff},
156 {0x00, 0x55, 0xaa, 0xff},
157 };
158 87 uint8_t color_used[16] = { 0 };
159 int nb_opaque_colors, i, level, j, r, g, b;
160 87 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
161
162
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 if(ctx->has_palette) {
163
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 87 times.
435 for(i = 0; i < 4; i++)
164 348 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
165 348 | ((alpha[i] * 17U) << 24);
166 87 return;
167 }
168
169 for(i = 0; i < 4; i++)
170 rgba_palette[i] = 0;
171
172 nb_opaque_colors = 0;
173 for(i = 0; i < 4; i++) {
174 if (alpha[i] != 0 && !color_used[colormap[i]]) {
175 color_used[colormap[i]] = 1;
176 nb_opaque_colors++;
177 }
178 }
179
180 if (nb_opaque_colors == 0)
181 return;
182
183 j = 0;
184 memset(color_used, 0, 16);
185 for(i = 0; i < 4; i++) {
186 if (alpha[i] != 0) {
187 if (!color_used[colormap[i]]) {
188 level = level_map[nb_opaque_colors - 1][j];
189 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
190 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
191 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
192 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17U) << 24);
193 color_used[colormap[i]] = (i + 1);
194 j++;
195 } else {
196 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
197 ((alpha[i] * 17U) << 24);
198 }
199 }
200 }
201 }
202
203 87 static void reset_rects(AVSubtitle *sub_header)
204 {
205 int i;
206
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (sub_header->rects) {
208 for (i = 0; i < sub_header->num_rects; i++) {
209 av_freep(&sub_header->rects[i]->data[0]);
210 av_freep(&sub_header->rects[i]->data[1]);
211 av_freep(&sub_header->rects[i]);
212 }
213 av_freep(&sub_header->rects);
214 sub_header->num_rects = 0;
215 }
216 87 }
217
218 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
219
220 93 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
221 const uint8_t *buf, int buf_size)
222 {
223 int cmd_pos, pos, cmd, x1, y1, x2, y2, next_cmd_pos;
224 93 int big_offsets, offset_size, is_8bit = 0;
225 93 const uint8_t *yuv_palette = NULL;
226 93 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
227 int date;
228 int i;
229 93 int is_menu = 0;
230 uint32_t size;
231 int64_t offset1, offset2;
232
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (buf_size < 10)
234 return -1;
235
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
237 big_offsets = 1;
238 offset_size = 4;
239 cmd_pos = 6;
240 } else {
241 93 big_offsets = 0;
242 93 offset_size = 2;
243 93 cmd_pos = 2;
244 }
245
246
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 93 times.
93 size = READ_OFFSET(buf + (big_offsets ? 2 : 0));
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 cmd_pos = READ_OFFSET(buf + cmd_pos);
248
249
3/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 87 times.
93 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (cmd_pos > size) {
251 av_log(ctx, AV_LOG_ERROR, "Discarding invalid packet\n");
252 return 0;
253 }
254 6 return AVERROR(EAGAIN);
255 }
256
257
2/4
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 173 times.
✗ Branch 3 not taken.
173 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
258 173 date = AV_RB16(buf + cmd_pos);
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173 times.
173 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
260 ff_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
261 cmd_pos, next_cmd_pos, date);
262 173 pos = cmd_pos + 2 + offset_size;
263 173 offset1 = -1;
264 173 offset2 = -1;
265 173 x1 = y1 = x2 = y2 = 0;
266
1/2
✓ Branch 0 taken 694 times.
✗ Branch 1 not taken.
694 while (pos < buf_size) {
267 694 cmd = buf[pos++];
268 ff_dlog(NULL, "cmd=%02x\n", cmd);
269
7/12
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 86 times.
✓ Branch 3 taken 87 times.
✓ Branch 4 taken 87 times.
✓ Branch 5 taken 87 times.
✓ Branch 6 taken 87 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 173 times.
✗ Branch 11 not taken.
694 switch(cmd) {
270 case 0x00:
271 /* menu subpicture */
272 is_menu = 1;
273 break;
274 87 case 0x01:
275 /* set start date */
276 87 sub_header->start_display_time = (date << 10) / 90;
277 87 break;
278 86 case 0x02:
279 /* set end date */
280 86 sub_header->end_display_time = (date << 10) / 90;
281 86 break;
282 87 case 0x03:
283 /* set colormap */
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if ((buf_size - pos) < 2)
285 goto fail;
286 87 colormap[3] = buf[pos] >> 4;
287 87 colormap[2] = buf[pos] & 0x0f;
288 87 colormap[1] = buf[pos + 1] >> 4;
289 87 colormap[0] = buf[pos + 1] & 0x0f;
290 87 pos += 2;
291 87 break;
292 87 case 0x04:
293 /* set alpha */
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if ((buf_size - pos) < 2)
295 goto fail;
296 87 alpha[3] = buf[pos] >> 4;
297 87 alpha[2] = buf[pos] & 0x0f;
298 87 alpha[1] = buf[pos + 1] >> 4;
299 87 alpha[0] = buf[pos + 1] & 0x0f;
300 87 pos += 2;
301 ff_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
302 87 break;
303 87 case 0x05:
304 case 0x85:
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if ((buf_size - pos) < 6)
306 goto fail;
307 87 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
308 87 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
309 87 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
310 87 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (cmd & 0x80)
312 is_8bit = 1;
313 ff_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
314 87 pos += 6;
315 87 break;
316 87 case 0x06:
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if ((buf_size - pos) < 4)
318 goto fail;
319 87 offset1 = AV_RB16(buf + pos);
320 87 offset2 = AV_RB16(buf + pos + 2);
321 ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
322 87 pos += 4;
323 87 break;
324 case 0x86:
325 if ((buf_size - pos) < 8)
326 goto fail;
327 offset1 = AV_RB32(buf + pos);
328 offset2 = AV_RB32(buf + pos + 4);
329 ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
330 pos += 8;
331 break;
332
333 case 0x83:
334 /* HD set palette */
335 if ((buf_size - pos) < 768)
336 goto fail;
337 yuv_palette = buf + pos;
338 pos += 768;
339 break;
340 case 0x84:
341 /* HD set contrast (alpha) */
342 if ((buf_size - pos) < 256)
343 goto fail;
344 for (i = 0; i < 256; i++)
345 alpha[i] = 0xFF - buf[pos+i];
346 pos += 256;
347 break;
348
349 173 case 0xff:
350 173 goto the_end;
351 default:
352 ff_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
353 goto the_end;
354 }
355 }
356 the_end:
357
2/4
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 173 times.
173 if (offset1 >= buf_size || offset2 >= buf_size)
358 goto fail;
359
360
3/4
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
173 if (offset1 >= 0 && offset2 >= 0) {
361 int w, h;
362 uint8_t *bitmap;
363
364 /* decode the bitmap */
365 87 w = x2 - x1 + 1;
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (w < 0)
367 w = 0;
368 87 h = y2 - y1 + 1;
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (h < 0)
370 h = 0;
371
2/4
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
87 if (w > 0 && h > 1) {
372 87 reset_rects(sub_header);
373 87 memset(ctx->used_color, 0, sizeof(ctx->used_color));
374 87 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (!sub_header->rects)
376 goto fail;
377 87 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (!sub_header->rects[0])
379 goto fail;
380 87 sub_header->num_rects = 1;
381 87 bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (!bitmap)
383 goto fail;
384
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
87 if (decode_rle(bitmap, w * 2, w, (h + 1) / 2, ctx->used_color,
385 buf, offset1, buf_size, is_8bit) < 0)
386 goto fail;
387
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
87 if (decode_rle(bitmap + w, w * 2, w, h / 2, ctx->used_color,
388 buf, offset2, buf_size, is_8bit) < 0)
389 goto fail;
390 87 sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (!sub_header->rects[0]->data[1])
392 goto fail;
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (is_8bit) {
394 if (!yuv_palette)
395 goto fail;
396 sub_header->rects[0]->nb_colors = 256;
397 yuv_a_to_rgba(yuv_palette, alpha,
398 (uint32_t *)sub_header->rects[0]->data[1],
399 256);
400 } else {
401 87 sub_header->rects[0]->nb_colors = 4;
402 87 guess_palette(ctx, (uint32_t*)sub_header->rects[0]->data[1],
403 0xffff00);
404 }
405 87 sub_header->rects[0]->x = x1;
406 87 sub_header->rects[0]->y = y1;
407 87 sub_header->rects[0]->w = w;
408 87 sub_header->rects[0]->h = h;
409 87 sub_header->rects[0]->type = SUBTITLE_BITMAP;
410 87 sub_header->rects[0]->linesize[0] = w;
411 87 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
412 }
413 }
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173 times.
173 if (next_cmd_pos < cmd_pos) {
415 av_log(ctx, AV_LOG_ERROR, "Invalid command offset\n");
416 break;
417 }
418
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 86 times.
173 if (next_cmd_pos == cmd_pos)
419 87 break;
420 86 cmd_pos = next_cmd_pos;
421 }
422
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 if (sub_header->num_rects > 0)
423 87 return is_menu;
424 fail:
425 reset_rects(sub_header);
426 return -1;
427 }
428
429 76353 static int is_transp(const uint8_t *buf, int pitch, int n,
430 const uint8_t *transp_color)
431 {
432 int i;
433
2/2
✓ Branch 0 taken 45739192 times.
✓ Branch 1 taken 76005 times.
45815197 for(i = 0; i < n; i++) {
434
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 45738844 times.
45739192 if (!transp_color[*buf])
435 348 return 0;
436 45738844 buf += pitch;
437 }
438 76005 return 1;
439 }
440
441 /* return 0 if empty rectangle, 1 if non empty */
442 87 static int find_smallest_bounding_rectangle(DVDSubContext *ctx, AVSubtitle *s)
443 {
444 87 uint8_t transp_color[256] = { 0 };
445 int y1, y2, x1, x2, y, w, h, i;
446 uint8_t *bitmap;
447 87 int transparent = 1;
448
449
4/8
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 87 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 87 times.
87 if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
450 return 0;
451
452
2/2
✓ Branch 0 taken 348 times.
✓ Branch 1 taken 87 times.
435 for(i = 0; i < s->rects[0]->nb_colors; i++) {
453
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 174 times.
348 if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0) {
454 174 transp_color[i] = 1;
455
1/2
✓ Branch 0 taken 174 times.
✗ Branch 1 not taken.
174 } else if (ctx->used_color[i])
456 174 transparent = 0;
457 }
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (transparent)
459 return 0;
460 87 y1 = 0;
461
3/4
✓ Branch 0 taken 31663 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31576 times.
✓ Branch 3 taken 87 times.
63326 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
462 31663 1, s->rects[0]->w, transp_color))
463 31576 y1++;
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (y1 == s->rects[0]->h) {
465 av_freep(&s->rects[0]->data[0]);
466 s->rects[0]->w = s->rects[0]->h = 0;
467 return 0;
468 }
469
470 87 y2 = s->rects[0]->h - 1;
471
3/4
✓ Branch 0 taken 6932 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6845 times.
✓ Branch 3 taken 87 times.
13864 while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
472 6932 s->rects[0]->w, transp_color))
473 6845 y2--;
474 87 x1 = 0;
475
3/4
✓ Branch 0 taken 18813 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18726 times.
✓ Branch 3 taken 87 times.
37626 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
476 18813 s->rects[0]->h, transp_color))
477 18726 x1++;
478 87 x2 = s->rects[0]->w - 1;
479
3/4
✓ Branch 0 taken 18945 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 18858 times.
✓ Branch 4 taken 87 times.
18945 while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
480 transp_color))
481 18858 x2--;
482 87 w = x2 - x1 + 1;
483 87 h = y2 - y1 + 1;
484 87 bitmap = av_malloc(w * h);
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (!bitmap)
486 return 1;
487
2/2
✓ Branch 0 taken 3161 times.
✓ Branch 1 taken 87 times.
3248 for(y = 0; y < h; y++) {
488 3161 memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
489 }
490 87 av_freep(&s->rects[0]->data[0]);
491 87 s->rects[0]->data[0] = bitmap;
492 87 s->rects[0]->linesize[0] = w;
493 87 s->rects[0]->w = w;
494 87 s->rects[0]->h = h;
495 87 s->rects[0]->x += x1;
496 87 s->rects[0]->y += y1;
497
498 87 return 1;
499 }
500
501 #ifdef DEBUG
502 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
503 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
504 uint32_t *rgba_palette)
505 {
506 int x, y, alpha;
507 uint32_t v;
508 int back[3] = {0, 255, 0}; /* green background */
509 FILE *f;
510
511 f = fopen(filename, "w");
512 if (!f) {
513 perror(filename);
514 return;
515 }
516 fprintf(f, "P6\n"
517 "%d %d\n"
518 "%d\n",
519 w, h, 255);
520 for(y = 0; y < h; y++) {
521 for(x = 0; x < w; x++) {
522 v = rgba_palette[bitmap[y * w + x]];
523 alpha = v >> 24;
524 putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
525 putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
526 putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
527 }
528 }
529 fclose(f);
530 }
531 #endif
532
533 12 static int append_to_cached_buf(AVCodecContext *avctx,
534 const uint8_t *buf, int buf_size)
535 {
536 12 DVDSubContext *ctx = avctx->priv_data;
537
538
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 av_assert0(buf_size >= 0 && ctx->buf_size <= sizeof(ctx->buf));
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (buf_size >= sizeof(ctx->buf) - ctx->buf_size) {
540 av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
541 "too large SPU packets aborted.\n");
542 ctx->buf_size = 0;
543 return AVERROR_INVALIDDATA;
544 }
545 12 memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
546 12 ctx->buf_size += buf_size;
547 12 return 0;
548 }
549
550 93 static int dvdsub_decode(AVCodecContext *avctx,
551 void *data, int *data_size,
552 AVPacket *avpkt)
553 {
554 93 DVDSubContext *ctx = avctx->priv_data;
555 93 const uint8_t *buf = avpkt->data;
556 93 int buf_size = avpkt->size;
557 93 AVSubtitle *sub = data;
558 93 int appended = 0;
559 int is_menu;
560
561
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 87 times.
93 if (ctx->buf_size) {
562 6 int ret = append_to_cached_buf(avctx, buf, buf_size);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
564 *data_size = 0;
565 return ret;
566 }
567 6 buf = ctx->buf;
568 6 buf_size = ctx->buf_size;
569 6 appended = 1;
570 }
571
572 93 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
573
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 87 times.
93 if (is_menu == AVERROR(EAGAIN)) {
574 6 *data_size = 0;
575
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 return appended ? 0 : append_to_cached_buf(avctx, buf, buf_size);
576 }
577
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (is_menu < 0) {
579 ctx->buf_size = 0;
580 no_subtitle:
581 reset_rects(sub);
582 *data_size = 0;
583
584 return buf_size;
585 }
586
2/4
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 87 times.
87 if (!is_menu && find_smallest_bounding_rectangle(ctx, sub) == 0)
587 goto no_subtitle;
588
589
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
87 if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
590 goto no_subtitle;
591
592 #if defined(DEBUG)
593 {
594 char ppm_name[32];
595
596 snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
597 ff_dlog(NULL, "start=%d ms end =%d ms\n",
598 sub->start_display_time,
599 sub->end_display_time);
600 ppm_save(ppm_name, sub->rects[0]->data[0],
601 sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->data[1]);
602 }
603 #endif
604
605 87 ctx->buf_size = 0;
606 87 *data_size = 1;
607 87 return buf_size;
608 }
609
610 static int parse_ifo_palette(DVDSubContext *ctx, char *p)
611 {
612 FILE *ifo;
613 char ifostr[12];
614 uint32_t sp_pgci, pgci, off_pgc, pgc;
615 uint8_t r, g, b, yuv[65], *buf;
616 int i, y, cb, cr, r_add, g_add, b_add;
617 int ret = 0;
618 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
619
620 ctx->has_palette = 0;
621 if ((ifo = fopen(p, "r")) == NULL) {
622 av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno)));
623 return AVERROR_EOF;
624 }
625 if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
626 av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
627 ret = AVERROR_INVALIDDATA;
628 goto end;
629 }
630 if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
631 ret = AVERROR(errno);
632 goto end;
633 }
634 if (fread(&sp_pgci, 4, 1, ifo) == 1) {
635 pgci = av_be2ne32(sp_pgci) * 2048;
636 if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
637 ret = AVERROR(errno);
638 goto end;
639 }
640 if (fread(&off_pgc, 4, 1, ifo) == 1) {
641 pgc = pgci + av_be2ne32(off_pgc);
642 if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
643 ret = AVERROR(errno);
644 goto end;
645 }
646 if (fread(yuv, 64, 1, ifo) == 1) {
647 buf = yuv;
648 for(i=0; i<16; i++) {
649 y = *++buf;
650 cr = *++buf;
651 cb = *++buf;
652 YUV_TO_RGB1_CCIR(cb, cr);
653 YUV_TO_RGB2_CCIR(r, g, b, y);
654 ctx->palette[i] = (r << 16) + (g << 8) + b;
655 buf++;
656 }
657 ctx->has_palette = 1;
658 }
659 }
660 }
661 if (ctx->has_palette == 0) {
662 av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
663 ret = AVERROR_INVALIDDATA;
664 }
665 end:
666 fclose(ifo);
667 return ret;
668 }
669
670 11 static int dvdsub_parse_extradata(AVCodecContext *avctx)
671 {
672 11 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
673 char *dataorig, *data;
674 11 int ret = 1;
675
676
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
11 if (!avctx->extradata || !avctx->extradata_size)
677 2 return 1;
678
679 9 dataorig = data = av_malloc(avctx->extradata_size+1);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!data)
681 return AVERROR(ENOMEM);
682 9 memcpy(data, avctx->extradata, avctx->extradata_size);
683 9 data[avctx->extradata_size] = '\0';
684
685 67 for(;;) {
686 76 int pos = strcspn(data, "\n\r");
687
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
76 if (pos==0 && *data==0)
688 9 break;
689
690
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 58 times.
67 if (strncmp("palette:", data, 8) == 0) {
691 9 ctx->has_palette = 1;
692 9 ff_dvdsub_parse_palette(ctx->palette, data + 8);
693
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 49 times.
58 } else if (strncmp("size:", data, 5) == 0) {
694 int w, h;
695
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
696 9 ret = ff_set_dimensions(avctx, w, h);
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
698 goto fail;
699 }
700 }
701
702 67 data += pos;
703 67 data += strspn(data, "\n\r");
704 }
705
706 9 fail:
707 9 av_free(dataorig);
708 9 return ret;
709 }
710
711 11 static av_cold int dvdsub_init(AVCodecContext *avctx)
712 {
713 11 DVDSubContext *ctx = avctx->priv_data;
714 int ret;
715
716
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 if ((ret = dvdsub_parse_extradata(avctx)) < 0)
717 return ret;
718
719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ctx->ifo_str)
720 parse_ifo_palette(ctx, ctx->ifo_str);
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ctx->palette_str) {
722 ctx->has_palette = 1;
723 ff_dvdsub_parse_palette(ctx->palette, ctx->palette_str);
724 }
725
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
11 if (ctx->has_palette) {
726 int i;
727 9 av_log(avctx, AV_LOG_DEBUG, "palette:");
728
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 9 times.
153 for(i=0;i<16;i++)
729 144 av_log(avctx, AV_LOG_DEBUG, " 0x%06"PRIx32, ctx->palette[i]);
730 9 av_log(avctx, AV_LOG_DEBUG, "\n");
731 }
732
733 11 return 1;
734 }
735
736 static void dvdsub_flush(AVCodecContext *avctx)
737 {
738 DVDSubContext *ctx = avctx->priv_data;
739 ctx->buf_size = 0;
740 }
741
742 #define OFFSET(field) offsetof(DVDSubContext, field)
743 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
744 static const AVOption options[] = {
745 { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
746 { "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
747 { "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD},
748 { NULL }
749 };
750 static const AVClass dvdsub_class = {
751 .class_name = "dvdsubdec",
752 .item_name = av_default_item_name,
753 .option = options,
754 .version = LIBAVUTIL_VERSION_INT,
755 };
756
757 const AVCodec ff_dvdsub_decoder = {
758 .name = "dvdsub",
759 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
760 .type = AVMEDIA_TYPE_SUBTITLE,
761 .id = AV_CODEC_ID_DVD_SUBTITLE,
762 .priv_data_size = sizeof(DVDSubContext),
763 .init = dvdsub_init,
764 .decode = dvdsub_decode,
765 .flush = dvdsub_flush,
766 .priv_class = &dvdsub_class,
767 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
768 };
769