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
32
35
36 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
37 #define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch
38 #define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch
39 #define MAX_OBJECT_REFS 2 // Max objects per display set
40
47 };
48
60
68
77
82
87
92
100
102 {
105
106 for (
i = 0;
i <
ctx->objects.count;
i++) {
108 ctx->objects.object[
i].rle_buffer_size = 0;
109 ctx->objects.object[
i].rle_remaining_len = 0;
110 }
111 ctx->objects.count = 0;
112 ctx->palettes.count = 0;
113 }
114
116 {
118
119 for (
i = 0;
i < objects->
count;
i++) {
122 }
124 }
125
127 {
129
130 for (
i = 0;
i < palettes->
count;
i++) {
133 }
135 }
136
138 {
140
141 return 0;
142 }
143
145 {
147
148 return 0;
149 }
150
151 /**
152 * Decode the RLE data.
153 *
154 * The subtitle is stored as a Run Length Encoded image.
155 *
156 * @param avctx contains the current codec context
157 * @param sub pointer to the processed subtitle data
158 * @param buf pointer to the RLE data to process
159 * @param buf_size size of the RLE data to process
160 */
162 const uint8_t *buf, unsigned int buf_size)
163 {
164 const uint8_t *rle_bitmap_end;
165 int pixel_count, line_count;
166
167 rle_bitmap_end = buf + buf_size;
168
170
173
174 pixel_count = 0;
175 line_count = 0;
176
177 while (buf < rle_bitmap_end && line_count < rect->
h) {
180
181 color = bytestream_get_byte(&buf);
183
185 flags = bytestream_get_byte(&buf);
188 run = (
run << 8) + bytestream_get_byte(&buf);
189 color =
flags & 0x80 ? bytestream_get_byte(&buf) : 0;
190 }
191
192 if (
run > 0 && pixel_count + run <= rect->
w *
rect->
h) {
196 /*
197 * New Line. Check if correct pixels decoded, if not display warning
198 * and adjust bitmap pointer to correct new line position.
199 */
200 if (pixel_count %
rect->
w > 0) {
205 }
206 }
207 line_count++;
208 }
209 }
210
211 if (pixel_count < rect->
w *
rect->
h) {
214 }
215
217
218 return 0;
219 }
220
221 /**
222 * Parse the picture segment packet.
223 *
224 * The picture segment contains details on the sequence id,
225 * width, height and Run Length Encoded (RLE) bitmap data.
226 *
227 * @param avctx contains the current codec context
228 * @param buf pointer to the packet to process
229 * @param buf_size size of packet to process
230 */
232 const uint8_t *buf, int buf_size)
233 {
236
237 uint8_t sequence_desc;
240
241 if (buf_size <= 4)
243 buf_size -= 4;
244
245 id = bytestream_get_be16(&buf);
247 if (!object) {
251 }
252 object = &
ctx->objects.object[
ctx->objects.count++];
254 }
255
256 /* skip object version number */
257 buf += 1;
258
259 /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
260 sequence_desc = bytestream_get_byte(&buf);
261
262 if (!(sequence_desc & 0x80)) {
263 /* Additional RLE data */
266
268 object->rle_data_len += buf_size;
269 object->rle_remaining_len -= buf_size;
270
271 return 0;
272 }
273
274 if (buf_size <= 7)
276 buf_size -= 7;
277
278 /* Decode rle bitmap length, stored size includes width/height data */
279 rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
280
281 if (buf_size > rle_bitmap_len) {
283 "Buffer dimension %d larger than the expected RLE data %d\n",
284 buf_size, rle_bitmap_len);
286 }
287
288 /* Get bitmap dimensions from data */
289 width = bytestream_get_be16(&buf);
290 height = bytestream_get_be16(&buf);
291
292 /* Make sure the bitmap is not too large */
296 }
297
300
302
304 object->rle_data_len = 0;
305 object->rle_remaining_len = 0;
307 }
308
309 memcpy(object->
rle, buf, buf_size);
310 object->rle_data_len = buf_size;
311 object->rle_remaining_len = rle_bitmap_len - buf_size;
312
313 return 0;
314 }
315
316 /**
317 * Parse the palette segment packet.
318 *
319 * The palette segment contains details of the palette,
320 * a maximum of 256 colors can be defined.
321 *
322 * @param avctx contains the current codec context
323 * @param buf pointer to the packet to process
324 * @param buf_size size of packet to process
325 */
327 const uint8_t *buf, int buf_size)
328 {
331
332 const uint8_t *buf_end = buf + buf_size;
334 int color_id;
336 int r,
g,
b, r_add, g_add, b_add;
338
339 id = bytestream_get_byte(&buf);
341 if (!palette) {
345 }
346 palette = &
ctx->palettes.palette[
ctx->palettes.count++];
348 }
349
350 /* Skip palette version */
351 buf += 1;
352
353 while (buf < buf_end) {
354 color_id = bytestream_get_byte(&buf);
355 y = bytestream_get_byte(&buf);
356 cr = bytestream_get_byte(&buf);
357 cb = bytestream_get_byte(&buf);
358 alpha = bytestream_get_byte(&buf);
359
360 /* Default to BT.709 colorspace. In case of <= 576 height use BT.601 */
363 } else {
365 }
366
368
369 ff_dlog(avctx,
"Color %d := (%d,%d,%d,%d)\n", color_id,
r,
g,
b,
alpha);
370
371 /* Store color in palette */
373 }
374 return 0;
375 }
376
377 /**
378 * Parse the presentation segment packet.
379 *
380 * The presentation segment contains details on the video
381 * width, video height, x & y subtitle position.
382 *
383 * @param avctx contains the current codec context
384 * @param buf pointer to the packet to process
385 * @param buf_size size of packet to process
386 * @todo TODO: Implement cropping
387 */
389 const uint8_t *buf, int buf_size,
391 {
394 const uint8_t *buf_end = buf + buf_size;
395
396 // Video descriptor
397 int w = bytestream_get_be16(&buf);
398 int h = bytestream_get_be16(&buf);
399
400 ctx->presentation.pts =
pts;
401
402 ff_dlog(avctx,
"Video Dimensions %dx%d\n",
407
408 /* Skip 1 bytes of unknown, frame rate */
409 buf++;
410
411 // Composition descriptor
412 ctx->presentation.id_number = bytestream_get_be16(&buf);
413 /*
414 * state is a 2 bit field that defines pgs epoch boundaries
415 * 00 - Normal, previously defined objects and palettes are still valid
416 * 01 - Acquisition point, previous objects and palettes can be released
417 * 10 - Epoch start, previous objects and palettes can be released
418 * 11 - Epoch continue, previous objects and palettes can be released
419 *
420 * reserved 6 bits discarded
421 */
422 state = bytestream_get_byte(&buf) >> 6;
425 }
426
427 /*
428 * skip palette_update_flag (0x80),
429 */
430 buf += 1;
431 ctx->presentation.palette_id = bytestream_get_byte(&buf);
432 ctx->presentation.object_count = bytestream_get_byte(&buf);
435 "Invalid number of presentation objects %d\n",
436 ctx->presentation.object_count);
437 ctx->presentation.object_count = 2;
440 }
441 }
442
443
444 for (
i = 0;
i <
ctx->presentation.object_count;
i++)
445 {
447
448 if (buf_end - buf < 8) {
450 ctx->presentation.object_count =
i;
452 }
453
454 object->id = bytestream_get_be16(&buf);
455 object->window_id = bytestream_get_byte(&buf);
456 object->composition_flag = bytestream_get_byte(&buf);
457
458 object->x = bytestream_get_be16(&buf);
459 object->y = bytestream_get_be16(&buf);
460
461 // If cropping
463 object->crop_x = bytestream_get_be16(&buf);
464 object->crop_y = bytestream_get_be16(&buf);
465 object->crop_w = bytestream_get_be16(&buf);
466 object->crop_h = bytestream_get_be16(&buf);
467 }
468
469 ff_dlog(avctx,
"Subtitle Placement x=%d, y=%d\n",
470 object->
x, object->
y);
471
473 av_log(avctx,
AV_LOG_ERROR,
"Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
474 object->
x, object->
y,
476 object->y = object->x = 0;
479 }
480 }
481 }
482
483 return 0;
484 }
485
486 /**
487 * Parse the display segment packet.
488 *
489 * The display segment controls the updating of the display.
490 *
491 * @param avctx contains the current codec context
492 * @param data pointer to the data pertaining the subtitle to display
493 * @param buf pointer to the packet to process
494 * @param buf_size size of packet to process
495 */
497 const uint8_t *buf, int buf_size)
498 {
503
505 memset(
sub, 0,
sizeof(*
sub));
508 sub->start_display_time = 0;
509 // There is no explicit end time for PGS subtitles. The end time
510 // is defined by the start of the next sub which may contain no
511 // objects (i.e. clears the previous sub)
512 sub->end_display_time = UINT32_MAX;
514
515 // Blank if last object_count was 0.
516 if (!
ctx->presentation.object_count)
517 return 1;
521 }
523 if (!palette) {
524 // Missing palette. Should only happen with damaged streams.
526 ctx->presentation.palette_id);
529 }
530 for (
i = 0;
i <
ctx->presentation.object_count;
i++) {
533
538
539 /* Process bitmap */
541 if (!object) {
542 // Missing object. Should only happen with damaged streams.
544 ctx->presentation.objects[
i].id);
547 // Leaves rect empty with 0 width and height.
548 continue;
549 }
550 if (
ctx->presentation.objects[
i].composition_flag & 0x40)
552
553 rect->
x =
ctx->presentation.objects[
i].x;
554 rect->
y =
ctx->presentation.objects[
i].y;
555
559
560 rect->linesize[0] =
object->
w;
561
567 }
573 }
576 continue;
577 }
578 }
579 /* Allocate memory for colors */
580 rect->nb_colors = 256;
584
585 if (!
ctx->forced_subs_only ||
ctx->presentation.objects[
i].composition_flag & 0x40)
586 memcpy(
rect->data[1], palette->
clut,
rect->nb_colors *
sizeof(uint32_t));
587 }
588 return 1;
589 }
590
592 int *got_sub_ptr,
const AVPacket *avpkt)
593 {
594 const uint8_t *buf = avpkt->
data;
595 int buf_size = avpkt->
size;
596
597 const uint8_t *buf_end;
598 uint8_t segment_type;
599 int segment_length;
601
602 ff_dlog(avctx,
"PGS sub packet:\n");
603
604 for (
i = 0;
i < buf_size;
i++) {
608 }
609
612
613 *got_sub_ptr = 0;
614
615 /* Ensure that we have received at a least a segment code and segment length */
616 if (buf_size < 3)
617 return -1;
618
619 buf_end = buf + buf_size;
620
621 /* Step through buffer to identify segments */
622 while (buf < buf_end) {
623 segment_type = bytestream_get_byte(&buf);
624 segment_length = bytestream_get_be16(&buf);
625
626 ff_dlog(avctx,
"Segment Length %d, Segment Type %x\n", segment_length, segment_type);
627
629 break;
630
632 switch (segment_type) {
635 break;
638 break;
641 break;
643 /*
644 * Window Segment Structure (No new information provided):
645 * 2 bytes: Unknown,
646 * 2 bytes: X position of subtitle,
647 * 2 bytes: Y position of subtitle,
648 * 2 bytes: Width of subtitle,
649 * 2 bytes: Height of subtitle.
650 */
651 break;
653 if (*got_sub_ptr) {
656 break;
657 }
661 break;
662 default:
664 segment_type, segment_length);
666 break;
667 }
671
672 buf += segment_length;
673 }
674
675 return buf_size;
676 }
677
678 #define OFFSET(x) offsetof(PGSSubContext, x)
679 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
681 {
"forced_subs_only",
"Only show forced subtitles",
OFFSET(forced_subs_only),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
SD},
683 };
684
690 };
691
702 };