/src/ffmpeg/libavcodec/pgssubdec.c
Line | Count | Source |
1 | | /* |
2 | | * PGS subtitle decoder |
3 | | * Copyright (c) 2009 Stephen Backway |
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 | | /** |
23 | | * @file |
24 | | * PGS subtitle decoder |
25 | | */ |
26 | | |
27 | | #include "avcodec.h" |
28 | | #include "bytestream.h" |
29 | | #include "codec_internal.h" |
30 | | #include "decode.h" |
31 | | #include "mathops.h" |
32 | | |
33 | | #include "libavutil/colorspace.h" |
34 | | #include "libavutil/mem.h" |
35 | | #include "libavutil/opt.h" |
36 | | |
37 | 149k | #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)) |
38 | 1.48k | #define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch |
39 | 4.38k | #define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch |
40 | 6.04k | #define MAX_OBJECT_REFS 2 // Max objects per display set |
41 | | |
42 | | enum SegmentType { |
43 | | PALETTE_SEGMENT = 0x14, |
44 | | OBJECT_SEGMENT = 0x15, |
45 | | PRESENTATION_SEGMENT = 0x16, |
46 | | WINDOW_SEGMENT = 0x17, |
47 | | DISPLAY_SEGMENT = 0x80, |
48 | | }; |
49 | | |
50 | | typedef struct PGSSubObjectRef { |
51 | | int id; |
52 | | int window_id; |
53 | | uint8_t composition_flag; |
54 | | int x; |
55 | | int y; |
56 | | int crop_x; |
57 | | int crop_y; |
58 | | int crop_w; |
59 | | int crop_h; |
60 | | } PGSSubObjectRef; |
61 | | |
62 | | typedef struct PGSSubPresentation { |
63 | | int id_number; |
64 | | int palette_id; |
65 | | int object_count; |
66 | | PGSSubObjectRef objects[MAX_OBJECT_REFS]; |
67 | | int64_t pts; |
68 | | } PGSSubPresentation; |
69 | | |
70 | | typedef struct PGSSubObject { |
71 | | int id; |
72 | | int w; |
73 | | int h; |
74 | | uint8_t *rle; |
75 | | unsigned int rle_buffer_size, rle_data_len; |
76 | | unsigned int rle_remaining_len; |
77 | | } PGSSubObject; |
78 | | |
79 | | typedef struct PGSSubObjects { |
80 | | int count; |
81 | | PGSSubObject object[MAX_EPOCH_OBJECTS]; |
82 | | } PGSSubObjects; |
83 | | |
84 | | typedef struct PGSSubPalette { |
85 | | int id; |
86 | | uint32_t clut[256]; |
87 | | } PGSSubPalette; |
88 | | |
89 | | typedef struct PGSSubPalettes { |
90 | | int count; |
91 | | PGSSubPalette palette[MAX_EPOCH_PALETTES]; |
92 | | } PGSSubPalettes; |
93 | | |
94 | | typedef struct PGSSubContext { |
95 | | AVClass *class; |
96 | | PGSSubPresentation presentation; |
97 | | PGSSubPalettes palettes; |
98 | | PGSSubObjects objects; |
99 | | int forced_subs_only; |
100 | | } PGSSubContext; |
101 | | |
102 | | static void flush_cache(AVCodecContext *avctx) |
103 | 4.47k | { |
104 | 4.47k | PGSSubContext *ctx = avctx->priv_data; |
105 | 4.47k | int i; |
106 | | |
107 | 8.66k | for (i = 0; i < ctx->objects.count; i++) { |
108 | 4.19k | av_freep(&ctx->objects.object[i].rle); |
109 | 4.19k | ctx->objects.object[i].rle_buffer_size = 0; |
110 | 4.19k | ctx->objects.object[i].rle_remaining_len = 0; |
111 | 4.19k | } |
112 | 4.47k | ctx->objects.count = 0; |
113 | 4.47k | ctx->palettes.count = 0; |
114 | 4.47k | } |
115 | | |
116 | | static PGSSubObject * find_object(int id, PGSSubObjects *objects) |
117 | 10.7k | { |
118 | 10.7k | int i; |
119 | | |
120 | 75.1k | for (i = 0; i < objects->count; i++) { |
121 | 69.3k | if (objects->object[i].id == id) |
122 | 4.97k | return &objects->object[i]; |
123 | 69.3k | } |
124 | 5.76k | return NULL; |
125 | 10.7k | } |
126 | | |
127 | | static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes) |
128 | 5.53k | { |
129 | 5.53k | int i; |
130 | | |
131 | 10.7k | for (i = 0; i < palettes->count; i++) { |
132 | 8.27k | if (palettes->palette[i].id == id) |
133 | 3.09k | return &palettes->palette[i]; |
134 | 8.27k | } |
135 | 2.44k | return NULL; |
136 | 5.53k | } |
137 | | |
138 | | static av_cold int init_decoder(AVCodecContext *avctx) |
139 | 1.65k | { |
140 | 1.65k | avctx->pix_fmt = AV_PIX_FMT_PAL8; |
141 | | |
142 | 1.65k | return 0; |
143 | 1.65k | } |
144 | | |
145 | | static av_cold int close_decoder(AVCodecContext *avctx) |
146 | 1.65k | { |
147 | 1.65k | flush_cache(avctx); |
148 | | |
149 | 1.65k | return 0; |
150 | 1.65k | } |
151 | | |
152 | | /** |
153 | | * Decode the RLE data. |
154 | | * |
155 | | * The subtitle is stored as a Run Length Encoded image. |
156 | | * |
157 | | * @param avctx contains the current codec context |
158 | | * @param sub pointer to the processed subtitle data |
159 | | * @param buf pointer to the RLE data to process |
160 | | * @param buf_size size of the RLE data to process |
161 | | */ |
162 | | static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect, |
163 | | const uint8_t *buf, unsigned int buf_size) |
164 | 1.20k | { |
165 | 1.20k | const uint8_t *rle_bitmap_end; |
166 | 1.20k | int pixel_count, line_count; |
167 | | |
168 | 1.20k | rle_bitmap_end = buf + buf_size; |
169 | | |
170 | 1.20k | rect->data[0] = av_malloc_array(rect->w, rect->h); |
171 | | |
172 | 1.20k | if (!rect->data[0]) |
173 | 0 | return AVERROR(ENOMEM); |
174 | | |
175 | 1.20k | pixel_count = 0; |
176 | 1.20k | line_count = 0; |
177 | | |
178 | 6.95k | while (buf < rle_bitmap_end && line_count < rect->h) { |
179 | 5.95k | uint8_t flags, color; |
180 | 5.95k | int run; |
181 | | |
182 | 5.95k | color = bytestream_get_byte(&buf); |
183 | 5.95k | run = 1; |
184 | | |
185 | 5.95k | if (color == 0x00) { |
186 | 2.76k | flags = bytestream_get_byte(&buf); |
187 | 2.76k | run = flags & 0x3f; |
188 | 2.76k | if (flags & 0x40) |
189 | 738 | run = (run << 8) + bytestream_get_byte(&buf); |
190 | 2.76k | color = flags & 0x80 ? bytestream_get_byte(&buf) : 0; |
191 | 2.76k | } |
192 | | |
193 | 5.95k | if (run > 0 && pixel_count + run <= rect->w * rect->h) { |
194 | 3.51k | memset(rect->data[0] + pixel_count, color, run); |
195 | 3.51k | pixel_count += run; |
196 | 3.51k | } else if (!run) { |
197 | | /* |
198 | | * New Line. Check if correct pixels decoded, if not display warning |
199 | | * and adjust bitmap pointer to correct new line position. |
200 | | */ |
201 | 1.54k | if (pixel_count % rect->w > 0) { |
202 | 1.09k | av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n", |
203 | 1.09k | pixel_count % rect->w, rect->w); |
204 | 1.09k | if (avctx->err_recognition & AV_EF_EXPLODE) { |
205 | 209 | return AVERROR_INVALIDDATA; |
206 | 209 | } |
207 | 1.09k | } |
208 | 1.33k | line_count++; |
209 | 1.33k | } |
210 | 5.95k | } |
211 | | |
212 | 997 | if (pixel_count < rect->w * rect->h) { |
213 | 768 | av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n"); |
214 | 768 | return AVERROR_INVALIDDATA; |
215 | 768 | } |
216 | | |
217 | 229 | ff_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, rect->w * rect->h); |
218 | | |
219 | 229 | return 0; |
220 | 997 | } |
221 | | |
222 | | /** |
223 | | * Parse the picture segment packet. |
224 | | * |
225 | | * The picture segment contains details on the sequence id, |
226 | | * width, height and Run Length Encoded (RLE) bitmap data. |
227 | | * |
228 | | * @param avctx contains the current codec context |
229 | | * @param buf pointer to the packet to process |
230 | | * @param buf_size size of packet to process |
231 | | */ |
232 | | static int parse_object_segment(AVCodecContext *avctx, |
233 | | const uint8_t *buf, int buf_size) |
234 | 7.47k | { |
235 | 7.47k | PGSSubContext *ctx = avctx->priv_data; |
236 | 7.47k | PGSSubObject *object; |
237 | | |
238 | 7.47k | uint8_t sequence_desc; |
239 | 7.47k | unsigned int rle_bitmap_len, width, height; |
240 | 7.47k | int id; |
241 | | |
242 | 7.47k | if (buf_size <= 4) |
243 | 236 | return AVERROR_INVALIDDATA; |
244 | 7.24k | buf_size -= 4; |
245 | | |
246 | 7.24k | id = bytestream_get_be16(&buf); |
247 | 7.24k | object = find_object(id, &ctx->objects); |
248 | 7.24k | if (!object) { |
249 | 4.38k | if (ctx->objects.count >= MAX_EPOCH_OBJECTS) { |
250 | 195 | av_log(avctx, AV_LOG_ERROR, "Too many objects in epoch\n"); |
251 | 195 | return AVERROR_INVALIDDATA; |
252 | 195 | } |
253 | 4.19k | object = &ctx->objects.object[ctx->objects.count++]; |
254 | 4.19k | object->id = id; |
255 | 4.19k | } |
256 | | |
257 | | /* skip object version number */ |
258 | 7.04k | buf += 1; |
259 | | |
260 | | /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */ |
261 | 7.04k | sequence_desc = bytestream_get_byte(&buf); |
262 | | |
263 | 7.04k | if (!(sequence_desc & 0x80)) { |
264 | | /* Additional RLE data */ |
265 | 2.39k | if (buf_size > object->rle_remaining_len) |
266 | 2.18k | return AVERROR_INVALIDDATA; |
267 | | |
268 | 215 | memcpy(object->rle + object->rle_data_len, buf, buf_size); |
269 | 215 | object->rle_data_len += buf_size; |
270 | 215 | object->rle_remaining_len -= buf_size; |
271 | | |
272 | 215 | return 0; |
273 | 2.39k | } |
274 | | |
275 | 4.64k | if (buf_size <= 7) |
276 | 1.15k | return AVERROR_INVALIDDATA; |
277 | 3.49k | buf_size -= 7; |
278 | | |
279 | | /* Decode rle bitmap length, stored size includes width/height data */ |
280 | 3.49k | rle_bitmap_len = bytestream_get_be24(&buf) - 2*2; |
281 | | |
282 | 3.49k | if (buf_size > rle_bitmap_len) { |
283 | 231 | av_log(avctx, AV_LOG_ERROR, |
284 | 231 | "Buffer dimension %d larger than the expected RLE data %d\n", |
285 | 231 | buf_size, rle_bitmap_len); |
286 | 231 | return AVERROR_INVALIDDATA; |
287 | 231 | } |
288 | | |
289 | | /* Get bitmap dimensions from data */ |
290 | 3.25k | width = bytestream_get_be16(&buf); |
291 | 3.25k | height = bytestream_get_be16(&buf); |
292 | | |
293 | | /* Make sure the bitmap is not too large */ |
294 | 3.25k | if (avctx->width < width || avctx->height < height || !width || !height) { |
295 | 1.85k | av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions (%dx%d) invalid.\n", width, height); |
296 | 1.85k | return AVERROR_INVALIDDATA; |
297 | 1.85k | } |
298 | | |
299 | 1.40k | object->w = width; |
300 | 1.40k | object->h = height; |
301 | | |
302 | 1.40k | av_fast_padded_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len); |
303 | | |
304 | 1.40k | if (!object->rle) { |
305 | 364 | object->rle_data_len = 0; |
306 | 364 | object->rle_remaining_len = 0; |
307 | 364 | return AVERROR(ENOMEM); |
308 | 364 | } |
309 | | |
310 | 1.04k | memcpy(object->rle, buf, buf_size); |
311 | 1.04k | object->rle_data_len = buf_size; |
312 | 1.04k | object->rle_remaining_len = rle_bitmap_len - buf_size; |
313 | | |
314 | 1.04k | return 0; |
315 | 1.40k | } |
316 | | |
317 | | /** |
318 | | * Parse the palette segment packet. |
319 | | * |
320 | | * The palette segment contains details of the palette, |
321 | | * a maximum of 256 colors can be defined. |
322 | | * |
323 | | * @param avctx contains the current codec context |
324 | | * @param buf pointer to the packet to process |
325 | | * @param buf_size size of packet to process |
326 | | */ |
327 | | static int parse_palette_segment(AVCodecContext *avctx, |
328 | | const uint8_t *buf, int buf_size) |
329 | 2.17k | { |
330 | 2.17k | PGSSubContext *ctx = avctx->priv_data; |
331 | 2.17k | PGSSubPalette *palette; |
332 | | |
333 | 2.17k | const uint8_t *buf_end = buf + buf_size; |
334 | 2.17k | const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; |
335 | 2.17k | int color_id; |
336 | 2.17k | int y, cb, cr, alpha; |
337 | 2.17k | int r, g, b, r_add, g_add, b_add; |
338 | 2.17k | int id; |
339 | | |
340 | 2.17k | id = bytestream_get_byte(&buf); |
341 | 2.17k | palette = find_palette(id, &ctx->palettes); |
342 | 2.17k | if (!palette) { |
343 | 1.48k | if (ctx->palettes.count >= MAX_EPOCH_PALETTES) { |
344 | 350 | av_log(avctx, AV_LOG_ERROR, "Too many palettes in epoch\n"); |
345 | 350 | return AVERROR_INVALIDDATA; |
346 | 350 | } |
347 | 1.13k | palette = &ctx->palettes.palette[ctx->palettes.count++]; |
348 | 1.13k | palette->id = id; |
349 | 1.13k | } |
350 | | |
351 | | /* Skip palette version */ |
352 | 1.82k | buf += 1; |
353 | | |
354 | 151k | while (buf < buf_end) { |
355 | 149k | color_id = bytestream_get_byte(&buf); |
356 | 149k | y = bytestream_get_byte(&buf); |
357 | 149k | cr = bytestream_get_byte(&buf); |
358 | 149k | cb = bytestream_get_byte(&buf); |
359 | 149k | alpha = bytestream_get_byte(&buf); |
360 | | |
361 | | /* Default to BT.709 colorspace. In case of <= 576 height use BT.601 */ |
362 | 149k | if (avctx->height <= 0 || avctx->height > 576) { |
363 | 145k | YUV_TO_RGB1_CCIR_BT709(cb, cr); |
364 | 145k | } else { |
365 | 3.46k | YUV_TO_RGB1_CCIR(cb, cr); |
366 | 3.46k | } |
367 | | |
368 | 149k | YUV_TO_RGB2_CCIR(r, g, b, y); |
369 | | |
370 | 149k | ff_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha); |
371 | | |
372 | | /* Store color in palette */ |
373 | 149k | palette->clut[color_id] = RGBA(r,g,b,alpha); |
374 | 149k | } |
375 | 1.82k | return 0; |
376 | 2.17k | } |
377 | | |
378 | | /** |
379 | | * Parse the presentation segment packet. |
380 | | * |
381 | | * The presentation segment contains details on the video |
382 | | * width, video height, x & y subtitle position. |
383 | | * |
384 | | * @param avctx contains the current codec context |
385 | | * @param buf pointer to the packet to process |
386 | | * @param buf_size size of packet to process |
387 | | * @todo TODO: Implement cropping |
388 | | */ |
389 | | static int parse_presentation_segment(AVCodecContext *avctx, |
390 | | const uint8_t *buf, int buf_size, |
391 | | int64_t pts) |
392 | 7.84k | { |
393 | 7.84k | PGSSubContext *ctx = avctx->priv_data; |
394 | 7.84k | int i, state, ret; |
395 | 7.84k | const uint8_t *buf_end = buf + buf_size; |
396 | | |
397 | | // Video descriptor |
398 | 7.84k | int w = bytestream_get_be16(&buf); |
399 | 7.84k | int h = bytestream_get_be16(&buf); |
400 | | |
401 | 7.84k | ctx->presentation.pts = pts; |
402 | | |
403 | 7.84k | ff_dlog(avctx, "Video Dimensions %dx%d\n", |
404 | 7.84k | w, h); |
405 | 7.84k | ret = ff_set_dimensions(avctx, w, h); |
406 | 7.84k | if (ret < 0) |
407 | 1.79k | return ret; |
408 | | |
409 | | /* Skip 1 bytes of unknown, frame rate */ |
410 | 6.04k | buf++; |
411 | | |
412 | | // Composition descriptor |
413 | 6.04k | ctx->presentation.id_number = bytestream_get_be16(&buf); |
414 | | /* |
415 | | * state is a 2 bit field that defines pgs epoch boundaries |
416 | | * 00 - Normal, previously defined objects and palettes are still valid |
417 | | * 01 - Acquisition point, previous objects and palettes can be released |
418 | | * 10 - Epoch start, previous objects and palettes can be released |
419 | | * 11 - Epoch continue, previous objects and palettes can be released |
420 | | * |
421 | | * reserved 6 bits discarded |
422 | | */ |
423 | 6.04k | state = bytestream_get_byte(&buf) >> 6; |
424 | 6.04k | if (state != 0) { |
425 | 2.82k | flush_cache(avctx); |
426 | 2.82k | } |
427 | | |
428 | | /* |
429 | | * skip palette_update_flag (0x80), |
430 | | */ |
431 | 6.04k | buf += 1; |
432 | 6.04k | ctx->presentation.palette_id = bytestream_get_byte(&buf); |
433 | 6.04k | ctx->presentation.object_count = bytestream_get_byte(&buf); |
434 | 6.04k | if (ctx->presentation.object_count > MAX_OBJECT_REFS) { |
435 | 3.41k | av_log(avctx, AV_LOG_ERROR, |
436 | 3.41k | "Invalid number of presentation objects %d\n", |
437 | 3.41k | ctx->presentation.object_count); |
438 | 3.41k | ctx->presentation.object_count = 2; |
439 | 3.41k | if (avctx->err_recognition & AV_EF_EXPLODE) { |
440 | 233 | return AVERROR_INVALIDDATA; |
441 | 233 | } |
442 | 3.41k | } |
443 | | |
444 | | |
445 | 8.36k | for (i = 0; i < ctx->presentation.object_count; i++) |
446 | 5.39k | { |
447 | 5.39k | PGSSubObjectRef *const object = &ctx->presentation.objects[i]; |
448 | | |
449 | 5.39k | if (buf_end - buf < 8) { |
450 | 2.64k | av_log(avctx, AV_LOG_ERROR, "Insufficient space for object\n"); |
451 | 2.64k | ctx->presentation.object_count = i; |
452 | 2.64k | return AVERROR_INVALIDDATA; |
453 | 2.64k | } |
454 | | |
455 | 2.75k | object->id = bytestream_get_be16(&buf); |
456 | 2.75k | object->window_id = bytestream_get_byte(&buf); |
457 | 2.75k | object->composition_flag = bytestream_get_byte(&buf); |
458 | | |
459 | 2.75k | object->x = bytestream_get_be16(&buf); |
460 | 2.75k | object->y = bytestream_get_be16(&buf); |
461 | | |
462 | | // If cropping |
463 | 2.75k | if (object->composition_flag & 0x80) { |
464 | 864 | object->crop_x = bytestream_get_be16(&buf); |
465 | 864 | object->crop_y = bytestream_get_be16(&buf); |
466 | 864 | object->crop_w = bytestream_get_be16(&buf); |
467 | 864 | object->crop_h = bytestream_get_be16(&buf); |
468 | 864 | } |
469 | | |
470 | 2.75k | ff_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", |
471 | 2.75k | object->x, object->y); |
472 | | |
473 | 2.75k | if (object->x > avctx->width || object->y > avctx->height) { |
474 | 2.04k | av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n", |
475 | 2.04k | object->x, object->y, |
476 | 2.04k | avctx->width, avctx->height); |
477 | 2.04k | object->y = object->x = 0; |
478 | 2.04k | if (avctx->err_recognition & AV_EF_EXPLODE) { |
479 | 203 | return AVERROR_INVALIDDATA; |
480 | 203 | } |
481 | 2.04k | } |
482 | 2.75k | } |
483 | | |
484 | 2.97k | return 0; |
485 | 5.81k | } |
486 | | |
487 | | /** |
488 | | * Parse the display segment packet. |
489 | | * |
490 | | * The display segment controls the updating of the display. |
491 | | * |
492 | | * @param avctx contains the current codec context |
493 | | * @param data pointer to the data pertaining the subtitle to display |
494 | | * @param buf pointer to the packet to process |
495 | | * @param buf_size size of packet to process |
496 | | */ |
497 | | static int display_end_segment(AVCodecContext *avctx, AVSubtitle *sub, |
498 | | const uint8_t *buf, int buf_size) |
499 | 4.32k | { |
500 | 4.32k | PGSSubContext *ctx = avctx->priv_data; |
501 | 4.32k | int64_t pts; |
502 | 4.32k | PGSSubPalette *palette; |
503 | 4.32k | int i, ret; |
504 | | |
505 | 4.32k | pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts; |
506 | 4.32k | memset(sub, 0, sizeof(*sub)); |
507 | 4.32k | sub->pts = pts; |
508 | 4.32k | ctx->presentation.pts = AV_NOPTS_VALUE; |
509 | 4.32k | sub->start_display_time = 0; |
510 | | // There is no explicit end time for PGS subtitles. The end time |
511 | | // is defined by the start of the next sub which may contain no |
512 | | // objects (i.e. clears the previous sub) |
513 | 4.32k | sub->end_display_time = UINT32_MAX; |
514 | 4.32k | sub->format = 0; |
515 | | |
516 | | // Blank if last object_count was 0. |
517 | 4.32k | if (!ctx->presentation.object_count) |
518 | 954 | return 1; |
519 | 3.36k | sub->rects = av_calloc(ctx->presentation.object_count, sizeof(*sub->rects)); |
520 | 3.36k | if (!sub->rects) { |
521 | 0 | return AVERROR(ENOMEM); |
522 | 0 | } |
523 | 3.36k | palette = find_palette(ctx->presentation.palette_id, &ctx->palettes); |
524 | 3.36k | if (!palette) { |
525 | | // Missing palette. Should only happen with damaged streams. |
526 | 960 | av_log(avctx, AV_LOG_ERROR, "Invalid palette id %d\n", |
527 | 960 | ctx->presentation.palette_id); |
528 | 960 | avsubtitle_free(sub); |
529 | 960 | return AVERROR_INVALIDDATA; |
530 | 960 | } |
531 | 5.25k | for (i = 0; i < ctx->presentation.object_count; i++) { |
532 | 3.49k | AVSubtitleRect *const rect = av_mallocz(sizeof(*rect)); |
533 | 3.49k | PGSSubObject *object; |
534 | | |
535 | 3.49k | if (!rect) |
536 | 0 | return AVERROR(ENOMEM); |
537 | 3.49k | sub->rects[sub->num_rects++] = rect; |
538 | 3.49k | rect->type = SUBTITLE_BITMAP; |
539 | | |
540 | | /* Process bitmap */ |
541 | 3.49k | object = find_object(ctx->presentation.objects[i].id, &ctx->objects); |
542 | 3.49k | if (!object) { |
543 | | // Missing object. Should only happen with damaged streams. |
544 | 1.37k | av_log(avctx, AV_LOG_ERROR, "Invalid object id %d\n", |
545 | 1.37k | ctx->presentation.objects[i].id); |
546 | 1.37k | if (avctx->err_recognition & AV_EF_EXPLODE) |
547 | 214 | return AVERROR_INVALIDDATA; |
548 | | // Leaves rect empty with 0 width and height. |
549 | 1.16k | continue; |
550 | 1.37k | } |
551 | 2.12k | if (ctx->presentation.objects[i].composition_flag & 0x40) |
552 | 1.13k | rect->flags |= AV_SUBTITLE_FLAG_FORCED; |
553 | | |
554 | 2.12k | rect->x = ctx->presentation.objects[i].x; |
555 | 2.12k | rect->y = ctx->presentation.objects[i].y; |
556 | | |
557 | 2.12k | if (object->rle) { |
558 | 1.42k | rect->w = object->w; |
559 | 1.42k | rect->h = object->h; |
560 | | |
561 | 1.42k | rect->linesize[0] = object->w; |
562 | | |
563 | 1.42k | if (object->rle_remaining_len) { |
564 | 660 | av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n", |
565 | 660 | object->rle_data_len, object->rle_remaining_len); |
566 | 660 | if (avctx->err_recognition & AV_EF_EXPLODE) |
567 | 222 | return AVERROR_INVALIDDATA; |
568 | 660 | } |
569 | 1.20k | ret = decode_rle(avctx, rect, object->rle, object->rle_data_len); |
570 | 1.20k | if (ret < 0) { |
571 | 977 | if ((avctx->err_recognition & AV_EF_EXPLODE) || |
572 | 768 | ret == AVERROR(ENOMEM)) { |
573 | 209 | return ret; |
574 | 209 | } |
575 | 768 | rect->w = 0; |
576 | 768 | rect->h = 0; |
577 | 768 | continue; |
578 | 977 | } |
579 | 1.20k | } |
580 | | /* Allocate memory for colors */ |
581 | 922 | rect->nb_colors = 256; |
582 | 922 | rect->data[1] = av_mallocz(AVPALETTE_SIZE); |
583 | 922 | if (!rect->data[1]) |
584 | 0 | return AVERROR(ENOMEM); |
585 | | |
586 | 922 | if (!ctx->forced_subs_only || ctx->presentation.objects[i].composition_flag & 0x40) |
587 | 922 | memcpy(rect->data[1], palette->clut, rect->nb_colors * sizeof(uint32_t)); |
588 | 922 | } |
589 | 1.76k | return 1; |
590 | 2.40k | } |
591 | | |
592 | | static int decode(AVCodecContext *avctx, AVSubtitle *sub, |
593 | | int *got_sub_ptr, const AVPacket *avpkt) |
594 | 226k | { |
595 | 226k | const uint8_t *buf = avpkt->data; |
596 | 226k | int buf_size = avpkt->size; |
597 | | |
598 | 226k | const uint8_t *buf_end; |
599 | 226k | uint8_t segment_type; |
600 | 226k | int segment_length; |
601 | 226k | int i, ret; |
602 | | |
603 | 226k | ff_dlog(avctx, "PGS sub packet:\n"); |
604 | | |
605 | 9.73M | for (i = 0; i < buf_size; i++) { |
606 | 9.51M | ff_dlog(avctx, "%02x ", buf[i]); |
607 | 9.51M | if (i % 16 == 15) |
608 | 572k | ff_dlog(avctx, "\n"); |
609 | 9.51M | } |
610 | | |
611 | 226k | if (i & 15) |
612 | 225k | ff_dlog(avctx, "\n"); |
613 | | |
614 | 226k | *got_sub_ptr = 0; |
615 | | |
616 | | /* Ensure that we have received at a least a segment code and segment length */ |
617 | 226k | if (buf_size < 3) |
618 | 206k | return -1; |
619 | | |
620 | 20.3k | buf_end = buf + buf_size; |
621 | | |
622 | | /* Step through buffer to identify segments */ |
623 | 75.8k | while (buf < buf_end) { |
624 | 72.1k | segment_type = bytestream_get_byte(&buf); |
625 | 72.1k | segment_length = bytestream_get_be16(&buf); |
626 | | |
627 | 72.1k | ff_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type); |
628 | | |
629 | 72.1k | if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf) |
630 | 14.8k | break; |
631 | | |
632 | 57.2k | ret = 0; |
633 | 57.2k | switch (segment_type) { |
634 | 2.17k | case PALETTE_SEGMENT: |
635 | 2.17k | ret = parse_palette_segment(avctx, buf, segment_length); |
636 | 2.17k | break; |
637 | 7.47k | case OBJECT_SEGMENT: |
638 | 7.47k | ret = parse_object_segment(avctx, buf, segment_length); |
639 | 7.47k | break; |
640 | 7.84k | case PRESENTATION_SEGMENT: |
641 | 7.84k | ret = parse_presentation_segment(avctx, buf, segment_length, sub->pts); |
642 | 7.84k | break; |
643 | 420 | case WINDOW_SEGMENT: |
644 | | /* |
645 | | * Window Segment Structure (No new information provided): |
646 | | * 2 bytes: Unknown, |
647 | | * 2 bytes: X position of subtitle, |
648 | | * 2 bytes: Y position of subtitle, |
649 | | * 2 bytes: Width of subtitle, |
650 | | * 2 bytes: Height of subtitle. |
651 | | */ |
652 | 420 | break; |
653 | 4.70k | case DISPLAY_SEGMENT: |
654 | 4.70k | if (*got_sub_ptr) { |
655 | 383 | av_log(avctx, AV_LOG_ERROR, "Duplicate display segment\n"); |
656 | 383 | ret = AVERROR_INVALIDDATA; |
657 | 383 | break; |
658 | 383 | } |
659 | 4.32k | ret = display_end_segment(avctx, sub, buf, segment_length); |
660 | 4.32k | if (ret >= 0) |
661 | 2.71k | *got_sub_ptr = ret; |
662 | 4.32k | break; |
663 | 34.6k | default: |
664 | 34.6k | av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n", |
665 | 34.6k | segment_type, segment_length); |
666 | 34.6k | ret = AVERROR_INVALIDDATA; |
667 | 34.6k | break; |
668 | 57.2k | } |
669 | 57.2k | if (ret < 0 && (ret == AVERROR(ENOMEM) || |
670 | 47.7k | avctx->err_recognition & AV_EF_EXPLODE)) |
671 | 1.73k | return ret; |
672 | | |
673 | 55.5k | buf += segment_length; |
674 | 55.5k | } |
675 | | |
676 | 18.5k | return buf_size; |
677 | 20.3k | } |
678 | | |
679 | | #define OFFSET(x) offsetof(PGSSubContext, x) |
680 | | #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM |
681 | | static const AVOption options[] = { |
682 | | {"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD}, |
683 | | { NULL }, |
684 | | }; |
685 | | |
686 | | static const AVClass pgsdec_class = { |
687 | | .class_name = "PGS subtitle decoder", |
688 | | .item_name = av_default_item_name, |
689 | | .option = options, |
690 | | .version = LIBAVUTIL_VERSION_INT, |
691 | | }; |
692 | | |
693 | | const FFCodec ff_pgssub_decoder = { |
694 | | .p.name = "pgssub", |
695 | | CODEC_LONG_NAME("HDMV Presentation Graphic Stream subtitles"), |
696 | | .p.type = AVMEDIA_TYPE_SUBTITLE, |
697 | | .p.id = AV_CODEC_ID_HDMV_PGS_SUBTITLE, |
698 | | .priv_data_size = sizeof(PGSSubContext), |
699 | | .init = init_decoder, |
700 | | .close = close_decoder, |
701 | | FF_CODEC_DECODE_SUB_CB(decode), |
702 | | .p.priv_class = &pgsdec_class, |
703 | | }; |