Browse code

Add Bluray Subtitle Support

Patch by Stephen Backway, stev391 A exemail D com D au

Originally committed as revision 19699 to svn://svn.ffmpeg.org/ffmpeg/trunk

Stephen Backway authored on 2009/08/25 17:47:39
Showing 7 changed files
... ...
@@ -34,6 +34,7 @@ version <next>:
34 34
 - Wave64 demuxer
35 35
 - IEC-61937 compatible Muxer
36 36
 - TwinVQ decoder
37
+- Bluray (PGS) subtitle decoder
37 38
 
38 39
 
39 40
 
... ...
@@ -636,6 +636,7 @@ performance on systems without hardware floating point support).
636 636
 @item SSA/ASS      @tab X @tab X
637 637
 @item DVB          @tab X @tab X @tab X @tab X
638 638
 @item DVD          @tab X @tab X @tab X @tab X
639
+@item PGS          @tab   @tab   @tab   @tab X
639 640
 @item XSUB         @tab   @tab   @tab X @tab X
640 641
 @end multitable
641 642
 
... ...
@@ -169,6 +169,7 @@ OBJS-$(CONFIG_PGM_DECODER)             += pnmenc.o pnm.o
169 169
 OBJS-$(CONFIG_PGM_ENCODER)             += pnmenc.o
170 170
 OBJS-$(CONFIG_PGMYUV_DECODER)          += pnmenc.o pnm.o
171 171
 OBJS-$(CONFIG_PGMYUV_ENCODER)          += pnmenc.o
172
+OBJS-$(CONFIG_PGSSUB_DECODER)          += pgssubdec.o
172 173
 OBJS-$(CONFIG_PNG_DECODER)             += png.o pngdec.o
173 174
 OBJS-$(CONFIG_PNG_ENCODER)             += png.o pngenc.o
174 175
 OBJS-$(CONFIG_PPM_DECODER)             += pnmenc.o pnm.o
... ...
@@ -301,6 +301,7 @@ void avcodec_register_all(void)
301 301
     /* subtitles */
302 302
     REGISTER_ENCDEC  (DVBSUB, dvbsub);
303 303
     REGISTER_ENCDEC  (DVDSUB, dvdsub);
304
+    REGISTER_DECODER (PGSSUB, pgssub);
304 305
     REGISTER_ENCDEC  (XSUB, xsub);
305 306
 
306 307
     /* external libraries */
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVCODEC_VERSION_MAJOR 52
33
-#define LIBAVCODEC_VERSION_MINOR 32
33
+#define LIBAVCODEC_VERSION_MINOR 33
34 34
 #define LIBAVCODEC_VERSION_MICRO  0
35 35
 
36 36
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -327,6 +327,7 @@ enum CodecID {
327 327
     CODEC_ID_XSUB,
328 328
     CODEC_ID_SSA,
329 329
     CODEC_ID_MOV_TEXT,
330
+    CODEC_ID_HDMV_PGS_SUBTITLE,
330 331
 
331 332
     /* other specific kind of codecs (generally used for attachments) */
332 333
     CODEC_ID_TTF= 0x18000,
333 334
new file mode 100644
... ...
@@ -0,0 +1,466 @@
0
+/*
1
+ * PGS subtitle decoder
2
+ * Copyright (c) 2009 Stephen Backway
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file libavcodec/pgssubdec.c
23
+ * PGS subtitle decoder
24
+ */
25
+
26
+#include "avcodec.h"
27
+#include "dsputil.h"
28
+#include "colorspace.h"
29
+#include "bytestream.h"
30
+
31
+//#define DEBUG_PACKET_CONTENTS
32
+
33
+#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
34
+
35
+enum SegmentType {
36
+    PALETTE_SEGMENT      = 0x14,
37
+    PICTURE_SEGMENT      = 0x15,
38
+    PRESENTATION_SEGMENT = 0x16,
39
+    WINDOW_SEGMENT       = 0x17,
40
+    DISPLAY_SEGMENT      = 0x80,
41
+};
42
+
43
+typedef struct PGSSubPresentation {
44
+    int x;
45
+    int y;
46
+    int video_w;
47
+    int video_h;
48
+    int id_number;
49
+} PGSSubPresentation;
50
+
51
+typedef struct PGSSubPicture {
52
+    int          w;
53
+    int          h;
54
+    uint8_t      *rle;
55
+    unsigned int rle_buffer_size, rle_data_len;
56
+} PGSSubPicture;
57
+
58
+typedef struct PGSSubContext {
59
+    PGSSubPresentation presentation;
60
+    uint32_t           clut[256];
61
+    PGSSubPicture      picture;
62
+} PGSSubContext;
63
+
64
+static av_cold int init_decoder(AVCodecContext *avctx)
65
+{
66
+    avctx->pix_fmt     = PIX_FMT_RGB32;
67
+
68
+    return 0;
69
+}
70
+
71
+static av_cold int close_decoder(AVCodecContext *avctx)
72
+{
73
+    PGSSubContext *ctx = avctx->priv_data;
74
+
75
+    av_freep(&ctx->picture.rle);
76
+    ctx->picture.rle_buffer_size  = 0;
77
+
78
+    return 0;
79
+}
80
+
81
+/**
82
+ * Decodes the RLE data.
83
+ *
84
+ * The subtitle is stored as an Run Length Encoded image.
85
+ *
86
+ * @param avctx contains the current codec context
87
+ * @param sub pointer to the processed subtitle data
88
+ * @param buf pointer to the RLE data to process
89
+ * @param buf_size size of the RLE data to process
90
+ */
91
+static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
92
+                      const uint8_t *buf, unsigned int buf_size)
93
+{
94
+    const uint8_t *rle_bitmap_end;
95
+    int pixel_count, line_count;
96
+
97
+    rle_bitmap_end = buf + buf_size;
98
+
99
+    sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h);
100
+
101
+    if (!sub->rects[0]->pict.data[0])
102
+        return -1;
103
+
104
+    pixel_count = 0;
105
+    line_count  = 0;
106
+
107
+    while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
108
+        uint8_t flags, color;
109
+        int run;
110
+
111
+        color = bytestream_get_byte(&buf);
112
+        run   = 1;
113
+
114
+        if (color == 0x00) {
115
+            flags = bytestream_get_byte(&buf);
116
+            run   = flags & 0x3f;
117
+            if (flags & 0x40)
118
+                run = (run << 8) + bytestream_get_byte(&buf);
119
+            color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
120
+        }
121
+
122
+        if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) {
123
+            memset(sub->rects[0]->pict.data[0] + pixel_count, color, run);
124
+            pixel_count += run;
125
+        } else if (!run) {
126
+            /*
127
+             * New Line. Check if correct pixels decoded, if not display warning
128
+             * and adjust bitmap pointer to correct new line position.
129
+             */
130
+            if (pixel_count % sub->rects[0]->w > 0)
131
+                av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
132
+                       pixel_count % sub->rects[0]->w, sub->rects[0]->w);
133
+            line_count++;
134
+        }
135
+    }
136
+
137
+    dprintf(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
138
+
139
+    return 0;
140
+}
141
+
142
+/**
143
+ * Parses the picture segment packet.
144
+ *
145
+ * The picture segment contains details on the sequence id,
146
+ * width, height and Run Length Encoded (RLE) bitmap data.
147
+ *
148
+ * @param avctx contains the current codec context
149
+ * @param buf pointer to the packet to process
150
+ * @param buf_size size of packet to process
151
+ * @todo TODO: Enable support for RLE data over multiple packets
152
+ */
153
+static int parse_picture_segment(AVCodecContext *avctx,
154
+                                  const uint8_t *buf, int buf_size)
155
+{
156
+    PGSSubContext *ctx = avctx->priv_data;
157
+
158
+    uint8_t sequence_desc;
159
+    unsigned int rle_bitmap_len, width, height;
160
+
161
+    /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
162
+    buf += 3;
163
+
164
+    /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
165
+    sequence_desc = bytestream_get_byte(&buf);
166
+
167
+    if (!(sequence_desc & 0x80)) {
168
+        av_log(avctx, AV_LOG_ERROR, "Decoder does not support object data over multiple packets.\n");
169
+        return -1;
170
+    }
171
+
172
+    /* Decode rle bitmap length */
173
+    rle_bitmap_len = bytestream_get_be24(&buf);
174
+
175
+    /* Check to ensure we have enough data for rle_bitmap_length if just a single packet */
176
+    if (rle_bitmap_len > buf_size - 7) {
177
+        av_log(avctx, AV_LOG_ERROR, "Not enough RLE data for specified length of %d.\n", rle_bitmap_len);
178
+        return -1;
179
+    }
180
+
181
+    ctx->picture.rle_data_len = rle_bitmap_len;
182
+
183
+    /* Get bitmap dimensions from data */
184
+    width  = bytestream_get_be16(&buf);
185
+    height = bytestream_get_be16(&buf);
186
+
187
+    /* Make sure the bitmap is not too large */
188
+    if (ctx->presentation.video_w < width || ctx->presentation.video_h < height) {
189
+        av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger then video.\n");
190
+        return -1;
191
+    }
192
+
193
+    ctx->picture.w = width;
194
+    ctx->picture.h = height;
195
+
196
+    av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
197
+
198
+    if (!ctx->picture.rle)
199
+        return -1;
200
+
201
+    memcpy(ctx->picture.rle, buf, rle_bitmap_len);
202
+
203
+    return 0;
204
+}
205
+
206
+/**
207
+ * Parses the palette segment packet.
208
+ *
209
+ * The palette segment contains details of the palette,
210
+ * a maximum of 256 colors can be defined.
211
+ *
212
+ * @param avctx contains the current codec context
213
+ * @param buf pointer to the packet to process
214
+ * @param buf_size size of packet to process
215
+ */
216
+static void parse_palette_segment(AVCodecContext *avctx,
217
+                                  const uint8_t *buf, int buf_size)
218
+{
219
+    PGSSubContext *ctx = avctx->priv_data;
220
+
221
+    const uint8_t *buf_end = buf + buf_size;
222
+    const uint8_t *cm      = ff_cropTbl + MAX_NEG_CROP;
223
+    int color_id;
224
+    int y, cb, cr, alpha;
225
+    int r, g, b, r_add, g_add, b_add;
226
+
227
+    /* Skip two null bytes */
228
+    buf += 2;
229
+
230
+    while (buf < buf_end) {
231
+        color_id  = bytestream_get_byte(&buf);
232
+        y         = bytestream_get_byte(&buf);
233
+        cb        = bytestream_get_byte(&buf);
234
+        cr        = bytestream_get_byte(&buf);
235
+        alpha     = bytestream_get_byte(&buf);
236
+
237
+        YUV_TO_RGB1(cb, cr);
238
+        YUV_TO_RGB2(r, g, b, y);
239
+
240
+        dprintf(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
241
+
242
+        /* Store color in palette */
243
+        ctx->clut[color_id] = RGBA(r,g,b,alpha);
244
+    }
245
+}
246
+
247
+/**
248
+ * Parses the presentation segment packet.
249
+ *
250
+ * The presentation segment contains details on the video
251
+ * width, video height, x & y subtitle position.
252
+ *
253
+ * @param avctx contains the current codec context
254
+ * @param buf pointer to the packet to process
255
+ * @param buf_size size of packet to process
256
+ * @todo TODO: Implement cropping
257
+ * @todo TODO: Implement forcing of subtitles
258
+ * @todo TODO: Blanking of subtitle
259
+ */
260
+static void parse_presentation_segment(AVCodecContext *avctx,
261
+                                       const uint8_t *buf, int buf_size)
262
+{
263
+    PGSSubContext *ctx = avctx->priv_data;
264
+
265
+    int x, y;
266
+    uint8_t block;
267
+
268
+    ctx->presentation.video_w = bytestream_get_be16(&buf);
269
+    ctx->presentation.video_h = bytestream_get_be16(&buf);
270
+
271
+    dprintf(avctx, "Video Dimensions %dx%d\n",
272
+            ctx->presentation.video_w, ctx->presentation.video_h);
273
+
274
+    /* Skip 1 bytes of unknown, frame rate? */
275
+    buf++;
276
+
277
+    ctx->presentation.id_number = bytestream_get_be16(&buf);
278
+
279
+    /* Next byte is the state. */
280
+    block = bytestream_get_byte(&buf);;
281
+    if (block == 0x80) {
282
+        /*
283
+         * Skip 7 bytes of unknown:
284
+         *     palette_update_flag (0x80),
285
+         *     palette_id_to_use,
286
+         *     Object Number (if > 0 determines if more data to process),
287
+         *     object_id_ref (2 bytes),
288
+         *     window_id_ref,
289
+         *     composition_flag (0x80 - object cropped, 0x40 - object forced)
290
+         */
291
+        buf += 7;
292
+
293
+        x = bytestream_get_be16(&buf);
294
+        y = bytestream_get_be16(&buf);
295
+
296
+        /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
297
+
298
+        dprintf(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
299
+
300
+        if (x > ctx->presentation.video_w || y > ctx->presentation.video_h) {
301
+            av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
302
+                   x, y, ctx->presentation.video_w, ctx->presentation.video_h);
303
+            x = 0; y = 0;
304
+        }
305
+
306
+        /* Fill in dimensions */
307
+        ctx->presentation.x = x;
308
+        ctx->presentation.y = y;
309
+    } else if (block == 0x00) {
310
+        /* TODO: Blank context as subtitle should not be displayed.
311
+         *       If the subtitle is blanked now the subtitle is not
312
+         *       on screen long enough to read, due to a delay in
313
+         *       initial display timing.
314
+         */
315
+    }
316
+}
317
+
318
+/**
319
+ * Parses the display segment packet.
320
+ *
321
+ * The display segment controls the updating of the display.
322
+ *
323
+ * @param avctx contains the current codec context
324
+ * @param data pointer to the data pertaining the subtitle to display
325
+ * @param buf pointer to the packet to process
326
+ * @param buf_size size of packet to process
327
+ * @todo TODO: Fix start time, relies on correct PTS, currently too late
328
+ *
329
+ * @todo TODO: Fix end time, normally cleared by a second display
330
+ * @todo       segment, which is currently ignored as it clears
331
+ * @todo       the subtitle too early.
332
+ */
333
+static int display_end_segment(AVCodecContext *avctx, void *data,
334
+                               const uint8_t *buf, int buf_size)
335
+{
336
+    AVSubtitle    *sub = data;
337
+    PGSSubContext *ctx = avctx->priv_data;
338
+
339
+    /*
340
+     *      The end display time is a timeout value and is only reached
341
+     *      if the next subtitle is later then timeout or subtitle has
342
+     *      not been cleared by a subsequent empty display command.
343
+     */
344
+
345
+    sub->start_display_time = 0;
346
+    sub->end_display_time   = 20000;
347
+    sub->format             = 0;
348
+
349
+    if (!sub->rects) {
350
+        sub->rects     = av_mallocz(sizeof(*sub->rects));
351
+        sub->rects[0]  = av_mallocz(sizeof(*sub->rects[0]));
352
+        sub->num_rects = 1;
353
+    }
354
+
355
+    sub->rects[0]->x    = ctx->presentation.x;
356
+    sub->rects[0]->y    = ctx->presentation.y;
357
+    sub->rects[0]->w    = ctx->picture.w;
358
+    sub->rects[0]->h    = ctx->picture.h;
359
+    sub->rects[0]->type = SUBTITLE_BITMAP;
360
+
361
+    /* Process bitmap */
362
+    sub->rects[0]->pict.linesize[0] = ctx->picture.w;
363
+
364
+    if (ctx->picture.rle)
365
+        if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
366
+            return 0;
367
+
368
+    /* Allocate memory for colors */
369
+    sub->rects[0]->nb_colors    = 256;
370
+    sub->rects[0]->pict.data[1] = av_malloc(sub->rects[0]->nb_colors * sizeof(uint32_t));
371
+
372
+    memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
373
+
374
+    return 1;
375
+}
376
+
377
+static int decode(AVCodecContext *avctx, void *data, int *data_size,
378
+                  AVPacket *avpkt)
379
+{
380
+    const uint8_t *buf = avpkt->data;
381
+    int buf_size       = avpkt->size;
382
+
383
+    const uint8_t *buf_end;
384
+    uint8_t       segment_type;
385
+    int           segment_length;
386
+
387
+#ifdef DEBUG_PACKET_CONTENTS
388
+    int i;
389
+
390
+    av_log(avctx, AV_LOG_INFO, "PGS sub packet:\n");
391
+
392
+    for (i = 0; i < buf_size; i++) {
393
+        av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
394
+        if (i % 16 == 15)
395
+            av_log(avctx, AV_LOG_INFO, "\n");
396
+    }
397
+
398
+    if (i & 15)
399
+        av_log(avctx, AV_LOG_INFO, "\n");
400
+#endif
401
+
402
+    *data_size = 0;
403
+
404
+    /* Ensure that we have received at a least a segment code and segment length */
405
+    if (buf_size < 3)
406
+        return -1;
407
+
408
+    buf_end = buf + buf_size;
409
+
410
+    /* Step through buffer to identify segments */
411
+    while (buf < buf_end) {
412
+        segment_type   = bytestream_get_byte(&buf);
413
+        segment_length = bytestream_get_be16(&buf);
414
+
415
+        dprintf(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
416
+
417
+        if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
418
+            break;
419
+
420
+        switch (segment_type) {
421
+        case PALETTE_SEGMENT:
422
+            parse_palette_segment(avctx, buf, segment_length);
423
+            break;
424
+        case PICTURE_SEGMENT:
425
+            parse_picture_segment(avctx, buf, segment_length);
426
+            break;
427
+        case PRESENTATION_SEGMENT:
428
+            parse_presentation_segment(avctx, buf, segment_length);
429
+            break;
430
+        case WINDOW_SEGMENT:
431
+            /*
432
+             * Window Segment Structure (No new information provided):
433
+             *     2 bytes: Unkown,
434
+             *     2 bytes: X position of subtitle,
435
+             *     2 bytes: Y position of subtitle,
436
+             *     2 bytes: Width of subtitle,
437
+             *     2 bytes: Height of subtitle.
438
+             */
439
+            break;
440
+        case DISPLAY_SEGMENT:
441
+            *data_size = display_end_segment(avctx, data, buf, segment_length);
442
+            break;
443
+        default:
444
+            av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
445
+                   segment_type, segment_length);
446
+            break;
447
+        }
448
+
449
+        buf += segment_length;
450
+    }
451
+
452
+    return buf_size;
453
+}
454
+
455
+AVCodec pgssub_decoder = {
456
+    "pgssub",
457
+    CODEC_TYPE_SUBTITLE,
458
+    CODEC_ID_HDMV_PGS_SUBTITLE,
459
+    sizeof(PGSSubContext),
460
+    init_decoder,
461
+    NULL,
462
+    close_decoder,
463
+    decode,
464
+    .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
465
+};
... ...
@@ -507,6 +507,7 @@ static const StreamType ISO_types[] = {
507 507
 static const StreamType HDMV_types[] = {
508 508
     { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
509 509
     { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
510
+    { 0x90, CODEC_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
510 511
     { 0 },
511 512
 };
512 513