3 * Copyright (c) 2009 Stephen Backway
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * PGS subtitle decoder
28 #include "bytestream.h"
32 #include "libavutil/colorspace.h"
33 #include "libavutil/imgutils.h"
35 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
36 #define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch
37 #define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch
38 #define MAX_OBJECT_REFS 2 // Max objects per display set
41 PALETTE_SEGMENT = 0x14,
42 OBJECT_SEGMENT = 0x15,
43 PRESENTATION_SEGMENT = 0x16,
44 WINDOW_SEGMENT = 0x17,
45 DISPLAY_SEGMENT = 0x80,
48 typedef struct PGSSubObjectRef {
51 uint8_t composition_flag;
60 typedef struct PGSSubPresentation {
64 PGSSubObjectRef objects[MAX_OBJECT_REFS];
68 typedef struct PGSSubObject {
73 unsigned int rle_buffer_size, rle_data_len;
74 unsigned int rle_remaining_len;
77 typedef struct PGSSubObjects {
79 PGSSubObject object[MAX_EPOCH_OBJECTS];
82 typedef struct PGSSubPalette {
87 typedef struct PGSSubPalettes {
89 PGSSubPalette palette[MAX_EPOCH_PALETTES];
92 typedef struct PGSSubContext {
93 PGSSubPresentation presentation;
94 PGSSubPalettes palettes;
95 PGSSubObjects objects;
98 static void flush_cache(AVCodecContext *avctx)
100 PGSSubContext *ctx = avctx->priv_data;
103 for (i = 0; i < ctx->objects.count; i++) {
104 av_freep(&ctx->objects.object[i].rle);
105 ctx->objects.object[i].rle_buffer_size = 0;
106 ctx->objects.object[i].rle_remaining_len = 0;
108 ctx->objects.count = 0;
109 ctx->palettes.count = 0;
112 static PGSSubObject * find_object(int id, PGSSubObjects *objects)
116 for (i = 0; i < objects->count; i++) {
117 if (objects->object[i].id == id)
118 return &objects->object[i];
123 static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
127 for (i = 0; i < palettes->count; i++) {
128 if (palettes->palette[i].id == id)
129 return &palettes->palette[i];
134 static av_cold int init_decoder(AVCodecContext *avctx)
136 avctx->pix_fmt = AV_PIX_FMT_PAL8;
141 static av_cold int close_decoder(AVCodecContext *avctx)
149 * Decode the RLE data.
151 * The subtitle is stored as an Run Length Encoded image.
153 * @param avctx contains the current codec context
154 * @param sub pointer to the processed subtitle data
155 * @param buf pointer to the RLE data to process
156 * @param buf_size size of the RLE data to process
158 static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect,
159 const uint8_t *buf, unsigned int buf_size)
161 const uint8_t *rle_bitmap_end;
162 int pixel_count, line_count;
164 rle_bitmap_end = buf + buf_size;
166 rect->data[0] = av_malloc(rect->w * rect->h);
169 return AVERROR(ENOMEM);
174 while (buf < rle_bitmap_end && line_count < rect->h) {
175 uint8_t flags, color;
178 color = bytestream_get_byte(&buf);
182 flags = bytestream_get_byte(&buf);
185 run = (run << 8) + bytestream_get_byte(&buf);
186 color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
189 if (run > 0 && pixel_count + run <= rect->w * rect->h) {
190 memset(rect->data[0] + pixel_count, color, run);
194 * New Line. Check if correct pixels decoded, if not display warning
195 * and adjust bitmap pointer to correct new line position.
197 if (pixel_count % rect->w > 0) {
198 av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
199 pixel_count % rect->w, rect->w);
200 if (avctx->err_recognition & AV_EF_EXPLODE) {
201 return AVERROR_INVALIDDATA;
208 if (pixel_count < rect->w * rect->h) {
209 av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
210 return AVERROR_INVALIDDATA;
213 ff_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, rect->w * rect->h);
219 * Parse the picture segment packet.
221 * The picture segment contains details on the sequence id,
222 * width, height and Run Length Encoded (RLE) bitmap data.
224 * @param avctx contains the current codec context
225 * @param buf pointer to the packet to process
226 * @param buf_size size of packet to process
228 static int parse_object_segment(AVCodecContext *avctx,
229 const uint8_t *buf, int buf_size)
231 PGSSubContext *ctx = avctx->priv_data;
232 PGSSubObject *object;
234 uint8_t sequence_desc;
235 unsigned int rle_bitmap_len, width, height;
239 return AVERROR_INVALIDDATA;
242 id = bytestream_get_be16(&buf);
243 object = find_object(id, &ctx->objects);
245 if (ctx->objects.count >= MAX_EPOCH_OBJECTS) {
246 av_log(avctx, AV_LOG_ERROR, "Too many objects in epoch\n");
247 return AVERROR_INVALIDDATA;
249 object = &ctx->objects.object[ctx->objects.count++];
253 /* skip object version number */
256 /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
257 sequence_desc = bytestream_get_byte(&buf);
259 if (!(sequence_desc & 0x80)) {
260 /* Additional RLE data */
261 if (buf_size > object->rle_remaining_len)
262 return AVERROR_INVALIDDATA;
264 memcpy(object->rle + object->rle_data_len, buf, buf_size);
265 object->rle_data_len += buf_size;
266 object->rle_remaining_len -= buf_size;
272 return AVERROR_INVALIDDATA;
275 /* Decode rle bitmap length, stored size includes width/height data */
276 rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
278 if (buf_size > rle_bitmap_len) {
279 av_log(avctx, AV_LOG_ERROR,
280 "Buffer dimension %d larger than the expected RLE data %d\n",
281 buf_size, rle_bitmap_len);
282 return AVERROR_INVALIDDATA;
285 /* Get bitmap dimensions from data */
286 width = bytestream_get_be16(&buf);
287 height = bytestream_get_be16(&buf);
289 /* Make sure the bitmap is not too large */
290 if (avctx->width < width || avctx->height < height) {
291 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
292 return AVERROR_INVALIDDATA;
298 av_fast_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len);
301 return AVERROR(ENOMEM);
303 memcpy(object->rle, buf, buf_size);
304 object->rle_data_len = buf_size;
305 object->rle_remaining_len = rle_bitmap_len - buf_size;
311 * Parse the palette segment packet.
313 * The palette segment contains details of the palette,
314 * a maximum of 256 colors can be defined.
316 * @param avctx contains the current codec context
317 * @param buf pointer to the packet to process
318 * @param buf_size size of packet to process
320 static int parse_palette_segment(AVCodecContext *avctx,
321 const uint8_t *buf, int buf_size)
323 PGSSubContext *ctx = avctx->priv_data;
324 PGSSubPalette *palette;
326 const uint8_t *buf_end = buf + buf_size;
327 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
329 int y, cb, cr, alpha;
330 int r, g, b, r_add, g_add, b_add;
333 id = bytestream_get_byte(&buf);
334 palette = find_palette(id, &ctx->palettes);
336 if (ctx->palettes.count >= MAX_EPOCH_PALETTES) {
337 av_log(avctx, AV_LOG_ERROR, "Too many palettes in epoch\n");
338 return AVERROR_INVALIDDATA;
340 palette = &ctx->palettes.palette[ctx->palettes.count++];
344 /* Skip palette version */
347 while (buf < buf_end) {
348 color_id = bytestream_get_byte(&buf);
349 y = bytestream_get_byte(&buf);
350 cr = bytestream_get_byte(&buf);
351 cb = bytestream_get_byte(&buf);
352 alpha = bytestream_get_byte(&buf);
354 /* Default to BT.709 colorspace. In case of <= 576 height use BT.601 */
355 if (avctx->height <= 0 || avctx->height > 576) {
356 YUV_TO_RGB1_CCIR_BT709(cb, cr);
358 YUV_TO_RGB1_CCIR(cb, cr);
361 YUV_TO_RGB2_CCIR(r, g, b, y);
363 ff_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
365 /* Store color in palette */
366 palette->clut[color_id] = RGBA(r,g,b,alpha);
372 * Parse the presentation segment packet.
374 * The presentation segment contains details on the video
375 * width, video height, x & y subtitle position.
377 * @param avctx contains the current codec context
378 * @param buf pointer to the packet to process
379 * @param buf_size size of packet to process
380 * @todo TODO: Implement cropping
382 static int parse_presentation_segment(AVCodecContext *avctx,
383 const uint8_t *buf, int buf_size,
386 PGSSubContext *ctx = avctx->priv_data;
391 int w = bytestream_get_be16(&buf);
392 int h = bytestream_get_be16(&buf);
394 ctx->presentation.pts = pts;
396 ff_dlog(avctx, "Video Dimensions %dx%d\n",
398 ret = ff_set_dimensions(avctx, w, h);
402 /* Skip 1 bytes of unknown, frame rate */
405 // Composition descriptor
406 ctx->presentation.id_number = bytestream_get_be16(&buf);
408 * state is a 2 bit field that defines pgs epoch boundaries
409 * 00 - Normal, previously defined objects and palettes are still valid
410 * 01 - Acquisition point, previous objects and palettes can be released
411 * 10 - Epoch start, previous objects and palettes can be released
412 * 11 - Epoch continue, previous objects and palettes can be released
414 * reserved 6 bits discarded
416 state = bytestream_get_byte(&buf) >> 6;
422 * skip palette_update_flag (0x80),
425 ctx->presentation.palette_id = bytestream_get_byte(&buf);
426 ctx->presentation.object_count = bytestream_get_byte(&buf);
427 if (ctx->presentation.object_count > MAX_OBJECT_REFS) {
428 av_log(avctx, AV_LOG_ERROR,
429 "Invalid number of presentation objects %d\n",
430 ctx->presentation.object_count);
431 ctx->presentation.object_count = 2;
432 if (avctx->err_recognition & AV_EF_EXPLODE) {
433 return AVERROR_INVALIDDATA;
437 for (i = 0; i < ctx->presentation.object_count; i++)
439 ctx->presentation.objects[i].id = bytestream_get_be16(&buf);
440 ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf);
441 ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf);
443 ctx->presentation.objects[i].x = bytestream_get_be16(&buf);
444 ctx->presentation.objects[i].y = bytestream_get_be16(&buf);
447 if (ctx->presentation.objects[i].composition_flag & 0x80) {
448 ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf);
449 ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf);
450 ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf);
451 ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf);
454 ff_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",
455 ctx->presentation.objects[i].x, ctx->presentation.objects[i].y);
457 if (ctx->presentation.objects[i].x > avctx->width ||
458 ctx->presentation.objects[i].y > avctx->height) {
459 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
460 ctx->presentation.objects[i].x,
461 ctx->presentation.objects[i].y,
462 avctx->width, avctx->height);
463 ctx->presentation.objects[i].x = 0;
464 ctx->presentation.objects[i].y = 0;
465 if (avctx->err_recognition & AV_EF_EXPLODE) {
466 return AVERROR_INVALIDDATA;
475 * Parse the display segment packet.
477 * The display segment controls the updating of the display.
479 * @param avctx contains the current codec context
480 * @param data pointer to the data pertaining the subtitle to display
481 * @param buf pointer to the packet to process
482 * @param buf_size size of packet to process
484 static int display_end_segment(AVCodecContext *avctx, void *data,
485 const uint8_t *buf, int buf_size)
487 AVSubtitle *sub = data;
488 PGSSubContext *ctx = avctx->priv_data;
489 PGSSubPalette *palette;
492 memset(sub, 0, sizeof(*sub));
493 sub->pts = ctx->presentation.pts;
494 sub->start_display_time = 0;
495 // There is no explicit end time for PGS subtitles. The end time
496 // is defined by the start of the next sub which may contain no
497 // objects (i.e. clears the previous sub)
498 sub->end_display_time = UINT32_MAX;
501 // Blank if last object_count was 0.
502 if (!ctx->presentation.object_count)
504 sub->rects = av_mallocz(sizeof(*sub->rects) * ctx->presentation.object_count);
506 return AVERROR(ENOMEM);
508 palette = find_palette(ctx->presentation.palette_id, &ctx->palettes);
510 // Missing palette. Should only happen with damaged streams.
511 av_log(avctx, AV_LOG_ERROR, "Invalid palette id %d\n",
512 ctx->presentation.palette_id);
513 avsubtitle_free(sub);
514 return AVERROR_INVALIDDATA;
516 for (i = 0; i < ctx->presentation.object_count; i++) {
517 PGSSubObject *object;
519 sub->rects[i] = av_mallocz(sizeof(*sub->rects[0]));
520 if (!sub->rects[i]) {
521 avsubtitle_free(sub);
522 return AVERROR(ENOMEM);
525 sub->rects[i]->type = SUBTITLE_BITMAP;
528 object = find_object(ctx->presentation.objects[i].id, &ctx->objects);
530 // Missing object. Should only happen with damaged streams.
531 av_log(avctx, AV_LOG_ERROR, "Invalid object id %d\n",
532 ctx->presentation.objects[i].id);
533 if (avctx->err_recognition & AV_EF_EXPLODE) {
534 avsubtitle_free(sub);
535 return AVERROR_INVALIDDATA;
537 // Leaves rect empty with 0 width and height.
540 if (ctx->presentation.objects[i].composition_flag & 0x40)
541 sub->rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED;
543 sub->rects[i]->x = ctx->presentation.objects[i].x;
544 sub->rects[i]->y = ctx->presentation.objects[i].y;
545 sub->rects[i]->w = object->w;
546 sub->rects[i]->h = object->h;
548 sub->rects[i]->linesize[0] = object->w;
551 if (object->rle_remaining_len) {
552 av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
553 object->rle_data_len, object->rle_remaining_len);
554 if (avctx->err_recognition & AV_EF_EXPLODE) {
555 avsubtitle_free(sub);
556 return AVERROR_INVALIDDATA;
559 ret = decode_rle(avctx, sub->rects[i], object->rle, object->rle_data_len);
561 if ((avctx->err_recognition & AV_EF_EXPLODE) ||
562 ret == AVERROR(ENOMEM)) {
563 avsubtitle_free(sub);
566 sub->rects[i]->w = 0;
567 sub->rects[i]->h = 0;
571 /* Allocate memory for colors */
572 sub->rects[i]->nb_colors = 256;
573 sub->rects[i]->data[1] = av_mallocz(AVPALETTE_SIZE);
574 if (!sub->rects[i]->data[1]) {
575 avsubtitle_free(sub);
576 return AVERROR(ENOMEM);
580 FF_DISABLE_DEPRECATION_WARNINGS
582 AVSubtitleRect *rect;
584 rect = sub->rects[i];
585 for (j = 0; j < 4; j++) {
586 rect->pict.data[j] = rect->data[j];
587 rect->pict.linesize[j] = rect->linesize[j];
590 FF_ENABLE_DEPRECATION_WARNINGS
593 memcpy(sub->rects[i]->data[1], palette->clut, sub->rects[i]->nb_colors * sizeof(uint32_t));
599 static int decode(AVCodecContext *avctx, void *data, int *data_size,
602 const uint8_t *buf = avpkt->data;
603 int buf_size = avpkt->size;
605 const uint8_t *buf_end;
606 uint8_t segment_type;
610 ff_dlog(avctx, "PGS sub packet:\n");
612 for (i = 0; i < buf_size; i++) {
613 ff_dlog(avctx, "%02x ", buf[i]);
615 ff_dlog(avctx, "\n");
619 ff_dlog(avctx, "\n");
623 /* Ensure that we have received at a least a segment code and segment length */
627 buf_end = buf + buf_size;
629 /* Step through buffer to identify segments */
630 while (buf < buf_end) {
631 segment_type = bytestream_get_byte(&buf);
632 segment_length = bytestream_get_be16(&buf);
634 ff_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
636 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
640 switch (segment_type) {
641 case PALETTE_SEGMENT:
642 ret = parse_palette_segment(avctx, buf, segment_length);
645 ret = parse_object_segment(avctx, buf, segment_length);
647 case PRESENTATION_SEGMENT:
648 ret = parse_presentation_segment(avctx, buf, segment_length, avpkt->pts);
652 * Window Segment Structure (No new information provided):
654 * 2 bytes: X position of subtitle,
655 * 2 bytes: Y position of subtitle,
656 * 2 bytes: Width of subtitle,
657 * 2 bytes: Height of subtitle.
660 case DISPLAY_SEGMENT:
661 ret = display_end_segment(avctx, data, buf, segment_length);
666 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
667 segment_type, segment_length);
668 ret = AVERROR_INVALIDDATA;
671 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
674 buf += segment_length;
680 AVCodec ff_pgssub_decoder = {
682 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
683 .type = AVMEDIA_TYPE_SUBTITLE,
684 .id = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
685 .priv_data_size = sizeof(PGSSubContext),
686 .init = init_decoder,
687 .close = close_decoder,