]> git.sesse.net Git - ffmpeg/blob - libavcodec/pgssubdec.c
Merge commit '984e3398662d460e15904f9e4a6df9ef759070cb'
[ffmpeg] / libavcodec / pgssubdec.c
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 "dsputil.h"
29 #include "bytestream.h"
30 #include "internal.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 enum SegmentType {
39     PALETTE_SEGMENT      = 0x14,
40     PICTURE_SEGMENT      = 0x15,
41     PRESENTATION_SEGMENT = 0x16,
42     WINDOW_SEGMENT       = 0x17,
43     DISPLAY_SEGMENT      = 0x80,
44 };
45
46 typedef struct PGSSubPictureReference {
47     int x;
48     int y;
49     int picture_id;
50     int composition;
51 } PGSSubPictureReference;
52
53 typedef struct PGSSubPresentation {
54     int                    id_number;
55     int                    object_count;
56     PGSSubPictureReference *objects;
57     int64_t pts;
58 } PGSSubPresentation;
59
60 typedef struct PGSSubPicture {
61     int          w;
62     int          h;
63     uint8_t      *rle;
64     unsigned int rle_buffer_size, rle_data_len;
65     unsigned int rle_remaining_len;
66 } PGSSubPicture;
67
68 typedef struct PGSSubContext {
69     AVClass *class;
70     PGSSubPresentation presentation;
71     uint32_t           clut[256];
72     PGSSubPicture      pictures[UINT16_MAX];
73     int forced_subs_only;
74 } PGSSubContext;
75
76 static av_cold int init_decoder(AVCodecContext *avctx)
77 {
78     avctx->pix_fmt     = AV_PIX_FMT_PAL8;
79
80     return 0;
81 }
82
83 static av_cold int close_decoder(AVCodecContext *avctx)
84 {
85     uint16_t picture;
86
87     PGSSubContext *ctx = avctx->priv_data;
88
89     av_freep(&ctx->presentation.objects);
90     ctx->presentation.object_count = 0;
91
92     for (picture = 0; picture < UINT16_MAX; ++picture) {
93         av_freep(&ctx->pictures[picture].rle);
94         ctx->pictures[picture].rle_buffer_size = 0;
95     }
96
97     return 0;
98 }
99
100 /**
101  * Decode the RLE data.
102  *
103  * The subtitle is stored as a Run Length Encoded image.
104  *
105  * @param avctx contains the current codec context
106  * @param sub pointer to the processed subtitle data
107  * @param buf pointer to the RLE data to process
108  * @param buf_size size of the RLE data to process
109  */
110 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub, int rect,
111                       const uint8_t *buf, unsigned int buf_size)
112 {
113     const uint8_t *rle_bitmap_end;
114     int pixel_count, line_count;
115
116     rle_bitmap_end = buf + buf_size;
117
118     sub->rects[rect]->pict.data[0] = av_malloc(sub->rects[rect]->w * sub->rects[rect]->h);
119
120     if (!sub->rects[rect]->pict.data[0])
121         return -1;
122
123     pixel_count = 0;
124     line_count  = 0;
125
126     while (buf < rle_bitmap_end && line_count < sub->rects[rect]->h) {
127         uint8_t flags, color;
128         int run;
129
130         color = bytestream_get_byte(&buf);
131         run   = 1;
132
133         if (color == 0x00) {
134             flags = bytestream_get_byte(&buf);
135             run   = flags & 0x3f;
136             if (flags & 0x40)
137                 run = (run << 8) + bytestream_get_byte(&buf);
138             color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
139         }
140
141         if (run > 0 && pixel_count + run <= sub->rects[rect]->w * sub->rects[rect]->h) {
142             memset(sub->rects[rect]->pict.data[0] + pixel_count, color, run);
143             pixel_count += run;
144         } else if (!run) {
145             /*
146              * New Line. Check if correct pixels decoded, if not display warning
147              * and adjust bitmap pointer to correct new line position.
148              */
149             if (pixel_count % sub->rects[rect]->w > 0)
150                 av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
151                        pixel_count % sub->rects[rect]->w, sub->rects[rect]->w);
152             line_count++;
153         }
154     }
155
156     if (pixel_count < sub->rects[rect]->w * sub->rects[rect]->h) {
157         av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
158         return -1;
159     }
160
161     av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[rect]->w * sub->rects[rect]->h);
162
163     return 0;
164 }
165
166 /**
167  * Parse the picture segment packet.
168  *
169  * The picture segment contains details on the sequence id,
170  * width, height and Run Length Encoded (RLE) bitmap data.
171  *
172  * @param avctx contains the current codec context
173  * @param buf pointer to the packet to process
174  * @param buf_size size of packet to process
175  * @todo TODO: Enable support for RLE data over multiple packets
176  */
177 static int parse_picture_segment(AVCodecContext *avctx,
178                                   const uint8_t *buf, int buf_size)
179 {
180     PGSSubContext *ctx = avctx->priv_data;
181
182     uint8_t sequence_desc;
183     unsigned int rle_bitmap_len, width, height;
184     uint16_t picture_id;
185
186     if (buf_size <= 4)
187         return -1;
188     buf_size -= 4;
189
190     picture_id = bytestream_get_be16(&buf);
191
192     /* skip 1 unknown byte: Version Number */
193     buf++;
194
195     /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
196     sequence_desc = bytestream_get_byte(&buf);
197
198     if (!(sequence_desc & 0x80)) {
199         /* Additional RLE data */
200         if (buf_size > ctx->pictures[picture_id].rle_remaining_len)
201             return -1;
202
203         memcpy(ctx->pictures[picture_id].rle + ctx->pictures[picture_id].rle_data_len, buf, buf_size);
204         ctx->pictures[picture_id].rle_data_len += buf_size;
205         ctx->pictures[picture_id].rle_remaining_len -= buf_size;
206
207         return 0;
208     }
209
210     if (buf_size <= 7)
211         return -1;
212     buf_size -= 7;
213
214     /* Decode rle bitmap length, stored size includes width/height data */
215     rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
216
217     /* Get bitmap dimensions from data */
218     width  = bytestream_get_be16(&buf);
219     height = bytestream_get_be16(&buf);
220
221     /* Make sure the bitmap is not too large */
222     if (avctx->width < width || avctx->height < height) {
223         av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
224         return -1;
225     }
226
227     if (buf_size > rle_bitmap_len) {
228         av_log(avctx, AV_LOG_ERROR, "too much RLE data\n");
229         return AVERROR_INVALIDDATA;
230     }
231
232     ctx->pictures[picture_id].w = width;
233     ctx->pictures[picture_id].h = height;
234
235     av_fast_padded_malloc(&ctx->pictures[picture_id].rle, &ctx->pictures[picture_id].rle_buffer_size, rle_bitmap_len);
236
237     if (!ctx->pictures[picture_id].rle)
238         return -1;
239
240     memcpy(ctx->pictures[picture_id].rle, buf, buf_size);
241     ctx->pictures[picture_id].rle_data_len      = buf_size;
242     ctx->pictures[picture_id].rle_remaining_len = rle_bitmap_len - buf_size;
243
244     return 0;
245 }
246
247 /**
248  * Parse the palette segment packet.
249  *
250  * The palette segment contains details of the palette,
251  * a maximum of 256 colors can be defined.
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  */
257 static void parse_palette_segment(AVCodecContext *avctx,
258                                   const uint8_t *buf, int buf_size)
259 {
260     PGSSubContext *ctx = avctx->priv_data;
261
262     const uint8_t *buf_end = buf + buf_size;
263     const uint8_t *cm      = ff_cropTbl + MAX_NEG_CROP;
264     int color_id;
265     int y, cb, cr, alpha;
266     int r, g, b, r_add, g_add, b_add;
267
268     /* Skip two null bytes */
269     buf += 2;
270
271     while (buf < buf_end) {
272         color_id  = bytestream_get_byte(&buf);
273         y         = bytestream_get_byte(&buf);
274         cr        = bytestream_get_byte(&buf);
275         cb        = bytestream_get_byte(&buf);
276         alpha     = bytestream_get_byte(&buf);
277
278         YUV_TO_RGB1(cb, cr);
279         YUV_TO_RGB2(r, g, b, y);
280
281         av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
282
283         /* Store color in palette */
284         ctx->clut[color_id] = RGBA(r,g,b,alpha);
285     }
286 }
287
288 /**
289  * Parse the presentation segment packet.
290  *
291  * The presentation segment contains details on the video
292  * width, video height, x & y subtitle position.
293  *
294  * @param avctx contains the current codec context
295  * @param buf pointer to the packet to process
296  * @param buf_size size of packet to process
297  * @todo TODO: Implement cropping
298  */
299 static int parse_presentation_segment(AVCodecContext *avctx,
300                                       const uint8_t *buf, int buf_size,
301                                       int64_t pts)
302 {
303     PGSSubContext *ctx = avctx->priv_data;
304     int ret;
305
306     int w = bytestream_get_be16(&buf);
307     int h = bytestream_get_be16(&buf);
308
309     uint16_t object_index;
310
311     ctx->presentation.pts = pts;
312
313     av_dlog(avctx, "Video Dimensions %dx%d\n",
314             w, h);
315     ret = ff_set_dimensions(avctx, w, h);
316     if (ret < 0)
317         return ret;
318
319     /* Skip 1 bytes of unknown, frame rate? */
320     buf++;
321
322     ctx->presentation.id_number = bytestream_get_be16(&buf);
323
324     /*
325      * Skip 3 bytes of unknown:
326      *     state
327      *     palette_update_flag (0x80),
328      *     palette_id_to_use,
329      */
330     buf += 3;
331
332     ctx->presentation.object_count = bytestream_get_byte(&buf);
333     if (!ctx->presentation.object_count)
334         return 0;
335
336     /* Verify that enough bytes are remaining for all of the objects. */
337     buf_size -= 11;
338     if (buf_size < ctx->presentation.object_count * 8) {
339         ctx->presentation.object_count = 0;
340         return AVERROR_INVALIDDATA;
341     }
342
343     av_freep(&ctx->presentation.objects);
344     ctx->presentation.objects = av_malloc(sizeof(PGSSubPictureReference) * ctx->presentation.object_count);
345     if (!ctx->presentation.objects) {
346         ctx->presentation.object_count = 0;
347         return AVERROR(ENOMEM);
348     }
349
350     for (object_index = 0; object_index < ctx->presentation.object_count; ++object_index) {
351         PGSSubPictureReference *reference = &ctx->presentation.objects[object_index];
352         reference->picture_id             = bytestream_get_be16(&buf);
353
354         /* Skip window_id_ref */
355         buf++;
356         /* composition_flag (0x80 - object cropped, 0x40 - object forced) */
357         reference->composition = bytestream_get_byte(&buf);
358
359         reference->x = bytestream_get_be16(&buf);
360         reference->y = bytestream_get_be16(&buf);
361
362         /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
363         av_dlog(avctx, "Subtitle Placement ID=%d, x=%d, y=%d\n", reference->picture_id, reference->x, reference->y);
364
365         if (reference->x > avctx->width || reference->y > avctx->height) {
366             av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
367                    reference->x, reference->y, avctx->width, avctx->height);
368             reference->x = 0;
369             reference->y = 0;
370         }
371     }
372
373     return 0;
374 }
375
376 /**
377  * Parse the display segment packet.
378  *
379  * The display segment controls the updating of the display.
380  *
381  * @param avctx contains the current codec context
382  * @param data pointer to the data pertaining the subtitle to display
383  * @param buf pointer to the packet to process
384  * @param buf_size size of packet to process
385  * @todo TODO: Fix start time, relies on correct PTS, currently too late
386  *
387  * @todo TODO: Fix end time, normally cleared by a second display
388  * @todo       segment, which is currently ignored as it clears
389  * @todo       the subtitle too early.
390  */
391 static int display_end_segment(AVCodecContext *avctx, void *data,
392                                const uint8_t *buf, int buf_size)
393 {
394     AVSubtitle    *sub = data;
395     PGSSubContext *ctx = avctx->priv_data;
396     int64_t pts;
397
398     uint16_t rect;
399
400     /*
401      *      The end display time is a timeout value and is only reached
402      *      if the next subtitle is later than timeout or subtitle has
403      *      not been cleared by a subsequent empty display command.
404      */
405
406     pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts;
407     memset(sub, 0, sizeof(*sub));
408     sub->pts = pts;
409     ctx->presentation.pts = AV_NOPTS_VALUE;
410
411     // Blank if last object_count was 0.
412     if (!ctx->presentation.object_count)
413         return 1;
414
415     sub->start_display_time = 0;
416     sub->end_display_time   = 20000;
417     sub->format             = 0;
418
419     sub->num_rects = ctx->presentation.object_count;
420     sub->rects     = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
421
422     for (rect = 0; rect < sub->num_rects; ++rect) {
423         uint16_t picture_id    = ctx->presentation.objects[rect].picture_id;
424         sub->rects[rect]       = av_mallocz(sizeof(*sub->rects[rect]));
425         sub->rects[rect]->x    = ctx->presentation.objects[rect].x;
426         sub->rects[rect]->y    = ctx->presentation.objects[rect].y;
427         sub->rects[rect]->w    = ctx->pictures[picture_id].w;
428         sub->rects[rect]->h    = ctx->pictures[picture_id].h;
429         sub->rects[rect]->type = SUBTITLE_BITMAP;
430
431         /* Process bitmap */
432         sub->rects[rect]->pict.linesize[0] = ctx->pictures[picture_id].w;
433         if (ctx->pictures[picture_id].rle) {
434             if (ctx->pictures[picture_id].rle_remaining_len)
435                 av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
436                        ctx->pictures[picture_id].rle_data_len, ctx->pictures[picture_id].rle_remaining_len);
437             if (decode_rle(avctx, sub, rect, ctx->pictures[picture_id].rle, ctx->pictures[picture_id].rle_data_len) < 0)
438                 return 0;
439         }
440
441         /* Allocate memory for colors */
442         sub->rects[rect]->nb_colors    = 256;
443         sub->rects[rect]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
444
445         /* Copy the forced flag */
446         sub->rects[rect]->flags = (ctx->presentation.objects[rect].composition & 0x40) != 0 ? AV_SUBTITLE_FLAG_FORCED : 0;
447
448         if (!ctx->forced_subs_only || ctx->presentation.objects[rect].composition & 0x40)
449         memcpy(sub->rects[rect]->pict.data[1], ctx->clut, sub->rects[rect]->nb_colors * sizeof(uint32_t));
450     }
451
452     return 1;
453 }
454
455 static int decode(AVCodecContext *avctx, void *data, int *data_size,
456                   AVPacket *avpkt)
457 {
458     const uint8_t *buf = avpkt->data;
459     int buf_size       = avpkt->size;
460     AVSubtitle *sub    = data;
461
462     const uint8_t *buf_end;
463     uint8_t       segment_type;
464     int           segment_length;
465     int i, ret;
466
467     av_dlog(avctx, "PGS sub packet:\n");
468
469     for (i = 0; i < buf_size; i++) {
470         av_dlog(avctx, "%02x ", buf[i]);
471         if (i % 16 == 15)
472             av_dlog(avctx, "\n");
473     }
474
475     if (i & 15)
476         av_dlog(avctx, "\n");
477
478     *data_size = 0;
479
480     /* Ensure that we have received at a least a segment code and segment length */
481     if (buf_size < 3)
482         return -1;
483
484     buf_end = buf + buf_size;
485
486     /* Step through buffer to identify segments */
487     while (buf < buf_end) {
488         segment_type   = bytestream_get_byte(&buf);
489         segment_length = bytestream_get_be16(&buf);
490
491         av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
492
493         if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
494             break;
495
496         switch (segment_type) {
497         case PALETTE_SEGMENT:
498             parse_palette_segment(avctx, buf, segment_length);
499             break;
500         case PICTURE_SEGMENT:
501             parse_picture_segment(avctx, buf, segment_length);
502             break;
503         case PRESENTATION_SEGMENT:
504             ret = parse_presentation_segment(avctx, buf, segment_length, sub->pts);
505             if (ret < 0)
506                 return ret;
507             break;
508         case WINDOW_SEGMENT:
509             /*
510              * Window Segment Structure (No new information provided):
511              *     2 bytes: Unknown,
512              *     2 bytes: X position of subtitle,
513              *     2 bytes: Y position of subtitle,
514              *     2 bytes: Width of subtitle,
515              *     2 bytes: Height of subtitle.
516              */
517             break;
518         case DISPLAY_SEGMENT:
519             *data_size = display_end_segment(avctx, data, buf, segment_length);
520             break;
521         default:
522             av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
523                    segment_type, segment_length);
524             break;
525         }
526
527         buf += segment_length;
528     }
529
530     return buf_size;
531 }
532
533 #define OFFSET(x) offsetof(PGSSubContext, x)
534 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
535 static const AVOption options[] = {
536     {"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
537     { NULL },
538 };
539
540 static const AVClass pgsdec_class = {
541     .class_name = "PGS subtitle decoder",
542     .item_name  = av_default_item_name,
543     .option     = options,
544     .version    = LIBAVUTIL_VERSION_INT,
545 };
546
547 AVCodec ff_pgssub_decoder = {
548     .name           = "pgssub",
549     .long_name      = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
550     .type           = AVMEDIA_TYPE_SUBTITLE,
551     .id             = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
552     .priv_data_size = sizeof(PGSSubContext),
553     .init           = init_decoder,
554     .close          = close_decoder,
555     .decode         = decode,
556     .priv_class     = &pgsdec_class,
557 };