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