FFmpeg coverage


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