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 "libavcore/imgutils.h"
33 //#define DEBUG_PACKET_CONTENTS
35 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
38 PALETTE_SEGMENT = 0x14,
39 PICTURE_SEGMENT = 0x15,
40 PRESENTATION_SEGMENT = 0x16,
41 WINDOW_SEGMENT = 0x17,
42 DISPLAY_SEGMENT = 0x80,
45 typedef struct PGSSubPresentation {
51 typedef struct PGSSubPicture {
55 unsigned int rle_buffer_size, rle_data_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 dprintf(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
143 * Parse the picture segment packet.
145 * The picture segment contains details on the sequence id,
146 * width, height and Run Length Encoded (RLE) bitmap data.
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
153 static int parse_picture_segment(AVCodecContext *avctx,
154 const uint8_t *buf, int buf_size)
156 PGSSubContext *ctx = avctx->priv_data;
158 uint8_t sequence_desc;
159 unsigned int rle_bitmap_len, width, height;
161 /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
164 /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
165 sequence_desc = bytestream_get_byte(&buf);
167 if (!(sequence_desc & 0x80)) {
168 av_log(avctx, AV_LOG_ERROR, "Decoder does not support object data over multiple packets.\n");
172 /* Decode rle bitmap length */
173 rle_bitmap_len = bytestream_get_be24(&buf);
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);
181 ctx->picture.rle_data_len = rle_bitmap_len;
183 /* Get bitmap dimensions from data */
184 width = bytestream_get_be16(&buf);
185 height = bytestream_get_be16(&buf);
187 /* Make sure the bitmap is not too large */
188 if (avctx->width < width || avctx->height < height) {
189 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger then video.\n");
193 ctx->picture.w = width;
194 ctx->picture.h = height;
196 av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
198 if (!ctx->picture.rle)
201 memcpy(ctx->picture.rle, buf, rle_bitmap_len);
207 * Parse the palette segment packet.
209 * The palette segment contains details of the palette,
210 * a maximum of 256 colors can be defined.
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
216 static void parse_palette_segment(AVCodecContext *avctx,
217 const uint8_t *buf, int buf_size)
219 PGSSubContext *ctx = avctx->priv_data;
221 const uint8_t *buf_end = buf + buf_size;
222 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
224 int y, cb, cr, alpha;
225 int r, g, b, r_add, g_add, b_add;
227 /* Skip two null bytes */
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);
238 YUV_TO_RGB2(r, g, b, y);
240 dprintf(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
242 /* Store color in palette */
243 ctx->clut[color_id] = RGBA(r,g,b,alpha);
248 * Parse the presentation segment packet.
250 * The presentation segment contains details on the video
251 * width, video height, x & y subtitle position.
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
260 static void parse_presentation_segment(AVCodecContext *avctx,
261 const uint8_t *buf, int buf_size)
263 PGSSubContext *ctx = avctx->priv_data;
268 int w = bytestream_get_be16(&buf);
269 int h = bytestream_get_be16(&buf);
271 dprintf(avctx, "Video Dimensions %dx%d\n",
273 if (av_image_check_size(w, h, 0, avctx) >= 0)
274 avcodec_set_dimensions(avctx, w, h);
276 /* Skip 1 bytes of unknown, frame rate? */
279 ctx->presentation.id_number = bytestream_get_be16(&buf);
281 /* Next byte is the state. */
282 block = bytestream_get_byte(&buf);;
285 * Skip 7 bytes of unknown:
286 * palette_update_flag (0x80),
288 * Object Number (if > 0 determines if more data to process),
289 * object_id_ref (2 bytes),
291 * composition_flag (0x80 - object cropped, 0x40 - object forced)
295 x = bytestream_get_be16(&buf);
296 y = bytestream_get_be16(&buf);
298 /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
300 dprintf(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
302 if (x > avctx->width || y > avctx->height) {
303 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
304 x, y, avctx->width, avctx->height);
308 /* Fill in dimensions */
309 ctx->presentation.x = x;
310 ctx->presentation.y = y;
311 } else if (block == 0x00) {
312 /* TODO: Blank context as subtitle should not be displayed.
313 * If the subtitle is blanked now the subtitle is not
314 * on screen long enough to read, due to a delay in
315 * initial display timing.
321 * Parse the display segment packet.
323 * The display segment controls the updating of the display.
325 * @param avctx contains the current codec context
326 * @param data pointer to the data pertaining the subtitle to display
327 * @param buf pointer to the packet to process
328 * @param buf_size size of packet to process
329 * @todo TODO: Fix start time, relies on correct PTS, currently too late
331 * @todo TODO: Fix end time, normally cleared by a second display
332 * @todo segment, which is currently ignored as it clears
333 * @todo the subtitle too early.
335 static int display_end_segment(AVCodecContext *avctx, void *data,
336 const uint8_t *buf, int buf_size)
338 AVSubtitle *sub = data;
339 PGSSubContext *ctx = avctx->priv_data;
342 * The end display time is a timeout value and is only reached
343 * if the next subtitle is later then timeout or subtitle has
344 * not been cleared by a subsequent empty display command.
347 memset(sub, 0, sizeof(*sub));
348 sub->start_display_time = 0;
349 sub->end_display_time = 20000;
352 sub->rects = av_mallocz(sizeof(*sub->rects));
353 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
356 sub->rects[0]->x = ctx->presentation.x;
357 sub->rects[0]->y = ctx->presentation.y;
358 sub->rects[0]->w = ctx->picture.w;
359 sub->rects[0]->h = ctx->picture.h;
360 sub->rects[0]->type = SUBTITLE_BITMAP;
363 sub->rects[0]->pict.linesize[0] = ctx->picture.w;
365 if (ctx->picture.rle)
366 if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
369 /* Allocate memory for colors */
370 sub->rects[0]->nb_colors = 256;
371 sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
373 memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
378 static int decode(AVCodecContext *avctx, void *data, int *data_size,
381 const uint8_t *buf = avpkt->data;
382 int buf_size = avpkt->size;
384 const uint8_t *buf_end;
385 uint8_t segment_type;
388 #ifdef DEBUG_PACKET_CONTENTS
391 av_log(avctx, AV_LOG_INFO, "PGS sub packet:\n");
393 for (i = 0; i < buf_size; i++) {
394 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
396 av_log(avctx, AV_LOG_INFO, "\n");
400 av_log(avctx, AV_LOG_INFO, "\n");
405 /* Ensure that we have received at a least a segment code and segment length */
409 buf_end = buf + buf_size;
411 /* Step through buffer to identify segments */
412 while (buf < buf_end) {
413 segment_type = bytestream_get_byte(&buf);
414 segment_length = bytestream_get_be16(&buf);
416 dprintf(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
418 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
421 switch (segment_type) {
422 case PALETTE_SEGMENT:
423 parse_palette_segment(avctx, buf, segment_length);
425 case PICTURE_SEGMENT:
426 parse_picture_segment(avctx, buf, segment_length);
428 case PRESENTATION_SEGMENT:
429 parse_presentation_segment(avctx, buf, segment_length);
433 * Window Segment Structure (No new information provided):
435 * 2 bytes: X position of subtitle,
436 * 2 bytes: Y position of subtitle,
437 * 2 bytes: Width of subtitle,
438 * 2 bytes: Height of subtitle.
441 case DISPLAY_SEGMENT:
442 *data_size = display_end_segment(avctx, data, buf, segment_length);
445 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
446 segment_type, segment_length);
450 buf += segment_length;
456 AVCodec pgssub_decoder = {
458 AVMEDIA_TYPE_SUBTITLE,
459 CODEC_ID_HDMV_PGS_SUBTITLE,
460 sizeof(PGSSubContext),
465 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),