comparison pgssubdec.c @ 10083:8071f8b68d05 libavcodec

Add Bluray Subtitle Support Patch by Stephen Backway, stev391 A exemail D com D au
author cehoyos
date Tue, 25 Aug 2009 08:47:39 +0000
parents
children 8f1e09db0905
comparison
equal deleted inserted replaced
10082:9f4b529bd5c0 10083:8071f8b68d05
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 libavcodec/pgssubdec.c
24 * PGS subtitle decoder
25 */
26
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "colorspace.h"
30 #include "bytestream.h"
31
32 //#define DEBUG_PACKET_CONTENTS
33
34 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
35
36 enum SegmentType {
37 PALETTE_SEGMENT = 0x14,
38 PICTURE_SEGMENT = 0x15,
39 PRESENTATION_SEGMENT = 0x16,
40 WINDOW_SEGMENT = 0x17,
41 DISPLAY_SEGMENT = 0x80,
42 };
43
44 typedef struct PGSSubPresentation {
45 int x;
46 int y;
47 int video_w;
48 int video_h;
49 int id_number;
50 } PGSSubPresentation;
51
52 typedef struct PGSSubPicture {
53 int w;
54 int h;
55 uint8_t *rle;
56 unsigned int rle_buffer_size, rle_data_len;
57 } PGSSubPicture;
58
59 typedef struct PGSSubContext {
60 PGSSubPresentation presentation;
61 uint32_t clut[256];
62 PGSSubPicture picture;
63 } PGSSubContext;
64
65 static av_cold int init_decoder(AVCodecContext *avctx)
66 {
67 avctx->pix_fmt = PIX_FMT_RGB32;
68
69 return 0;
70 }
71
72 static av_cold int close_decoder(AVCodecContext *avctx)
73 {
74 PGSSubContext *ctx = avctx->priv_data;
75
76 av_freep(&ctx->picture.rle);
77 ctx->picture.rle_buffer_size = 0;
78
79 return 0;
80 }
81
82 /**
83 * Decodes the RLE data.
84 *
85 * The subtitle is stored as an Run Length Encoded image.
86 *
87 * @param avctx contains the current codec context
88 * @param sub pointer to the processed subtitle data
89 * @param buf pointer to the RLE data to process
90 * @param buf_size size of the RLE data to process
91 */
92 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
93 const uint8_t *buf, unsigned int buf_size)
94 {
95 const uint8_t *rle_bitmap_end;
96 int pixel_count, line_count;
97
98 rle_bitmap_end = buf + buf_size;
99
100 sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h);
101
102 if (!sub->rects[0]->pict.data[0])
103 return -1;
104
105 pixel_count = 0;
106 line_count = 0;
107
108 while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
109 uint8_t flags, color;
110 int run;
111
112 color = bytestream_get_byte(&buf);
113 run = 1;
114
115 if (color == 0x00) {
116 flags = bytestream_get_byte(&buf);
117 run = flags & 0x3f;
118 if (flags & 0x40)
119 run = (run << 8) + bytestream_get_byte(&buf);
120 color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
121 }
122
123 if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) {
124 memset(sub->rects[0]->pict.data[0] + pixel_count, color, run);
125 pixel_count += run;
126 } else if (!run) {
127 /*
128 * New Line. Check if correct pixels decoded, if not display warning
129 * and adjust bitmap pointer to correct new line position.
130 */
131 if (pixel_count % sub->rects[0]->w > 0)
132 av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
133 pixel_count % sub->rects[0]->w, sub->rects[0]->w);
134 line_count++;
135 }
136 }
137
138 dprintf(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
139
140 return 0;
141 }
142
143 /**
144 * Parses the picture segment packet.
145 *
146 * The picture segment contains details on the sequence id,
147 * width, height and Run Length Encoded (RLE) bitmap data.
148 *
149 * @param avctx contains the current codec context
150 * @param buf pointer to the packet to process
151 * @param buf_size size of packet to process
152 * @todo TODO: Enable support for RLE data over multiple packets
153 */
154 static int parse_picture_segment(AVCodecContext *avctx,
155 const uint8_t *buf, int buf_size)
156 {
157 PGSSubContext *ctx = avctx->priv_data;
158
159 uint8_t sequence_desc;
160 unsigned int rle_bitmap_len, width, height;
161
162 /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
163 buf += 3;
164
165 /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
166 sequence_desc = bytestream_get_byte(&buf);
167
168 if (!(sequence_desc & 0x80)) {
169 av_log(avctx, AV_LOG_ERROR, "Decoder does not support object data over multiple packets.\n");
170 return -1;
171 }
172
173 /* Decode rle bitmap length */
174 rle_bitmap_len = bytestream_get_be24(&buf);
175
176 /* Check to ensure we have enough data for rle_bitmap_length if just a single packet */
177 if (rle_bitmap_len > buf_size - 7) {
178 av_log(avctx, AV_LOG_ERROR, "Not enough RLE data for specified length of %d.\n", rle_bitmap_len);
179 return -1;
180 }
181
182 ctx->picture.rle_data_len = rle_bitmap_len;
183
184 /* Get bitmap dimensions from data */
185 width = bytestream_get_be16(&buf);
186 height = bytestream_get_be16(&buf);
187
188 /* Make sure the bitmap is not too large */
189 if (ctx->presentation.video_w < width || ctx->presentation.video_h < height) {
190 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger then video.\n");
191 return -1;
192 }
193
194 ctx->picture.w = width;
195 ctx->picture.h = height;
196
197 av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
198
199 if (!ctx->picture.rle)
200 return -1;
201
202 memcpy(ctx->picture.rle, buf, rle_bitmap_len);
203
204 return 0;
205 }
206
207 /**
208 * Parses the palette segment packet.
209 *
210 * The palette segment contains details of the palette,
211 * a maximum of 256 colors can be defined.
212 *
213 * @param avctx contains the current codec context
214 * @param buf pointer to the packet to process
215 * @param buf_size size of packet to process
216 */
217 static void parse_palette_segment(AVCodecContext *avctx,
218 const uint8_t *buf, int buf_size)
219 {
220 PGSSubContext *ctx = avctx->priv_data;
221
222 const uint8_t *buf_end = buf + buf_size;
223 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
224 int color_id;
225 int y, cb, cr, alpha;
226 int r, g, b, r_add, g_add, b_add;
227
228 /* Skip two null bytes */
229 buf += 2;
230
231 while (buf < buf_end) {
232 color_id = bytestream_get_byte(&buf);
233 y = bytestream_get_byte(&buf);
234 cb = bytestream_get_byte(&buf);
235 cr = bytestream_get_byte(&buf);
236 alpha = bytestream_get_byte(&buf);
237
238 YUV_TO_RGB1(cb, cr);
239 YUV_TO_RGB2(r, g, b, y);
240
241 dprintf(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
242
243 /* Store color in palette */
244 ctx->clut[color_id] = RGBA(r,g,b,alpha);
245 }
246 }
247
248 /**
249 * Parses the presentation segment packet.
250 *
251 * The presentation segment contains details on the video
252 * width, video height, x & y subtitle position.
253 *
254 * @param avctx contains the current codec context
255 * @param buf pointer to the packet to process
256 * @param buf_size size of packet to process
257 * @todo TODO: Implement cropping
258 * @todo TODO: Implement forcing of subtitles
259 * @todo TODO: Blanking of subtitle
260 */
261 static void parse_presentation_segment(AVCodecContext *avctx,
262 const uint8_t *buf, int buf_size)
263 {
264 PGSSubContext *ctx = avctx->priv_data;
265
266 int x, y;
267 uint8_t block;
268
269 ctx->presentation.video_w = bytestream_get_be16(&buf);
270 ctx->presentation.video_h = bytestream_get_be16(&buf);
271
272 dprintf(avctx, "Video Dimensions %dx%d\n",
273 ctx->presentation.video_w, ctx->presentation.video_h);
274
275 /* Skip 1 bytes of unknown, frame rate? */
276 buf++;
277
278 ctx->presentation.id_number = bytestream_get_be16(&buf);
279
280 /* Next byte is the state. */
281 block = bytestream_get_byte(&buf);;
282 if (block == 0x80) {
283 /*
284 * Skip 7 bytes of unknown:
285 * palette_update_flag (0x80),
286 * palette_id_to_use,
287 * Object Number (if > 0 determines if more data to process),
288 * object_id_ref (2 bytes),
289 * window_id_ref,
290 * composition_flag (0x80 - object cropped, 0x40 - object forced)
291 */
292 buf += 7;
293
294 x = bytestream_get_be16(&buf);
295 y = bytestream_get_be16(&buf);
296
297 /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
298
299 dprintf(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
300
301 if (x > ctx->presentation.video_w || y > ctx->presentation.video_h) {
302 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
303 x, y, ctx->presentation.video_w, ctx->presentation.video_h);
304 x = 0; y = 0;
305 }
306
307 /* Fill in dimensions */
308 ctx->presentation.x = x;
309 ctx->presentation.y = y;
310 } else if (block == 0x00) {
311 /* TODO: Blank context as subtitle should not be displayed.
312 * If the subtitle is blanked now the subtitle is not
313 * on screen long enough to read, due to a delay in
314 * initial display timing.
315 */
316 }
317 }
318
319 /**
320 * Parses the display segment packet.
321 *
322 * The display segment controls the updating of the display.
323 *
324 * @param avctx contains the current codec context
325 * @param data pointer to the data pertaining the subtitle to display
326 * @param buf pointer to the packet to process
327 * @param buf_size size of packet to process
328 * @todo TODO: Fix start time, relies on correct PTS, currently too late
329 *
330 * @todo TODO: Fix end time, normally cleared by a second display
331 * @todo segment, which is currently ignored as it clears
332 * @todo the subtitle too early.
333 */
334 static int display_end_segment(AVCodecContext *avctx, void *data,
335 const uint8_t *buf, int buf_size)
336 {
337 AVSubtitle *sub = data;
338 PGSSubContext *ctx = avctx->priv_data;
339
340 /*
341 * The end display time is a timeout value and is only reached
342 * if the next subtitle is later then timeout or subtitle has
343 * not been cleared by a subsequent empty display command.
344 */
345
346 sub->start_display_time = 0;
347 sub->end_display_time = 20000;
348 sub->format = 0;
349
350 if (!sub->rects) {
351 sub->rects = av_mallocz(sizeof(*sub->rects));
352 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
353 sub->num_rects = 1;
354 }
355
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;
361
362 /* Process bitmap */
363 sub->rects[0]->pict.linesize[0] = ctx->picture.w;
364
365 if (ctx->picture.rle)
366 if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
367 return 0;
368
369 /* Allocate memory for colors */
370 sub->rects[0]->nb_colors = 256;
371 sub->rects[0]->pict.data[1] = av_malloc(sub->rects[0]->nb_colors * sizeof(uint32_t));
372
373 memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
374
375 return 1;
376 }
377
378 static int decode(AVCodecContext *avctx, void *data, int *data_size,
379 AVPacket *avpkt)
380 {
381 const uint8_t *buf = avpkt->data;
382 int buf_size = avpkt->size;
383
384 const uint8_t *buf_end;
385 uint8_t segment_type;
386 int segment_length;
387
388 #ifdef DEBUG_PACKET_CONTENTS
389 int i;
390
391 av_log(avctx, AV_LOG_INFO, "PGS sub packet:\n");
392
393 for (i = 0; i < buf_size; i++) {
394 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
395 if (i % 16 == 15)
396 av_log(avctx, AV_LOG_INFO, "\n");
397 }
398
399 if (i & 15)
400 av_log(avctx, AV_LOG_INFO, "\n");
401 #endif
402
403 *data_size = 0;
404
405 /* Ensure that we have received at a least a segment code and segment length */
406 if (buf_size < 3)
407 return -1;
408
409 buf_end = buf + buf_size;
410
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);
415
416 dprintf(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
417
418 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
419 break;
420
421 switch (segment_type) {
422 case PALETTE_SEGMENT:
423 parse_palette_segment(avctx, buf, segment_length);
424 break;
425 case PICTURE_SEGMENT:
426 parse_picture_segment(avctx, buf, segment_length);
427 break;
428 case PRESENTATION_SEGMENT:
429 parse_presentation_segment(avctx, buf, segment_length);
430 break;
431 case WINDOW_SEGMENT:
432 /*
433 * Window Segment Structure (No new information provided):
434 * 2 bytes: Unkown,
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.
439 */
440 break;
441 case DISPLAY_SEGMENT:
442 *data_size = display_end_segment(avctx, data, buf, segment_length);
443 break;
444 default:
445 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
446 segment_type, segment_length);
447 break;
448 }
449
450 buf += segment_length;
451 }
452
453 return buf_size;
454 }
455
456 AVCodec pgssub_decoder = {
457 "pgssub",
458 CODEC_TYPE_SUBTITLE,
459 CODEC_ID_HDMV_PGS_SUBTITLE,
460 sizeof(PGSSubContext),
461 init_decoder,
462 NULL,
463 close_decoder,
464 decode,
465 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
466 };