3 * Copyright (c) 2009 Stephen Backway
5 * This file is part of FFmpeg.
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.
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.
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
24 * PGS subtitle decoder
29 #include "bytestream.h"
30 #include "libavutil/colorspace.h"
31 #include "libavutil/imgutils.h"
33 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
36 PALETTE_SEGMENT = 0x14,
37 PICTURE_SEGMENT = 0x15,
38 PRESENTATION_SEGMENT = 0x16,
39 WINDOW_SEGMENT = 0x17,
40 DISPLAY_SEGMENT = 0x80,
43 typedef struct PGSSubPresentation {
50 typedef struct PGSSubPicture {
54 unsigned int rle_buffer_size, rle_data_len;
55 unsigned int rle_remaining_len;
58 typedef struct PGSSubContext {
59 PGSSubPresentation presentation;
61 PGSSubPicture picture;
64 static av_cold int init_decoder(AVCodecContext *avctx)
66 avctx->pix_fmt = PIX_FMT_PAL8;
71 static av_cold int close_decoder(AVCodecContext *avctx)
73 PGSSubContext *ctx = avctx->priv_data;
75 av_freep(&ctx->picture.rle);
76 ctx->picture.rle_buffer_size = 0;
82 * Decode the RLE data.
84 * The subtitle is stored as an Run Length Encoded image.
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
91 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
92 const uint8_t *buf, unsigned int buf_size)
94 const uint8_t *rle_bitmap_end;
95 int pixel_count, line_count;
97 rle_bitmap_end = buf + buf_size;
99 sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h);
101 if (!sub->rects[0]->pict.data[0])
107 while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
108 uint8_t flags, color;
111 color = bytestream_get_byte(&buf);
115 flags = bytestream_get_byte(&buf);
118 run = (run << 8) + bytestream_get_byte(&buf);
119 color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
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);
127 * New Line. Check if correct pixels decoded, if not display warning
128 * and adjust bitmap pointer to correct new line position.
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);
137 if (pixel_count < sub->rects[0]->w * sub->rects[0]->h) {
138 av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
142 av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
148 * Parse the picture segment packet.
150 * The picture segment contains details on the sequence id,
151 * width, height and Run Length Encoded (RLE) bitmap data.
153 * @param avctx contains the current codec context
154 * @param buf pointer to the packet to process
155 * @param buf_size size of packet to process
156 * @todo TODO: Enable support for RLE data over multiple packets
158 static int parse_picture_segment(AVCodecContext *avctx,
159 const uint8_t *buf, int buf_size)
161 PGSSubContext *ctx = avctx->priv_data;
163 uint8_t sequence_desc;
164 unsigned int rle_bitmap_len, width, height;
170 /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
173 /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
174 sequence_desc = bytestream_get_byte(&buf);
176 if (!(sequence_desc & 0x80)) {
177 /* Additional RLE data */
178 if (buf_size > ctx->picture.rle_remaining_len)
181 memcpy(ctx->picture.rle + ctx->picture.rle_data_len, buf, buf_size);
182 ctx->picture.rle_data_len += buf_size;
183 ctx->picture.rle_remaining_len -= buf_size;
192 /* Decode rle bitmap length, stored size includes width/height data */
193 rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
195 /* Get bitmap dimensions from data */
196 width = bytestream_get_be16(&buf);
197 height = bytestream_get_be16(&buf);
199 /* Make sure the bitmap is not too large */
200 if (avctx->width < width || avctx->height < height) {
201 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
205 ctx->picture.w = width;
206 ctx->picture.h = height;
208 av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
210 if (!ctx->picture.rle)
213 memcpy(ctx->picture.rle, buf, buf_size);
214 ctx->picture.rle_data_len = buf_size;
215 ctx->picture.rle_remaining_len = rle_bitmap_len - buf_size;
221 * Parse the palette segment packet.
223 * The palette segment contains details of the palette,
224 * a maximum of 256 colors can be defined.
226 * @param avctx contains the current codec context
227 * @param buf pointer to the packet to process
228 * @param buf_size size of packet to process
230 static void parse_palette_segment(AVCodecContext *avctx,
231 const uint8_t *buf, int buf_size)
233 PGSSubContext *ctx = avctx->priv_data;
235 const uint8_t *buf_end = buf + buf_size;
236 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
238 int y, cb, cr, alpha;
239 int r, g, b, r_add, g_add, b_add;
241 /* Skip two null bytes */
244 while (buf < buf_end) {
245 color_id = bytestream_get_byte(&buf);
246 y = bytestream_get_byte(&buf);
247 cr = bytestream_get_byte(&buf);
248 cb = bytestream_get_byte(&buf);
249 alpha = bytestream_get_byte(&buf);
252 YUV_TO_RGB2(r, g, b, y);
254 av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
256 /* Store color in palette */
257 ctx->clut[color_id] = RGBA(r,g,b,alpha);
262 * Parse the presentation segment packet.
264 * The presentation segment contains details on the video
265 * width, video height, x & y subtitle position.
267 * @param avctx contains the current codec context
268 * @param buf pointer to the packet to process
269 * @param buf_size size of packet to process
270 * @todo TODO: Implement cropping
271 * @todo TODO: Implement forcing of subtitles
273 static void parse_presentation_segment(AVCodecContext *avctx,
274 const uint8_t *buf, int buf_size)
276 PGSSubContext *ctx = avctx->priv_data;
280 int w = bytestream_get_be16(&buf);
281 int h = bytestream_get_be16(&buf);
283 av_dlog(avctx, "Video Dimensions %dx%d\n",
285 if (av_image_check_size(w, h, 0, avctx) >= 0)
286 avcodec_set_dimensions(avctx, w, h);
288 /* Skip 1 bytes of unknown, frame rate? */
291 ctx->presentation.id_number = bytestream_get_be16(&buf);
294 * Skip 3 bytes of unknown:
296 * palette_update_flag (0x80),
301 ctx->presentation.object_number = bytestream_get_byte(&buf);
302 if (!ctx->presentation.object_number)
306 * Skip 4 bytes of unknown:
307 * object_id_ref (2 bytes),
309 * composition_flag (0x80 - object cropped, 0x40 - object forced)
313 x = bytestream_get_be16(&buf);
314 y = bytestream_get_be16(&buf);
316 /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
318 av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
320 if (x > avctx->width || y > avctx->height) {
321 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
322 x, y, avctx->width, avctx->height);
326 /* Fill in dimensions */
327 ctx->presentation.x = x;
328 ctx->presentation.y = y;
332 * Parse the display segment packet.
334 * The display segment controls the updating of the display.
336 * @param avctx contains the current codec context
337 * @param data pointer to the data pertaining the subtitle to display
338 * @param buf pointer to the packet to process
339 * @param buf_size size of packet to process
340 * @todo TODO: Fix start time, relies on correct PTS, currently too late
342 * @todo TODO: Fix end time, normally cleared by a second display
343 * @todo segment, which is currently ignored as it clears
344 * @todo the subtitle too early.
346 static int display_end_segment(AVCodecContext *avctx, void *data,
347 const uint8_t *buf, int buf_size)
349 AVSubtitle *sub = data;
350 PGSSubContext *ctx = avctx->priv_data;
353 * The end display time is a timeout value and is only reached
354 * if the next subtitle is later then timeout or subtitle has
355 * not been cleared by a subsequent empty display command.
358 // Blank if last object_number was 0.
359 // Note that this may be wrong for more complex subtitles.
360 if (!ctx->presentation.object_number)
362 sub->start_display_time = 0;
363 sub->end_display_time = 20000;
366 sub->rects = av_mallocz(sizeof(*sub->rects));
367 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
370 sub->rects[0]->x = ctx->presentation.x;
371 sub->rects[0]->y = ctx->presentation.y;
372 sub->rects[0]->w = ctx->picture.w;
373 sub->rects[0]->h = ctx->picture.h;
374 sub->rects[0]->type = SUBTITLE_BITMAP;
377 sub->rects[0]->pict.linesize[0] = ctx->picture.w;
379 if (ctx->picture.rle) {
380 if (ctx->picture.rle_remaining_len)
381 av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
382 ctx->picture.rle_data_len, ctx->picture.rle_remaining_len);
383 if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
386 /* Allocate memory for colors */
387 sub->rects[0]->nb_colors = 256;
388 sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
390 memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
395 static int decode(AVCodecContext *avctx, void *data, int *data_size,
398 const uint8_t *buf = avpkt->data;
399 int buf_size = avpkt->size;
401 const uint8_t *buf_end;
402 uint8_t segment_type;
406 av_dlog(avctx, "PGS sub packet:\n");
408 for (i = 0; i < buf_size; i++) {
409 av_dlog(avctx, "%02x ", buf[i]);
411 av_dlog(avctx, "\n");
415 av_dlog(avctx, "\n");
419 /* Ensure that we have received at a least a segment code and segment length */
423 buf_end = buf + buf_size;
425 /* Step through buffer to identify segments */
426 while (buf < buf_end) {
427 segment_type = bytestream_get_byte(&buf);
428 segment_length = bytestream_get_be16(&buf);
430 av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
432 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
435 switch (segment_type) {
436 case PALETTE_SEGMENT:
437 parse_palette_segment(avctx, buf, segment_length);
439 case PICTURE_SEGMENT:
440 parse_picture_segment(avctx, buf, segment_length);
442 case PRESENTATION_SEGMENT:
443 parse_presentation_segment(avctx, buf, segment_length);
447 * Window Segment Structure (No new information provided):
449 * 2 bytes: X position of subtitle,
450 * 2 bytes: Y position of subtitle,
451 * 2 bytes: Width of subtitle,
452 * 2 bytes: Height of subtitle.
455 case DISPLAY_SEGMENT:
456 *data_size = display_end_segment(avctx, data, buf, segment_length);
459 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
460 segment_type, segment_length);
464 buf += segment_length;
470 AVCodec ff_pgssub_decoder = {
472 .type = AVMEDIA_TYPE_SUBTITLE,
473 .id = CODEC_ID_HDMV_PGS_SUBTITLE,
474 .priv_data_size = sizeof(PGSSubContext),
475 .init = init_decoder,
476 .close = close_decoder,
478 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),