FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dvbsubdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 475 838 56.7%
Functions: 19 23 82.6%
Branches: 267 587 45.5%

Line Branch Exec Source
1 /*
2 * DVB subtitle decoding
3 * Copyright (c) 2005 Ian Caulfield
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 "bytestream.h"
25 #include "codec_internal.h"
26 #include "decode.h"
27 #include "libavutil/colorspace.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/thread.h"
32
33 #define DVBSUB_PAGE_SEGMENT 0x10
34 #define DVBSUB_REGION_SEGMENT 0x11
35 #define DVBSUB_CLUT_SEGMENT 0x12
36 #define DVBSUB_OBJECT_SEGMENT 0x13
37 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
38 #define DVBSUB_DISPLAY_SEGMENT 0x80
39
40 #define cm (ff_crop_tab + MAX_NEG_CROP)
41
42 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
43
44 typedef struct DVBSubCLUT {
45 int id;
46 int version;
47
48 uint32_t clut4[4];
49 uint32_t clut16[16];
50 uint32_t clut256[256];
51
52 struct DVBSubCLUT *next;
53 } DVBSubCLUT;
54
55 static DVBSubCLUT default_clut;
56
57 typedef struct DVBSubObjectDisplay {
58 int object_id;
59 int region_id;
60
61 int x_pos;
62 int y_pos;
63
64 int fgcolor;
65 int bgcolor;
66
67 struct DVBSubObjectDisplay *region_list_next;
68 struct DVBSubObjectDisplay *object_list_next;
69 } DVBSubObjectDisplay;
70
71 typedef struct DVBSubObject {
72 int id;
73 int version;
74
75 int type;
76
77 DVBSubObjectDisplay *display_list;
78
79 struct DVBSubObject *next;
80 } DVBSubObject;
81
82 typedef struct DVBSubRegionDisplay {
83 int region_id;
84
85 int x_pos;
86 int y_pos;
87
88 struct DVBSubRegionDisplay *next;
89 } DVBSubRegionDisplay;
90
91 typedef struct DVBSubRegion {
92 int id;
93 int version;
94
95 int width;
96 int height;
97 int depth;
98
99 int clut;
100 int bgcolor;
101
102 uint8_t computed_clut[4*256];
103 int has_computed_clut;
104
105 uint8_t *pbuf;
106 int buf_size;
107 int dirty;
108
109 DVBSubObjectDisplay *display_list;
110
111 struct DVBSubRegion *next;
112 } DVBSubRegion;
113
114 typedef struct DVBSubDisplayDefinition {
115 int version;
116
117 int x;
118 int y;
119 int width;
120 int height;
121 } DVBSubDisplayDefinition;
122
123 typedef struct DVBSubContext {
124 AVClass *class;
125 int composition_id;
126 int ancillary_id;
127
128 int version;
129 int time_out;
130 int compute_edt; /**< if 1 end display time calculated using pts
131 if 0 (Default) calculated using time out */
132 int compute_clut;
133 int clut_count2[257][256];
134 int substream;
135 int64_t prev_start;
136 DVBSubRegion *region_list;
137 DVBSubCLUT *clut_list;
138 DVBSubObject *object_list;
139
140 DVBSubRegionDisplay *display_list;
141 DVBSubDisplayDefinition *display_definition;
142 } DVBSubContext;
143
144
145 126 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
146 {
147 126 DVBSubObject *ptr = ctx->object_list;
148
149
4/4
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 84 times.
162 while (ptr && ptr->id != object_id) {
150 36 ptr = ptr->next;
151 }
152
153 126 return ptr;
154 }
155
156 79 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
157 {
158 79 DVBSubCLUT *ptr = ctx->clut_list;
159
160
4/4
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 66 times.
115 while (ptr && ptr->id != clut_id) {
161 36 ptr = ptr->next;
162 }
163
164 79 return ptr;
165 }
166
167 273 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
168 {
169 273 DVBSubRegion *ptr = ctx->region_list;
170
171
4/4
✓ Branch 0 taken 878 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 633 times.
✓ Branch 3 taken 245 times.
906 while (ptr && ptr->id != region_id) {
172 633 ptr = ptr->next;
173 }
174
175 273 return ptr;
176 }
177
178 85 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
179 {
180 DVBSubObject *object, *obj2, **obj2_ptr;
181 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
182
183
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 85 times.
127 while (region->display_list) {
184 42 display = region->display_list;
185
186 42 object = get_object(ctx, display->object_id);
187
188
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (object) {
189 42 obj_disp_ptr = &object->display_list;
190 42 obj_disp = *obj_disp_ptr;
191
192
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
42 while (obj_disp && obj_disp != display) {
193 obj_disp_ptr = &obj_disp->object_list_next;
194 obj_disp = *obj_disp_ptr;
195 }
196
197
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (obj_disp) {
198 42 *obj_disp_ptr = obj_disp->object_list_next;
199
200
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (!object->display_list) {
201 42 obj2_ptr = &ctx->object_list;
202 42 obj2 = *obj2_ptr;
203
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 while (obj2 != object) {
205 av_assert0(obj2);
206 obj2_ptr = &obj2->next;
207 obj2 = *obj2_ptr;
208 }
209
210 42 *obj2_ptr = obj2->next;
211
212 42 av_freep(&obj2);
213 }
214 }
215 }
216
217 42 region->display_list = display->region_list_next;
218
219 42 av_freep(&display);
220 }
221
222 85 }
223
224 15 static void delete_cluts(DVBSubContext *ctx)
225 {
226
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 15 times.
28 while (ctx->clut_list) {
227 13 DVBSubCLUT *clut = ctx->clut_list;
228
229 13 ctx->clut_list = clut->next;
230
231 13 av_freep(&clut);
232 }
233 15 }
234
235 15 static void delete_objects(DVBSubContext *ctx)
236 {
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 while (ctx->object_list) {
238 DVBSubObject *object = ctx->object_list;
239
240 ctx->object_list = object->next;
241
242 av_freep(&object);
243 }
244 15 }
245
246 15 static void delete_regions(DVBSubContext *ctx)
247 {
248
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 15 times.
43 while (ctx->region_list) {
249 28 DVBSubRegion *region = ctx->region_list;
250
251 28 ctx->region_list = region->next;
252
253 28 delete_region_display_list(ctx, region);
254
255 28 av_freep(&region->pbuf);
256 28 av_freep(&region);
257 }
258 15 }
259
260 4 static av_cold void init_default_clut(void)
261 {
262 4 int i, r, g, b, a = 0;
263
264 4 default_clut.id = -1;
265 4 default_clut.next = NULL;
266
267 4 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
268 4 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
269 4 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
270 4 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
271
272 4 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
273
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 4 times.
64 for (i = 1; i < 16; i++) {
274
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 32 times.
60 if (i < 8) {
275
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 r = (i & 1) ? 255 : 0;
276
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 g = (i & 2) ? 255 : 0;
277
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 b = (i & 4) ? 255 : 0;
278 } else {
279
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 r = (i & 1) ? 127 : 0;
280
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 g = (i & 2) ? 127 : 0;
281
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 b = (i & 4) ? 127 : 0;
282 }
283 60 default_clut.clut16[i] = RGBA(r, g, b, 255);
284 }
285
286 4 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
287
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 4 times.
1024 for (i = 1; i < 256; i++) {
288
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 992 times.
1020 if (i < 8) {
289
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 r = (i & 1) ? 255 : 0;
290
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 g = (i & 2) ? 255 : 0;
291
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 b = (i & 4) ? 255 : 0;
292 28 a = 63;
293 } else {
294
4/5
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 256 times.
✓ Branch 2 taken 256 times.
✓ Branch 3 taken 256 times.
✗ Branch 4 not taken.
992 switch (i & 0x88) {
295 224 case 0x00:
296
4/4
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 96 times.
224 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
297
4/4
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 96 times.
224 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
298
4/4
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 96 times.
224 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
299 224 a = 255;
300 224 break;
301 256 case 0x08:
302
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
303
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
304
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
305 256 a = 127;
306 256 break;
307 256 case 0x80:
308
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
309
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
310
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
311 256 a = 255;
312 256 break;
313 256 case 0x88:
314
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
315
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
316
4/4
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 128 times.
256 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
317 256 a = 255;
318 256 break;
319 }
320 }
321 1020 default_clut.clut256[i] = RGBA(r, g, b, a);
322 }
323 4 }
324
325 8 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
326 {
327 static AVOnce init_static_once = AV_ONCE_INIT;
328 8 DVBSubContext *ctx = avctx->priv_data;
329
330
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (ctx->substream < 0) {
331 8 ctx->composition_id = -1;
332 8 ctx->ancillary_id = -1;
333 } else if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
334 av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
335 ctx->composition_id = -1;
336 ctx->ancillary_id = -1;
337 } else {
338 if (avctx->extradata_size > 5*ctx->substream + 2) {
339 ctx->composition_id = AV_RB16(avctx->extradata + 5*ctx->substream);
340 ctx->ancillary_id = AV_RB16(avctx->extradata + 5*ctx->substream + 2);
341 } else {
342 av_log(avctx, AV_LOG_WARNING, "Selected DVB subtitles sub-stream %d is not available\n", ctx->substream);
343 ctx->composition_id = AV_RB16(avctx->extradata);
344 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
345 }
346 }
347
348 8 ctx->version = -1;
349 8 ctx->prev_start = AV_NOPTS_VALUE;
350
351 8 ff_thread_once(&init_static_once, init_default_clut);
352
353 8 return 0;
354 }
355
356 8 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
357 {
358 8 DVBSubContext *ctx = avctx->priv_data;
359 DVBSubRegionDisplay *display;
360
361 8 delete_regions(ctx);
362
363 8 delete_objects(ctx);
364
365 8 delete_cluts(ctx);
366
367 8 av_freep(&ctx->display_definition);
368
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 while (ctx->display_list) {
370 display = ctx->display_list;
371 ctx->display_list = display->next;
372
373 av_freep(&display);
374 }
375
376 8 return 0;
377 }
378
379 static int dvbsub_read_2bit_string(AVCodecContext *avctx,
380 uint8_t *destbuf, int dbuf_len,
381 const uint8_t **srcbuf, int buf_size,
382 int non_mod, uint8_t *map_table, int x_pos)
383 {
384 GetBitContext gb;
385
386 int bits;
387 int run_length;
388 int pixels_read = x_pos;
389
390 init_get_bits(&gb, *srcbuf, buf_size << 3);
391
392 destbuf += x_pos;
393
394 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
395 bits = get_bits(&gb, 2);
396
397 if (bits) {
398 if (non_mod != 1 || bits != 1) {
399 if (map_table)
400 *destbuf++ = map_table[bits];
401 else
402 *destbuf++ = bits;
403 }
404 pixels_read++;
405 } else {
406 bits = get_bits1(&gb);
407 if (bits == 1) {
408 run_length = get_bits(&gb, 3) + 3;
409 bits = get_bits(&gb, 2);
410
411 if (non_mod == 1 && bits == 1)
412 pixels_read += run_length;
413 else {
414 if (map_table)
415 bits = map_table[bits];
416 while (run_length-- > 0 && pixels_read < dbuf_len) {
417 *destbuf++ = bits;
418 pixels_read++;
419 }
420 }
421 } else {
422 bits = get_bits1(&gb);
423 if (bits == 0) {
424 bits = get_bits(&gb, 2);
425 if (bits == 2) {
426 run_length = get_bits(&gb, 4) + 12;
427 bits = get_bits(&gb, 2);
428
429 if (non_mod == 1 && bits == 1)
430 pixels_read += run_length;
431 else {
432 if (map_table)
433 bits = map_table[bits];
434 while (run_length-- > 0 && pixels_read < dbuf_len) {
435 *destbuf++ = bits;
436 pixels_read++;
437 }
438 }
439 } else if (bits == 3) {
440 run_length = get_bits(&gb, 8) + 29;
441 bits = get_bits(&gb, 2);
442
443 if (non_mod == 1 && bits == 1)
444 pixels_read += run_length;
445 else {
446 if (map_table)
447 bits = map_table[bits];
448 while (run_length-- > 0 && pixels_read < dbuf_len) {
449 *destbuf++ = bits;
450 pixels_read++;
451 }
452 }
453 } else if (bits == 1) {
454 if (map_table)
455 bits = map_table[0];
456 else
457 bits = 0;
458 run_length = 2;
459 while (run_length-- > 0 && pixels_read < dbuf_len) {
460 *destbuf++ = bits;
461 pixels_read++;
462 }
463 } else {
464 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
465 return pixels_read;
466 }
467 } else {
468 if (map_table)
469 bits = map_table[0];
470 else
471 bits = 0;
472 *destbuf++ = bits;
473 pixels_read++;
474 }
475 }
476 }
477 }
478
479 if (get_bits(&gb, 6))
480 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
481
482 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
483
484 return pixels_read;
485 }
486
487 1512 static int dvbsub_read_4bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len,
488 const uint8_t **srcbuf, int buf_size,
489 int non_mod, uint8_t *map_table, int x_pos)
490 {
491 GetBitContext gb;
492
493 int bits;
494 int run_length;
495 1512 int pixels_read = x_pos;
496
497 1512 init_get_bits(&gb, *srcbuf, buf_size << 3);
498
499 1512 destbuf += x_pos;
500
501
2/4
✓ Branch 1 taken 70104 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 70104 times.
✗ Branch 4 not taken.
70104 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
502 70104 bits = get_bits(&gb, 4);
503
504
2/2
✓ Branch 0 taken 62235 times.
✓ Branch 1 taken 7869 times.
70104 if (bits) {
505
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 62235 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
62235 if (non_mod != 1 || bits != 1) {
506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62235 times.
62235 if (map_table)
507 *destbuf++ = map_table[bits];
508 else
509 62235 *destbuf++ = bits;
510 }
511 62235 pixels_read++;
512 } else {
513 7869 bits = get_bits1(&gb);
514
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 6357 times.
7869 if (bits == 0) {
515 1512 run_length = get_bits(&gb, 3);
516
517
1/2
✓ Branch 0 taken 1512 times.
✗ Branch 1 not taken.
1512 if (run_length == 0) {
518 1512 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
519 1512 return pixels_read;
520 }
521
522 run_length += 2;
523
524 if (map_table)
525 bits = map_table[0];
526 else
527 bits = 0;
528
529 while (run_length-- > 0 && pixels_read < dbuf_len) {
530 *destbuf++ = bits;
531 pixels_read++;
532 }
533 } else {
534 6357 bits = get_bits1(&gb);
535
2/2
✓ Branch 0 taken 3538 times.
✓ Branch 1 taken 2819 times.
6357 if (bits == 0) {
536 3538 run_length = get_bits(&gb, 2) + 4;
537 3538 bits = get_bits(&gb, 4);
538
539
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3538 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3538 if (non_mod == 1 && bits == 1)
540 pixels_read += run_length;
541 else {
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3538 times.
3538 if (map_table)
543 bits = map_table[bits];
544
3/4
✓ Branch 0 taken 17337 times.
✓ Branch 1 taken 3538 times.
✓ Branch 2 taken 17337 times.
✗ Branch 3 not taken.
20875 while (run_length-- > 0 && pixels_read < dbuf_len) {
545 17337 *destbuf++ = bits;
546 17337 pixels_read++;
547 }
548 }
549 } else {
550 2819 bits = get_bits(&gb, 2);
551
2/2
✓ Branch 0 taken 1741 times.
✓ Branch 1 taken 1078 times.
2819 if (bits == 2) {
552 1741 run_length = get_bits(&gb, 4) + 9;
553 1741 bits = get_bits(&gb, 4);
554
555
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1741 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1741 if (non_mod == 1 && bits == 1)
556 pixels_read += run_length;
557 else {
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1741 times.
1741 if (map_table)
559 bits = map_table[bits];
560
3/4
✓ Branch 0 taken 21366 times.
✓ Branch 1 taken 1741 times.
✓ Branch 2 taken 21366 times.
✗ Branch 3 not taken.
23107 while (run_length-- > 0 && pixels_read < dbuf_len) {
561 21366 *destbuf++ = bits;
562 21366 pixels_read++;
563 }
564 }
565
1/2
✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
1078 } else if (bits == 3) {
566 1078 run_length = get_bits(&gb, 8) + 25;
567 1078 bits = get_bits(&gb, 4);
568
569
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1078 if (non_mod == 1 && bits == 1)
570 pixels_read += run_length;
571 else {
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
1078 if (map_table)
573 bits = map_table[bits];
574
3/4
✓ Branch 0 taken 90942 times.
✓ Branch 1 taken 1078 times.
✓ Branch 2 taken 90942 times.
✗ Branch 3 not taken.
92020 while (run_length-- > 0 && pixels_read < dbuf_len) {
575 90942 *destbuf++ = bits;
576 90942 pixels_read++;
577 }
578 }
579 } else if (bits == 1) {
580 if (map_table)
581 bits = map_table[0];
582 else
583 bits = 0;
584 run_length = 2;
585 while (run_length-- > 0 && pixels_read < dbuf_len) {
586 *destbuf++ = bits;
587 pixels_read++;
588 }
589 } else {
590 if (map_table)
591 bits = map_table[0];
592 else
593 bits = 0;
594 *destbuf++ = bits;
595 pixels_read ++;
596 }
597 }
598 }
599 }
600 }
601
602 if (get_bits(&gb, 8))
603 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
604
605 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
606
607 return pixels_read;
608 }
609
610 static int dvbsub_read_8bit_string(AVCodecContext *avctx,
611 uint8_t *destbuf, int dbuf_len,
612 const uint8_t **srcbuf, int buf_size,
613 int non_mod, uint8_t *map_table, int x_pos)
614 {
615 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
616 int bits;
617 int run_length;
618 int pixels_read = x_pos;
619
620 destbuf += x_pos;
621
622 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
623 bits = *(*srcbuf)++;
624
625 if (bits) {
626 if (non_mod != 1 || bits != 1) {
627 if (map_table)
628 *destbuf++ = map_table[bits];
629 else
630 *destbuf++ = bits;
631 }
632 pixels_read++;
633 } else {
634 bits = *(*srcbuf)++;
635 run_length = bits & 0x7f;
636 if ((bits & 0x80) == 0) {
637 if (run_length == 0) {
638 return pixels_read;
639 }
640
641 bits = 0;
642 } else {
643 bits = *(*srcbuf)++;
644 }
645 if (non_mod == 1 && bits == 1)
646 pixels_read += run_length;
647 else {
648 if (map_table)
649 bits = map_table[bits];
650 while (run_length-- > 0 && pixels_read < dbuf_len) {
651 *destbuf++ = bits;
652 pixels_read++;
653 }
654 }
655 }
656 }
657
658 if (*(*srcbuf)++)
659 av_log(avctx, AV_LOG_ERROR, "line overflow\n");
660
661 return pixels_read;
662 }
663
664 static void compute_default_clut(DVBSubContext *ctx, uint8_t *clut, AVSubtitleRect *rect, int w, int h)
665 {
666 uint8_t list[256] = {0};
667 uint8_t list_inv[256];
668 int counttab[256] = {0};
669 int (*counttab2)[256] = ctx->clut_count2;
670 int count, i, x, y;
671 ptrdiff_t stride = rect->linesize[0];
672
673 memset(ctx->clut_count2, 0 , sizeof(ctx->clut_count2));
674
675 #define V(x,y) rect->data[0][(x) + (y)*stride]
676 for (y = 0; y<h; y++) {
677 for (x = 0; x<w; x++) {
678 int v = V(x,y) + 1;
679 int vl = x ? V(x-1,y) + 1 : 0;
680 int vr = x+1<w ? V(x+1,y) + 1 : 0;
681 int vt = y ? V(x,y-1) + 1 : 0;
682 int vb = y+1<h ? V(x,y+1) + 1 : 0;
683 counttab[v-1] += !!((v!=vl) + (v!=vr) + (v!=vt) + (v!=vb));
684 counttab2[vl][v-1] ++;
685 counttab2[vr][v-1] ++;
686 counttab2[vt][v-1] ++;
687 counttab2[vb][v-1] ++;
688 }
689 }
690 #define L(x,y) list[d[(x) + (y)*stride]]
691
692 for (i = 0; i<256; i++) {
693 counttab2[i+1][i] = 0;
694 }
695 for (i = 0; i<256; i++) {
696 int bestscore = 0;
697 int bestv = 0;
698
699 for (x = 0; x < 256; x++) {
700 int scorev = 0;
701 if (list[x])
702 continue;
703 scorev += counttab2[0][x];
704 for (y = 0; y < 256; y++) {
705 scorev += list[y] * counttab2[y+1][x];
706 }
707
708 if (scorev) {
709 int score = 1024LL*scorev / counttab[x];
710 if (score > bestscore) {
711 bestscore = score;
712 bestv = x;
713 }
714 }
715 }
716 if (!bestscore)
717 break;
718 list [ bestv ] = 1;
719 list_inv[ i ] = bestv;
720 }
721
722 count = FFMAX(i - 1, 1);
723 for (i--; i >= 0; i--) {
724 int v = i * 255 / count;
725 AV_WN32(clut + 4*list_inv[i], RGBA(v/2,v,v/2,v));
726 }
727 }
728
729
730 46 static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
731 {
732 46 DVBSubContext *ctx = avctx->priv_data;
733 DVBSubRegionDisplay *display;
734 46 DVBSubDisplayDefinition *display_def = ctx->display_definition;
735 DVBSubRegion *region;
736 AVSubtitleRect *rect;
737 const DVBSubCLUT *clut;
738 const uint32_t *clut_table;
739 int i;
740 46 int offset_x=0, offset_y=0;
741 46 int ret = 0;
742
743
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (display_def) {
745 offset_x = display_def->x;
746 offset_y = display_def->y;
747 }
748
749 /* Not touching AVSubtitles again*/
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (sub->num_rects) {
751 avpriv_request_sample(ctx, "Different Version of Segment asked Twice");
752 return AVERROR_PATCHWELCOME;
753 }
754
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 46 times.
112 for (display = ctx->display_list; display; display = display->next) {
755 66 region = get_region(ctx, display->region_id);
756
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
66 if (region && region->dirty)
757 66 sub->num_rects++;
758 }
759
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ctx->compute_edt == 0) {
761 sub->end_display_time = ctx->time_out * 1000;
762 *got_output = 1;
763
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 1 times.
46 } else if (ctx->prev_start != AV_NOPTS_VALUE) {
764 45 sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
765 45 *got_output = 1;
766 }
767
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 10 times.
46 if (sub->num_rects > 0) {
768
769 36 sub->rects = av_calloc(sub->num_rects, sizeof(*sub->rects));
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!sub->rects) {
771 ret = AVERROR(ENOMEM);
772 goto fail;
773 }
774
775
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 36 times.
102 for (i = 0; i < sub->num_rects; i++) {
776 66 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!sub->rects[i]) {
778 ret = AVERROR(ENOMEM);
779 goto fail;
780 }
781 }
782
783 36 i = 0;
784
785
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 36 times.
102 for (display = ctx->display_list; display; display = display->next) {
786 66 region = get_region(ctx, display->region_id);
787
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!region)
789 continue;
790
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!region->dirty)
792 continue;
793
794 66 rect = sub->rects[i];
795 66 rect->x = display->x_pos + offset_x;
796 66 rect->y = display->y_pos + offset_y;
797 66 rect->w = region->width;
798 66 rect->h = region->height;
799 66 rect->nb_colors = (1 << region->depth);
800 66 rect->type = SUBTITLE_BITMAP;
801 66 rect->linesize[0] = region->width;
802
803 66 clut = get_clut(ctx, region->clut);
804
805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!clut)
806 clut = &default_clut;
807
808
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
66 switch (region->depth) {
809 case 2:
810 clut_table = clut->clut4;
811 break;
812 case 8:
813 clut_table = clut->clut256;
814 break;
815 66 case 4:
816 default:
817 66 clut_table = clut->clut16;
818 66 break;
819 }
820
821 66 rect->data[1] = av_mallocz(AVPALETTE_SIZE);
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!rect->data[1]) {
823 ret = AVERROR(ENOMEM);
824 goto fail;
825 }
826 66 memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(*clut_table));
827
828 66 rect->data[0] = av_memdup(region->pbuf, region->buf_size);
829
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (!rect->data[0]) {
830 ret = AVERROR(ENOMEM);
831 goto fail;
832 }
833
834
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 66 times.
66 if ((clut == &default_clut && ctx->compute_clut < 0) || ctx->compute_clut == 1) {
835 if (!region->has_computed_clut) {
836 compute_default_clut(ctx, region->computed_clut, rect, rect->w, rect->h);
837 region->has_computed_clut = 1;
838 }
839
840 memcpy(rect->data[1], region->computed_clut, sizeof(region->computed_clut));
841 }
842
843 66 i++;
844 }
845 }
846
847 46 return 0;
848 fail:
849 if (sub->rects) {
850 for (i=0; i < sub->num_rects; i++) {
851 rect = sub->rects[i];
852 if (rect) {
853 av_freep(&rect->data[0]);
854 av_freep(&rect->data[1]);
855 }
856 av_freep(&sub->rects[i]);
857 }
858 av_freep(&sub->rects);
859 }
860 sub->num_rects = 0;
861 return ret;
862 }
863
864 84 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
865 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
866 {
867 84 DVBSubContext *ctx = avctx->priv_data;
868
869 84 DVBSubRegion *region = get_region(ctx, display->region_id);
870 84 const uint8_t *buf_end = buf + buf_size;
871 uint8_t *pbuf;
872 int x_pos, y_pos;
873 int i;
874
875 84 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
876 84 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
877 84 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
878 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
879 uint8_t *map_table;
880
881 #if 0
882 ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
883 top_bottom ? "bottom" : "top");
884
885 for (i = 0; i < buf_size; i++) {
886 if (i % 16 == 0)
887 ff_dlog(avctx, "0x%8p: ", buf+i);
888
889 ff_dlog(avctx, "%02x ", buf[i]);
890 if (i % 16 == 15)
891 ff_dlog(avctx, "\n");
892 }
893
894 if (i % 16)
895 ff_dlog(avctx, "\n");
896 #endif
897
898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 if (!region)
899 return;
900
901 84 pbuf = region->pbuf;
902 84 region->dirty = 1;
903
904 84 x_pos = display->x_pos;
905 84 y_pos = display->y_pos;
906
907 84 y_pos += top_bottom;
908
909
2/2
✓ Branch 0 taken 3024 times.
✓ Branch 1 taken 84 times.
3108 while (buf < buf_end) {
910
4/6
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 1512 times.
✓ Branch 2 taken 1512 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3024 times.
3024 if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
911 av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
912 return;
913 }
914
915
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1512 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1512 times.
✗ Branch 7 not taken.
3024 switch (*buf++) {
916 case 0x10:
917 if (region->depth == 8)
918 map_table = map2to8;
919 else if (region->depth == 4)
920 map_table = map2to4;
921 else
922 map_table = NULL;
923
924 x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
925 region->width, &buf, buf_end - buf,
926 non_mod, map_table, x_pos);
927 break;
928 1512 case 0x11:
929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1512 times.
1512 if (region->depth < 4) {
930 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
931 return;
932 }
933
934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1512 times.
1512 if (region->depth == 8)
935 map_table = map4to8;
936 else
937 1512 map_table = NULL;
938
939 1512 x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
940 1512 region->width, &buf, buf_end - buf,
941 non_mod, map_table, x_pos);
942 1512 break;
943 case 0x12:
944 if (region->depth < 8) {
945 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
946 return;
947 }
948
949 x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
950 region->width, &buf, buf_end - buf,
951 non_mod, NULL, x_pos);
952 break;
953
954 case 0x20:
955 map2to4[0] = (*buf) >> 4;
956 map2to4[1] = (*buf++) & 0xf;
957 map2to4[2] = (*buf) >> 4;
958 map2to4[3] = (*buf++) & 0xf;
959 break;
960 case 0x21:
961 for (i = 0; i < 4; i++)
962 map2to8[i] = *buf++;
963 break;
964 case 0x22:
965 for (i = 0; i < 16; i++)
966 map4to8[i] = *buf++;
967 break;
968
969 1512 case 0xf0:
970 1512 x_pos = display->x_pos;
971 1512 y_pos += 2;
972 1512 break;
973 default:
974 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
975 }
976 }
977
978
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 if (ctx->compute_clut != -2)
979 84 region->has_computed_clut = 0;
980 }
981
982 42 static int dvbsub_parse_object_segment(AVCodecContext *avctx,
983 const uint8_t *buf, int buf_size)
984 {
985 42 DVBSubContext *ctx = avctx->priv_data;
986
987 42 const uint8_t *buf_end = buf + buf_size;
988 int object_id;
989 DVBSubObject *object;
990 DVBSubObjectDisplay *display;
991 int top_field_len, bottom_field_len;
992
993 int coding_method, non_modifying_color;
994
995 42 object_id = AV_RB16(buf);
996 42 buf += 2;
997
998 42 object = get_object(ctx, object_id);
999
1000
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!object)
1001 return AVERROR_INVALIDDATA;
1002
1003 42 coding_method = ((*buf) >> 2) & 3;
1004 42 non_modifying_color = ((*buf++) >> 1) & 1;
1005
1006
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (coding_method == 0) {
1007 42 top_field_len = AV_RB16(buf);
1008 42 buf += 2;
1009 42 bottom_field_len = AV_RB16(buf);
1010 42 buf += 2;
1011
1012
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (buf + top_field_len + bottom_field_len > buf_end) {
1013 av_log(avctx, AV_LOG_ERROR, "Field data size %d+%d too large\n", top_field_len, bottom_field_len);
1014 return AVERROR_INVALIDDATA;
1015 }
1016
1017
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
84 for (display = object->display_list; display; display = display->object_list_next) {
1018 42 const uint8_t *block = buf;
1019 42 int bfl = bottom_field_len;
1020
1021 42 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
1022 non_modifying_color);
1023
1024
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (bottom_field_len > 0)
1025 42 block = buf + top_field_len;
1026 else
1027 bfl = top_field_len;
1028
1029 42 dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
1030 non_modifying_color);
1031 }
1032 } else if (coding_method == 1) {
1033 avpriv_report_missing_feature(avctx, "coded as a string of characters");
1034 return AVERROR_PATCHWELCOME;
1035 } else if (coding_method == 2) {
1036 avpriv_report_missing_feature(avctx, "progressive coding of pixels");
1037 return AVERROR_PATCHWELCOME;
1038 } else {
1039 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
1040 return AVERROR_INVALIDDATA;
1041 }
1042
1043 42 return 0;
1044 }
1045
1046 13 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
1047 const uint8_t *buf, int buf_size)
1048 {
1049 13 DVBSubContext *ctx = avctx->priv_data;
1050
1051 13 const uint8_t *buf_end = buf + buf_size;
1052 int i, clut_id;
1053 int version;
1054 DVBSubCLUT *clut;
1055 int entry_id, depth , full_range;
1056 int y, cr, cb, alpha;
1057 int r, g, b, r_add, g_add, b_add;
1058
1059 ff_dlog(avctx, "DVB clut packet:\n");
1060
1061
2/2
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 13 times.
741 for (i=0; i < buf_size; i++) {
1062 ff_dlog(avctx, "%02x ", buf[i]);
1063 728 if (i % 16 == 15)
1064 ff_dlog(avctx, "\n");
1065 }
1066
1067 13 if (i % 16)
1068 ff_dlog(avctx, "\n");
1069
1070 13 clut_id = *buf++;
1071 13 version = ((*buf)>>4)&15;
1072 13 buf += 1;
1073
1074 13 clut = get_clut(ctx, clut_id);
1075
1076
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (!clut) {
1077 13 clut = av_memdup(&default_clut, sizeof(*clut));
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!clut)
1079 return AVERROR(ENOMEM);
1080
1081 13 clut->id = clut_id;
1082 13 clut->version = -1;
1083
1084 13 clut->next = ctx->clut_list;
1085 13 ctx->clut_list = clut;
1086 }
1087
1088
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (clut->version != version) {
1089
1090 13 clut->version = version;
1091
1092
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 13 times.
130 while (buf + 4 < buf_end) {
1093 117 entry_id = *buf++;
1094
1095 117 depth = (*buf) & 0xe0;
1096
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (depth == 0) {
1098 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
1099 }
1100
1101 117 full_range = (*buf++) & 1;
1102
1103
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 if (full_range) {
1104 117 y = *buf++;
1105 117 cr = *buf++;
1106 117 cb = *buf++;
1107 117 alpha = *buf++;
1108 } else {
1109 y = buf[0] & 0xfc;
1110 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1111 cb = (buf[1] << 2) & 0xf0;
1112 alpha = (buf[1] << 6) & 0xc0;
1113
1114 buf += 2;
1115 }
1116
1117
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 104 times.
117 if (y == 0)
1118 13 alpha = 0xff;
1119
1120 117 YUV_TO_RGB1_CCIR(cb, cr);
1121 117 YUV_TO_RGB2_CCIR(r, g, b, y);
1122
1123 ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1125 ff_dlog(avctx, "More than one bit level marked: %x\n", depth);
1126 if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
1127 return AVERROR_INVALIDDATA;
1128 }
1129
1130
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
117 if (depth & 0x80 && entry_id < 4)
1131 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1132
2/4
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
117 else if (depth & 0x40 && entry_id < 16)
1133 117 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1134 else if (depth & 0x20)
1135 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1136 }
1137 }
1138
1139 13 return 0;
1140 }
1141
1142
1143 57 static int dvbsub_parse_region_segment(AVCodecContext *avctx,
1144 const uint8_t *buf, int buf_size)
1145 {
1146 57 DVBSubContext *ctx = avctx->priv_data;
1147
1148 57 const uint8_t *buf_end = buf + buf_size;
1149 int region_id, object_id;
1150 int av_unused version;
1151 DVBSubRegion *region;
1152 DVBSubObject *object;
1153 DVBSubObjectDisplay *display;
1154 int fill;
1155 int ret;
1156
1157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (buf_size < 10)
1158 return AVERROR_INVALIDDATA;
1159
1160 57 region_id = *buf++;
1161
1162 57 region = get_region(ctx, region_id);
1163
1164
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 29 times.
57 if (!region) {
1165 28 region = av_mallocz(sizeof(*region));
1166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!region)
1167 return AVERROR(ENOMEM);
1168
1169 28 region->id = region_id;
1170 28 region->version = -1;
1171
1172 28 region->next = ctx->region_list;
1173 28 ctx->region_list = region;
1174 }
1175
1176 57 version = ((*buf)>>4) & 15;
1177 57 fill = ((*buf++) >> 3) & 1;
1178
1179 57 region->width = AV_RB16(buf);
1180 57 buf += 2;
1181 57 region->height = AV_RB16(buf);
1182 57 buf += 2;
1183
1184 57 ret = av_image_check_size2(region->width, region->height, avctx->max_pixels, AV_PIX_FMT_PAL8, 0, avctx);
1185
2/4
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 57 times.
57 if (ret >= 0 && region->width * region->height * 2 > 320 * 1024 * 8) {
1186 ret = AVERROR_INVALIDDATA;
1187 av_log(avctx, AV_LOG_ERROR, "Pixel buffer memory constraint violated\n");
1188 }
1189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0) {
1190 region->width= region->height= 0;
1191 return ret;
1192 }
1193
1194
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 29 times.
57 if (region->width * region->height != region->buf_size) {
1195 28 av_free(region->pbuf);
1196
1197 28 region->buf_size = region->width * region->height;
1198
1199 28 region->pbuf = av_malloc(region->buf_size);
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!region->pbuf) {
1201 region->buf_size =
1202 region->width =
1203 region->height = 0;
1204 return AVERROR(ENOMEM);
1205 }
1206
1207 28 fill = 1;
1208 28 region->dirty = 0;
1209 }
1210
1211 57 region->depth = 1 << (((*buf++) >> 2) & 7);
1212
2/4
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 57 times.
57 if (region->depth < 2 || region->depth > 8) {
1213 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1214 region->depth= 4;
1215 }
1216 57 region->clut = *buf++;
1217
1218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (region->depth == 8) {
1219 region->bgcolor = *buf++;
1220 buf += 1;
1221 } else {
1222 57 buf += 1;
1223
1224
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 if (region->depth == 4)
1225 57 region->bgcolor = (((*buf++) >> 4) & 15);
1226 else
1227 region->bgcolor = (((*buf++) >> 2) & 3);
1228 }
1229
1230 ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1231
1232
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 29 times.
57 if (fill) {
1233 28 memset(region->pbuf, region->bgcolor, region->buf_size);
1234 ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1235 }
1236
1237 57 delete_region_display_list(ctx, region);
1238
1239
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 57 times.
99 while (buf + 5 < buf_end) {
1240 42 object_id = AV_RB16(buf);
1241 42 buf += 2;
1242
1243 42 object = get_object(ctx, object_id);
1244
1245
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (!object) {
1246 42 object = av_mallocz(sizeof(*object));
1247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!object)
1248 return AVERROR(ENOMEM);
1249
1250 42 object->id = object_id;
1251 42 object->next = ctx->object_list;
1252 42 ctx->object_list = object;
1253 }
1254
1255 42 object->type = (*buf) >> 6;
1256
1257 42 display = av_mallocz(sizeof(*display));
1258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!display)
1259 return AVERROR(ENOMEM);
1260
1261 42 display->object_id = object_id;
1262 42 display->region_id = region_id;
1263
1264 42 display->x_pos = AV_RB16(buf) & 0xfff;
1265 42 buf += 2;
1266 42 display->y_pos = AV_RB16(buf) & 0xfff;
1267 42 buf += 2;
1268
1269
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (display->x_pos >= region->width ||
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 display->y_pos >= region->height) {
1271 av_log(avctx, AV_LOG_ERROR, "Object outside region\n");
1272 av_free(display);
1273 return AVERROR_INVALIDDATA;
1274 }
1275
1276
2/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
42 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1277 display->fgcolor = *buf++;
1278 display->bgcolor = *buf++;
1279 }
1280
1281 42 display->region_list_next = region->display_list;
1282 42 region->display_list = display;
1283
1284 42 display->object_list_next = object->display_list;
1285 42 object->display_list = display;
1286 }
1287
1288 57 return 0;
1289 }
1290
1291 46 static int dvbsub_parse_page_segment(AVCodecContext *avctx,
1292 const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
1293 {
1294 46 DVBSubContext *ctx = avctx->priv_data;
1295 DVBSubRegionDisplay *display;
1296 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1297
1298 46 const uint8_t *buf_end = buf + buf_size;
1299 int region_id;
1300 int page_state;
1301 int timeout;
1302 int version;
1303
1304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (buf_size < 1)
1305 return AVERROR_INVALIDDATA;
1306
1307 46 timeout = *buf++;
1308 46 version = ((*buf)>>4) & 15;
1309 46 page_state = ((*buf++) >> 2) & 3;
1310
1311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ctx->version == version) {
1312 return 0;
1313 }
1314
1315 46 ctx->time_out = timeout;
1316 46 ctx->version = version;
1317
1318 ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1319
1320
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (ctx->compute_edt == 1)
1321 46 save_subtitle_set(avctx, sub, got_output);
1322
1323
3/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
46 if (page_state == 1 || page_state == 2) {
1324 7 delete_regions(ctx);
1325 7 delete_objects(ctx);
1326 7 delete_cluts(ctx);
1327 }
1328
1329 46 tmp_display_list = ctx->display_list;
1330 46 ctx->display_list = NULL;
1331
1332
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 46 times.
112 while (buf + 5 < buf_end) {
1333 66 region_id = *buf++;
1334 66 buf += 1;
1335
1336 66 display = ctx->display_list;
1337
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
96 while (display && display->region_id != region_id) {
1338 30 display = display->next;
1339 }
1340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
66 if (display) {
1341 av_log(avctx, AV_LOG_ERROR, "duplicate region\n");
1342 break;
1343 }
1344
1345 66 display = tmp_display_list;
1346 66 tmp_ptr = &tmp_display_list;
1347
1348
4/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 64 times.
95 while (display && display->region_id != region_id) {
1349 29 tmp_ptr = &display->next;
1350 29 display = display->next;
1351 }
1352
1353
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 64 times.
66 if (!display) {
1354 2 display = av_mallocz(sizeof(*display));
1355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!display)
1356 return AVERROR(ENOMEM);
1357 }
1358
1359 66 display->region_id = region_id;
1360
1361 66 display->x_pos = AV_RB16(buf);
1362 66 buf += 2;
1363 66 display->y_pos = AV_RB16(buf);
1364 66 buf += 2;
1365
1366 66 *tmp_ptr = display->next;
1367
1368 66 display->next = ctx->display_list;
1369 66 ctx->display_list = display;
1370
1371 ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1372 }
1373
1374
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 46 times.
48 while (tmp_display_list) {
1375 2 display = tmp_display_list;
1376
1377 2 tmp_display_list = display->next;
1378
1379 2 av_freep(&display);
1380 }
1381
1382 46 return 0;
1383 }
1384
1385 static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1386 const uint8_t *buf,
1387 int buf_size)
1388 {
1389 DVBSubContext *ctx = avctx->priv_data;
1390 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1391 int dds_version, info_byte;
1392
1393 if (buf_size < 5)
1394 return AVERROR_INVALIDDATA;
1395
1396 info_byte = bytestream_get_byte(&buf);
1397 dds_version = info_byte >> 4;
1398 if (display_def && display_def->version == dds_version)
1399 return 0; // already have this display definition version
1400
1401 if (!display_def) {
1402 display_def = av_mallocz(sizeof(*display_def));
1403 if (!display_def)
1404 return AVERROR(ENOMEM);
1405 ctx->display_definition = display_def;
1406 }
1407
1408 display_def->version = dds_version;
1409 display_def->x = 0;
1410 display_def->y = 0;
1411 display_def->width = bytestream_get_be16(&buf) + 1;
1412 display_def->height = bytestream_get_be16(&buf) + 1;
1413 if (!avctx->width || !avctx->height) {
1414 int ret = ff_set_dimensions(avctx, display_def->width, display_def->height);
1415 if (ret < 0)
1416 return ret;
1417 }
1418
1419 if (info_byte & 1<<3) { // display_window_flag
1420 if (buf_size < 13)
1421 return AVERROR_INVALIDDATA;
1422
1423 display_def->x = bytestream_get_be16(&buf);
1424 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1425 display_def->y = bytestream_get_be16(&buf);
1426 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1427 }
1428
1429 return 0;
1430 }
1431
1432 46 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1433 int buf_size, AVSubtitle *sub,int *got_output)
1434 {
1435 46 DVBSubContext *ctx = avctx->priv_data;
1436
1437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ctx->compute_edt == 0)
1438 save_subtitle_set(avctx, sub, got_output);
1439 46 return 0;
1440 }
1441
1442 46 static int dvbsub_decode(AVCodecContext *avctx, AVSubtitle *sub,
1443 int *got_sub_ptr, const AVPacket *avpkt)
1444 {
1445 46 const uint8_t *buf = avpkt->data;
1446 46 int buf_size = avpkt->size;
1447 46 DVBSubContext *ctx = avctx->priv_data;
1448 const uint8_t *p, *p_end;
1449 int segment_type;
1450 int page_id;
1451 int segment_length;
1452 int i;
1453 46 int ret = 0;
1454 46 int got_segment = 0;
1455 46 int got_dds = 0;
1456
1457 ff_dlog(avctx, "DVB sub packet:\n");
1458
1459
2/2
✓ Branch 0 taken 51158 times.
✓ Branch 1 taken 46 times.
51204 for (i=0; i < buf_size; i++) {
1460 ff_dlog(avctx, "%02x ", buf[i]);
1461 51158 if (i % 16 == 15)
1462 ff_dlog(avctx, "\n");
1463 }
1464
1465 46 if (i % 16)
1466 ff_dlog(avctx, "\n");
1467
1468
2/4
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
46 if (buf_size <= 6 || *buf != 0x0f) {
1469 ff_dlog(avctx, "incomplete or broken packet");
1470 return AVERROR_INVALIDDATA;
1471 }
1472
1473 46 p = buf;
1474 46 p_end = buf + buf_size;
1475
1476
3/4
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
250 while (p_end - p >= 6 && *p == 0x0f) {
1477 204 p += 1;
1478 204 segment_type = *p++;
1479 204 page_id = AV_RB16(p);
1480 204 p += 2;
1481 204 segment_length = AV_RB16(p);
1482 204 p += 2;
1483
1484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 if (avctx->debug & FF_DEBUG_STARTCODE) {
1485 av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
1486 }
1487
1488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 if (p_end - p < segment_length) {
1489 ff_dlog(avctx, "incomplete or broken packet");
1490 ret = -1;
1491 goto end;
1492 }
1493
1494
2/4
✓ Branch 0 taken 204 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
204 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1495
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
204 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1496 204 int ret = 0;
1497
5/7
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 42 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 46 times.
✗ Branch 6 not taken.
204 switch (segment_type) {
1498 46 case DVBSUB_PAGE_SEGMENT:
1499 46 ret = dvbsub_parse_page_segment(avctx, p, segment_length, sub, got_sub_ptr);
1500 46 got_segment |= 1;
1501 46 break;
1502 57 case DVBSUB_REGION_SEGMENT:
1503 57 ret = dvbsub_parse_region_segment(avctx, p, segment_length);
1504 57 got_segment |= 2;
1505 57 break;
1506 13 case DVBSUB_CLUT_SEGMENT:
1507 13 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0) goto end;
1509 13 got_segment |= 4;
1510 13 break;
1511 42 case DVBSUB_OBJECT_SEGMENT:
1512 42 ret = dvbsub_parse_object_segment(avctx, p, segment_length);
1513 42 got_segment |= 8;
1514 42 break;
1515 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1516 ret = dvbsub_parse_display_definition_segment(avctx, p,
1517 segment_length);
1518 got_dds = 1;
1519 break;
1520 46 case DVBSUB_DISPLAY_SEGMENT:
1521 46 ret = dvbsub_display_end_segment(avctx, p, segment_length, sub, got_sub_ptr);
1522
6/8
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
46 if (got_segment == 15 && !got_dds && !avctx->width && !avctx->height) {
1523 // Default from ETSI EN 300 743 V1.3.1 (7.2.1)
1524 1 avctx->width = 720;
1525 1 avctx->height = 576;
1526 }
1527 46 got_segment |= 16;
1528 46 break;
1529 default:
1530 ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1531 segment_type, page_id, segment_length);
1532 break;
1533 }
1534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 204 times.
204 if (ret < 0)
1535 goto end;
1536 }
1537
1538 204 p += segment_length;
1539 }
1540 // Some streams do not send a display segment but if we have all the other
1541 // segments then we need no further data.
1542
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (got_segment == 15) {
1543 av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
1544 dvbsub_display_end_segment(avctx, p, 0, sub, got_sub_ptr);
1545 }
1546
1547 46 end:
1548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret < 0) {
1549 return ret;
1550 } else {
1551
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (ctx->compute_edt == 1)
1552 46 FFSWAP(int64_t, ctx->prev_start, sub->pts);
1553 }
1554
1555 46 return p - buf;
1556 }
1557
1558 #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
1559 #define OFFSET(x) offsetof(DVBSubContext, x)
1560 static const AVOption options[] = {
1561 {"compute_edt", "compute end of time using pts or timeout", OFFSET(compute_edt), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DS},
1562 {"compute_clut", "compute clut when not available(-1) or only once (-2) or always(1) or never(0)", OFFSET(compute_clut), AV_OPT_TYPE_BOOL, {.i64 = -1}, -2, 1, DS},
1563 {"dvb_substream", "", OFFSET(substream), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, DS},
1564 {NULL}
1565 };
1566 static const AVClass dvbsubdec_class = {
1567 .class_name = "DVB Sub Decoder",
1568 .item_name = av_default_item_name,
1569 .option = options,
1570 .version = LIBAVUTIL_VERSION_INT,
1571 };
1572
1573 const FFCodec ff_dvbsub_decoder = {
1574 .p.name = "dvbsub",
1575 CODEC_LONG_NAME("DVB subtitles"),
1576 .p.type = AVMEDIA_TYPE_SUBTITLE,
1577 .p.id = AV_CODEC_ID_DVB_SUBTITLE,
1578 .priv_data_size = sizeof(DVBSubContext),
1579 .init = dvbsub_init_decoder,
1580 .close = dvbsub_close_decoder,
1581 FF_CODEC_DECODE_SUB_CB(dvbsub_decode),
1582 .p.priv_class = &dvbsubdec_class,
1583 };
1584