pgssubdec.c 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * PGS subtitle decoder
 * Copyright (c) 2009 Stephen Backway
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26 27 28
 * PGS subtitle decoder
 */

#include "avcodec.h"
#include "bytestream.h"
29
#include "internal.h"
30
#include "mathops.h"
31

32
#include "libavutil/colorspace.h"
33
#include "libavutil/imgutils.h"
34
#include "libavutil/opt.h"
35 36

#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
37 38 39
#define MAX_EPOCH_PALETTES 8   // Max 8 allowed per PGS epoch
#define MAX_EPOCH_OBJECTS  64  // Max 64 allowed per PGS epoch
#define MAX_OBJECT_REFS    2   // Max objects per display set
40 41 42

enum SegmentType {
    PALETTE_SEGMENT      = 0x14,
43
    OBJECT_SEGMENT       = 0x15,
44 45 46 47 48
    PRESENTATION_SEGMENT = 0x16,
    WINDOW_SEGMENT       = 0x17,
    DISPLAY_SEGMENT      = 0x80,
};

49 50 51 52 53 54 55 56 57 58 59
typedef struct PGSSubObjectRef {
    int     id;
    int     window_id;
    uint8_t composition_flag;
    int     x;
    int     y;
    int     crop_x;
    int     crop_y;
    int     crop_w;
    int     crop_h;
} PGSSubObjectRef;
60 61

typedef struct PGSSubPresentation {
62
    int id_number;
63 64 65
    int palette_id;
    int object_count;
    PGSSubObjectRef objects[MAX_OBJECT_REFS];
66
    int64_t pts;
67 68
} PGSSubPresentation;

69 70
typedef struct PGSSubObject {
    int          id;
71 72 73 74
    int          w;
    int          h;
    uint8_t      *rle;
    unsigned int rle_buffer_size, rle_data_len;
75
    unsigned int rle_remaining_len;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
} PGSSubObject;

typedef struct PGSSubObjects {
    int          count;
    PGSSubObject object[MAX_EPOCH_OBJECTS];
} PGSSubObjects;

typedef struct PGSSubPalette {
    int         id;
    uint32_t    clut[256];
} PGSSubPalette;

typedef struct PGSSubPalettes {
    int           count;
    PGSSubPalette palette[MAX_EPOCH_PALETTES];
} PGSSubPalettes;
92 93

typedef struct PGSSubContext {
94
    AVClass *class;
95
    PGSSubPresentation presentation;
96 97
    PGSSubPalettes     palettes;
    PGSSubObjects      objects;
98
    int forced_subs_only;
99 100
} PGSSubContext;

101
static void flush_cache(AVCodecContext *avctx)
102 103
{
    PGSSubContext *ctx = avctx->priv_data;
104
    int i;
105

106 107 108 109 110 111 112 113
    for (i = 0; i < ctx->objects.count; i++) {
        av_freep(&ctx->objects.object[i].rle);
        ctx->objects.object[i].rle_buffer_size  = 0;
        ctx->objects.object[i].rle_remaining_len  = 0;
    }
    ctx->objects.count = 0;
    ctx->palettes.count = 0;
}
114

115 116 117 118 119 120 121
static PGSSubObject * find_object(int id, PGSSubObjects *objects)
{
    int i;

    for (i = 0; i < objects->count; i++) {
        if (objects->object[i].id == id)
            return &objects->object[i];
122
    }
123 124 125 126 127 128 129 130 131 132 133 134
    return NULL;
}

static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
{
    int i;

    for (i = 0; i < palettes->count; i++) {
        if (palettes->palette[i].id == id)
            return &palettes->palette[i];
    }
    return NULL;
135 136 137 138 139 140 141 142 143 144 145 146
}

static av_cold int init_decoder(AVCodecContext *avctx)
{
    avctx->pix_fmt     = AV_PIX_FMT_PAL8;

    return 0;
}

static av_cold int close_decoder(AVCodecContext *avctx)
{
    flush_cache(avctx);
147 148 149 150 151

    return 0;
}

/**
152
 * Decode the RLE data.
153
 *
154
 * The subtitle is stored as a Run Length Encoded image.
155 156 157 158 159 160
 *
 * @param avctx contains the current codec context
 * @param sub pointer to the processed subtitle data
 * @param buf pointer to the RLE data to process
 * @param buf_size size of the RLE data to process
 */
161
static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect,
162 163 164 165 166 167 168
                      const uint8_t *buf, unsigned int buf_size)
{
    const uint8_t *rle_bitmap_end;
    int pixel_count, line_count;

    rle_bitmap_end = buf + buf_size;

169
    rect->pict.data[0] = av_malloc(rect->w * rect->h);
170

171
    if (!rect->pict.data[0])
172
        return AVERROR(ENOMEM);
173 174 175 176

    pixel_count = 0;
    line_count  = 0;

177
    while (buf < rle_bitmap_end && line_count < rect->h) {
178 179 180 181 182 183 184 185 186 187 188 189 190 191
        uint8_t flags, color;
        int run;

        color = bytestream_get_byte(&buf);
        run   = 1;

        if (color == 0x00) {
            flags = bytestream_get_byte(&buf);
            run   = flags & 0x3f;
            if (flags & 0x40)
                run = (run << 8) + bytestream_get_byte(&buf);
            color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
        }

192 193
        if (run > 0 && pixel_count + run <= rect->w * rect->h) {
            memset(rect->pict.data[0] + pixel_count, color, run);
194 195 196 197 198 199
            pixel_count += run;
        } else if (!run) {
            /*
             * New Line. Check if correct pixels decoded, if not display warning
             * and adjust bitmap pointer to correct new line position.
             */
200
            if (pixel_count % rect->w > 0) {
201
                av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
202
                       pixel_count % rect->w, rect->w);
203 204 205 206
                if (avctx->err_recognition & AV_EF_EXPLODE) {
                    return AVERROR_INVALIDDATA;
                }
            }
207 208 209 210
            line_count++;
        }
    }

211
    if (pixel_count < rect->w * rect->h) {
212
        av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
213
        return AVERROR_INVALIDDATA;
214 215
    }

216
    av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, rect->w * rect->h);
217 218 219 220 221

    return 0;
}

/**
222
 * Parse the picture segment packet.
223 224 225 226 227 228 229 230
 *
 * The picture segment contains details on the sequence id,
 * width, height and Run Length Encoded (RLE) bitmap data.
 *
 * @param avctx contains the current codec context
 * @param buf pointer to the packet to process
 * @param buf_size size of packet to process
 */
231
static int parse_object_segment(AVCodecContext *avctx,
232 233 234
                                  const uint8_t *buf, int buf_size)
{
    PGSSubContext *ctx = avctx->priv_data;
235
    PGSSubObject *object;
236 237 238

    uint8_t sequence_desc;
    unsigned int rle_bitmap_len, width, height;
239
    int id;
240

241
    if (buf_size <= 4)
242
        return AVERROR_INVALIDDATA;
243 244
    buf_size -= 4;

245 246 247 248 249 250 251 252 253 254
    id = bytestream_get_be16(&buf);
    object = find_object(id, &ctx->objects);
    if (!object) {
        if (ctx->objects.count >= MAX_EPOCH_OBJECTS) {
            av_log(avctx, AV_LOG_ERROR, "Too many objects in epoch\n");
            return AVERROR_INVALIDDATA;
        }
        object = &ctx->objects.object[ctx->objects.count++];
        object->id = id;
    }
255

256 257
    /* skip object version number */
    buf += 1;
258 259 260 261 262

    /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
    sequence_desc = bytestream_get_byte(&buf);

    if (!(sequence_desc & 0x80)) {
263
        /* Additional RLE data */
264
        if (buf_size > object->rle_remaining_len)
265
            return AVERROR_INVALIDDATA;
266

267 268 269
        memcpy(object->rle + object->rle_data_len, buf, buf_size);
        object->rle_data_len += buf_size;
        object->rle_remaining_len -= buf_size;
270

271
        return 0;
272 273
    }

274
    if (buf_size <= 7)
275
        return AVERROR_INVALIDDATA;
276 277 278 279
    buf_size -= 7;

    /* Decode rle bitmap length, stored size includes width/height data */
    rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
280

281 282 283 284 285 286 287
    if (buf_size > rle_bitmap_len) {
        av_log(avctx, AV_LOG_ERROR,
               "Buffer dimension %d larger than the expected RLE data %d\n",
               buf_size, rle_bitmap_len);
        return AVERROR_INVALIDDATA;
    }

288 289 290 291 292
    /* Get bitmap dimensions from data */
    width  = bytestream_get_be16(&buf);
    height = bytestream_get_be16(&buf);

    /* Make sure the bitmap is not too large */
293
    if (avctx->width < width || avctx->height < height) {
294
        av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
295
        return AVERROR_INVALIDDATA;
296 297
    }

298 299
    object->w = width;
    object->h = height;
300

301
    av_fast_padded_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len);
302

303
    if (!object->rle)
304
        return AVERROR(ENOMEM);
305

306 307 308
    memcpy(object->rle, buf, buf_size);
    object->rle_data_len = buf_size;
    object->rle_remaining_len = rle_bitmap_len - buf_size;
309 310 311 312 313

    return 0;
}

/**
314
 * Parse the palette segment packet.
315 316 317 318 319 320 321 322
 *
 * The palette segment contains details of the palette,
 * a maximum of 256 colors can be defined.
 *
 * @param avctx contains the current codec context
 * @param buf pointer to the packet to process
 * @param buf_size size of packet to process
 */
323
static int parse_palette_segment(AVCodecContext *avctx,
324 325 326
                                  const uint8_t *buf, int buf_size)
{
    PGSSubContext *ctx = avctx->priv_data;
327
    PGSSubPalette *palette;
328 329

    const uint8_t *buf_end = buf + buf_size;
330
    const uint8_t *cm      = ff_crop_tab + MAX_NEG_CROP;
331 332 333
    int color_id;
    int y, cb, cr, alpha;
    int r, g, b, r_add, g_add, b_add;
334 335 336 337 338 339 340 341 342 343 344 345
    int id;

    id  = bytestream_get_byte(&buf);
    palette = find_palette(id, &ctx->palettes);
    if (!palette) {
        if (ctx->palettes.count >= MAX_EPOCH_PALETTES) {
            av_log(avctx, AV_LOG_ERROR, "Too many palettes in epoch\n");
            return AVERROR_INVALIDDATA;
        }
        palette = &ctx->palettes.palette[ctx->palettes.count++];
        palette->id  = id;
    }
346

347 348
    /* Skip palette version */
    buf += 1;
349 350 351 352 353

    while (buf < buf_end) {
        color_id  = bytestream_get_byte(&buf);
        y         = bytestream_get_byte(&buf);
        cr        = bytestream_get_byte(&buf);
354
        cb        = bytestream_get_byte(&buf);
355 356 357 358 359
        alpha     = bytestream_get_byte(&buf);

        YUV_TO_RGB1(cb, cr);
        YUV_TO_RGB2(r, g, b, y);

360
        av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
361 362

        /* Store color in palette */
363
        palette->clut[color_id] = RGBA(r,g,b,alpha);
364
    }
365
    return 0;
366 367 368
}

/**
369
 * Parse the presentation segment packet.
370 371 372 373 374 375 376 377 378
 *
 * The presentation segment contains details on the video
 * width, video height, x & y subtitle position.
 *
 * @param avctx contains the current codec context
 * @param buf pointer to the packet to process
 * @param buf_size size of packet to process
 * @todo TODO: Implement cropping
 */
379 380 381
static int parse_presentation_segment(AVCodecContext *avctx,
                                      const uint8_t *buf, int buf_size,
                                      int64_t pts)
382 383
{
    PGSSubContext *ctx = avctx->priv_data;
384
    int i, state, ret;
385
    const uint8_t *buf_end = buf + buf_size;
386

387
    // Video descriptor
388 389
    int w = bytestream_get_be16(&buf);
    int h = bytestream_get_be16(&buf);
390

391 392
    ctx->presentation.pts = pts;

393
    av_dlog(avctx, "Video Dimensions %dx%d\n",
394
            w, h);
395 396 397
    ret = ff_set_dimensions(avctx, w, h);
    if (ret < 0)
        return ret;
398

399
    /* Skip 1 bytes of unknown, frame rate */
400 401
    buf++;

402
    // Composition descriptor
403
    ctx->presentation.id_number = bytestream_get_be16(&buf);
Reimar Döffinger's avatar
Reimar Döffinger committed
404
    /*
405 406 407 408 409 410 411
     * state is a 2 bit field that defines pgs epoch boundaries
     * 00 - Normal, previously defined objects and palettes are still valid
     * 01 - Acquisition point, previous objects and palettes can be released
     * 10 - Epoch start, previous objects and palettes can be released
     * 11 - Epoch continue, previous objects and palettes can be released
     *
     * reserved 6 bits discarded
Reimar Döffinger's avatar
Reimar Döffinger committed
412
     */
413 414 415 416
    state = bytestream_get_byte(&buf) >> 6;
    if (state != 0) {
        flush_cache(avctx);
    }
Reimar Döffinger's avatar
Reimar Döffinger committed
417 418

    /*
419
     * skip palette_update_flag (0x80),
Reimar Döffinger's avatar
Reimar Döffinger committed
420
     */
421 422
    buf += 1;
    ctx->presentation.palette_id = bytestream_get_byte(&buf);
423
    ctx->presentation.object_count = bytestream_get_byte(&buf);
424 425 426 427 428 429 430 431
    if (ctx->presentation.object_count > MAX_OBJECT_REFS) {
        av_log(avctx, AV_LOG_ERROR,
               "Invalid number of presentation objects %d\n",
               ctx->presentation.object_count);
        ctx->presentation.object_count = 2;
        if (avctx->err_recognition & AV_EF_EXPLODE) {
            return AVERROR_INVALIDDATA;
        }
432
    }
Reimar Döffinger's avatar
Reimar Döffinger committed
433 434


435 436
    for (i = 0; i < ctx->presentation.object_count; i++)
    {
437 438 439 440 441 442 443

        if (buf_end - buf < 8) {
            av_log(avctx, AV_LOG_ERROR, "Insufficent space for object\n");
            ctx->presentation.object_count = i;
            return AVERROR_INVALIDDATA;
        }

444 445 446
        ctx->presentation.objects[i].id = bytestream_get_be16(&buf);
        ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf);
        ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf);
Reimar Döffinger's avatar
Reimar Döffinger committed
447

448 449
        ctx->presentation.objects[i].x = bytestream_get_be16(&buf);
        ctx->presentation.objects[i].y = bytestream_get_be16(&buf);
Reimar Döffinger's avatar
Reimar Döffinger committed
450

451 452 453 454 455 456 457
        // If cropping
        if (ctx->presentation.objects[i].composition_flag & 0x80) {
            ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf);
            ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf);
            ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf);
            ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf);
        }
458

459 460
        av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",
                ctx->presentation.objects[i].x, ctx->presentation.objects[i].y);
461

462 463
        if (ctx->presentation.objects[i].x > avctx->width ||
            ctx->presentation.objects[i].y > avctx->height) {
464
            av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
465 466 467 468 469 470 471 472
                   ctx->presentation.objects[i].x,
                   ctx->presentation.objects[i].y,
                    avctx->width, avctx->height);
            ctx->presentation.objects[i].x = 0;
            ctx->presentation.objects[i].y = 0;
            if (avctx->err_recognition & AV_EF_EXPLODE) {
                return AVERROR_INVALIDDATA;
            }
473 474
        }
    }
475 476

    return 0;
477 478 479
}

/**
480
 * Parse the display segment packet.
481 482 483 484 485 486 487 488 489 490 491 492 493
 *
 * The display segment controls the updating of the display.
 *
 * @param avctx contains the current codec context
 * @param data pointer to the data pertaining the subtitle to display
 * @param buf pointer to the packet to process
 * @param buf_size size of packet to process
 */
static int display_end_segment(AVCodecContext *avctx, void *data,
                               const uint8_t *buf, int buf_size)
{
    AVSubtitle    *sub = data;
    PGSSubContext *ctx = avctx->priv_data;
494
    int64_t pts;
495 496
    PGSSubPalette *palette;
    int i, ret;
497

498
    pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts;
499
    memset(sub, 0, sizeof(*sub));
500
    sub->pts = pts;
501
    ctx->presentation.pts = AV_NOPTS_VALUE;
502
    sub->start_display_time = 0;
503 504 505 506
    // There is no explicit end time for PGS subtitles.  The end time
    // is defined by the start of the next sub which may contain no
    // objects (i.e. clears the previous sub)
    sub->end_display_time   = UINT32_MAX;
507 508
    sub->format             = 0;

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    // Blank if last object_count was 0.
    if (!ctx->presentation.object_count)
        return 1;
    sub->rects = av_mallocz(sizeof(*sub->rects) * ctx->presentation.object_count);
    if (!sub->rects) {
        return AVERROR(ENOMEM);
    }
    palette = find_palette(ctx->presentation.palette_id, &ctx->palettes);
    if (!palette) {
        // Missing palette.  Should only happen with damaged streams.
        av_log(avctx, AV_LOG_ERROR, "Invalid palette id %d\n",
               ctx->presentation.palette_id);
        avsubtitle_free(sub);
        return AVERROR_INVALIDDATA;
    }
    for (i = 0; i < ctx->presentation.object_count; i++) {
        PGSSubObject *object;
526

527 528 529 530 531 532 533
        sub->rects[i]  = av_mallocz(sizeof(*sub->rects[0]));
        if (!sub->rects[i]) {
            avsubtitle_free(sub);
            return AVERROR(ENOMEM);
        }
        sub->num_rects++;
        sub->rects[i]->type = SUBTITLE_BITMAP;
534 535

        /* Process bitmap */
536 537 538 539 540 541 542 543 544 545 546
        object = find_object(ctx->presentation.objects[i].id, &ctx->objects);
        if (!object) {
            // Missing object.  Should only happen with damaged streams.
            av_log(avctx, AV_LOG_ERROR, "Invalid object id %d\n",
                   ctx->presentation.objects[i].id);
            if (avctx->err_recognition & AV_EF_EXPLODE) {
                avsubtitle_free(sub);
                return AVERROR_INVALIDDATA;
            }
            // Leaves rect empty with 0 width and height.
            continue;
547
        }
548 549
        if (ctx->presentation.objects[i].composition_flag & 0x40)
            sub->rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED;
550

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
        sub->rects[i]->x    = ctx->presentation.objects[i].x;
        sub->rects[i]->y    = ctx->presentation.objects[i].y;
        sub->rects[i]->w    = object->w;
        sub->rects[i]->h    = object->h;

        sub->rects[i]->pict.linesize[0] = object->w;

        if (object->rle) {
            if (object->rle_remaining_len) {
                av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
                       object->rle_data_len, object->rle_remaining_len);
                if (avctx->err_recognition & AV_EF_EXPLODE) {
                    avsubtitle_free(sub);
                    return AVERROR_INVALIDDATA;
                }
            }
            ret = decode_rle(avctx, sub->rects[i], object->rle, object->rle_data_len);
            if (ret < 0) {
                if ((avctx->err_recognition & AV_EF_EXPLODE) ||
                    ret == AVERROR(ENOMEM)) {
                    avsubtitle_free(sub);
                    return ret;
                }
                sub->rects[i]->w = 0;
                sub->rects[i]->h = 0;
                continue;
            }
        }
579
        /* Allocate memory for colors */
580 581 582 583 584 585
        sub->rects[i]->nb_colors    = 256;
        sub->rects[i]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
        if (!sub->rects[i]->pict.data[1]) {
            avsubtitle_free(sub);
            return AVERROR(ENOMEM);
        }
586

587
        if (!ctx->forced_subs_only || ctx->presentation.objects[i].composition_flag & 0x40)
588
        memcpy(sub->rects[i]->pict.data[1], palette->clut, sub->rects[i]->nb_colors * sizeof(uint32_t));
589

590
    }
591 592 593 594 595 596 597 598 599 600 601 602
    return 1;
}

static int decode(AVCodecContext *avctx, void *data, int *data_size,
                  AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size       = avpkt->size;

    const uint8_t *buf_end;
    uint8_t       segment_type;
    int           segment_length;
603
    int i, ret;
604

605
    av_dlog(avctx, "PGS sub packet:\n");
606 607

    for (i = 0; i < buf_size; i++) {
608
        av_dlog(avctx, "%02x ", buf[i]);
609
        if (i % 16 == 15)
610
            av_dlog(avctx, "\n");
611 612 613
    }

    if (i & 15)
614
        av_dlog(avctx, "\n");
615 616 617 618 619 620 621 622 623 624 625 626 627 628

    *data_size = 0;

    /* Ensure that we have received at a least a segment code and segment length */
    if (buf_size < 3)
        return -1;

    buf_end = buf + buf_size;

    /* Step through buffer to identify segments */
    while (buf < buf_end) {
        segment_type   = bytestream_get_byte(&buf);
        segment_length = bytestream_get_be16(&buf);

629
        av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
630 631 632 633

        if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
            break;

634
        ret = 0;
635 636
        switch (segment_type) {
        case PALETTE_SEGMENT:
637
            ret = parse_palette_segment(avctx, buf, segment_length);
638
            break;
639
        case OBJECT_SEGMENT:
640
            ret = parse_object_segment(avctx, buf, segment_length);
641 642
            break;
        case PRESENTATION_SEGMENT:
643
            ret = parse_presentation_segment(avctx, buf, segment_length, ((AVSubtitle*)(data))->pts);
644 645 646 647
            break;
        case WINDOW_SEGMENT:
            /*
             * Window Segment Structure (No new information provided):
648
             *     2 bytes: Unknown,
649 650 651 652 653 654 655
             *     2 bytes: X position of subtitle,
             *     2 bytes: Y position of subtitle,
             *     2 bytes: Width of subtitle,
             *     2 bytes: Height of subtitle.
             */
            break;
        case DISPLAY_SEGMENT:
656 657 658
            ret = display_end_segment(avctx, data, buf, segment_length);
            if (ret >= 0)
                *data_size = ret;
659 660 661 662
            break;
        default:
            av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
                   segment_type, segment_length);
663
            ret = AVERROR_INVALIDDATA;
664 665
            break;
        }
666 667
        if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
            return ret;
668 669 670 671 672 673 674

        buf += segment_length;
    }

    return buf_size;
}

675 676 677
#define OFFSET(x) offsetof(PGSSubContext, x)
#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
static const AVOption options[] = {
678
    {"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
679 680 681 682 683 684 685 686 687 688
    { NULL },
};

static const AVClass pgsdec_class = {
    .class_name = "PGS subtitle decoder",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

689
AVCodec ff_pgssub_decoder = {
690
    .name           = "pgssub",
691
    .long_name      = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
692
    .type           = AVMEDIA_TYPE_SUBTITLE,
693
    .id             = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
694 695 696 697
    .priv_data_size = sizeof(PGSSubContext),
    .init           = init_decoder,
    .close          = close_decoder,
    .decode         = decode,
698
    .priv_class     = &pgsdec_class,
699
};