00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "dsputil.h"
00029 #include "bytestream.h"
00030 #include "libavutil/colorspace.h"
00031 #include "libavutil/imgutils.h"
00032 #include "libavutil/opt.h"
00033
00034 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
00035
00036 enum SegmentType {
00037 PALETTE_SEGMENT = 0x14,
00038 PICTURE_SEGMENT = 0x15,
00039 PRESENTATION_SEGMENT = 0x16,
00040 WINDOW_SEGMENT = 0x17,
00041 DISPLAY_SEGMENT = 0x80,
00042 };
00043
00044 typedef struct PGSSubPictureReference {
00045 int x;
00046 int y;
00047 int picture_id;
00048 int composition;
00049 } PGSSubPictureReference;
00050
00051 typedef struct PGSSubPresentation {
00052 int id_number;
00053 int object_count;
00054 PGSSubPictureReference *objects;
00055 } PGSSubPresentation;
00056
00057 typedef struct PGSSubPicture {
00058 int w;
00059 int h;
00060 uint8_t *rle;
00061 unsigned int rle_buffer_size, rle_data_len;
00062 unsigned int rle_remaining_len;
00063 } PGSSubPicture;
00064
00065 typedef struct PGSSubContext {
00066 AVClass *class;
00067 PGSSubPresentation presentation;
00068 uint32_t clut[256];
00069 PGSSubPicture pictures[UINT16_MAX];
00070 int64_t pts;
00071 int forced_subs_only;
00072 } PGSSubContext;
00073
00074 static av_cold int init_decoder(AVCodecContext *avctx)
00075 {
00076 avctx->pix_fmt = PIX_FMT_PAL8;
00077
00078 return 0;
00079 }
00080
00081 static av_cold int close_decoder(AVCodecContext *avctx)
00082 {
00083 uint16_t picture;
00084
00085 PGSSubContext *ctx = avctx->priv_data;
00086
00087 av_freep(&ctx->presentation.objects);
00088 ctx->presentation.object_count = 0;
00089
00090 for (picture = 0; picture < UINT16_MAX; ++picture) {
00091 av_freep(&ctx->pictures[picture].rle);
00092 ctx->pictures[picture].rle_buffer_size = 0;
00093 }
00094
00095 return 0;
00096 }
00097
00108 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub, int rect,
00109 const uint8_t *buf, unsigned int buf_size)
00110 {
00111 const uint8_t *rle_bitmap_end;
00112 int pixel_count, line_count;
00113
00114 rle_bitmap_end = buf + buf_size;
00115
00116 sub->rects[rect]->pict.data[0] = av_malloc(sub->rects[rect]->w * sub->rects[rect]->h);
00117
00118 if (!sub->rects[rect]->pict.data[0])
00119 return -1;
00120
00121 pixel_count = 0;
00122 line_count = 0;
00123
00124 while (buf < rle_bitmap_end && line_count < sub->rects[rect]->h) {
00125 uint8_t flags, color;
00126 int run;
00127
00128 color = bytestream_get_byte(&buf);
00129 run = 1;
00130
00131 if (color == 0x00) {
00132 flags = bytestream_get_byte(&buf);
00133 run = flags & 0x3f;
00134 if (flags & 0x40)
00135 run = (run << 8) + bytestream_get_byte(&buf);
00136 color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
00137 }
00138
00139 if (run > 0 && pixel_count + run <= sub->rects[rect]->w * sub->rects[rect]->h) {
00140 memset(sub->rects[rect]->pict.data[0] + pixel_count, color, run);
00141 pixel_count += run;
00142 } else if (!run) {
00143
00144
00145
00146
00147 if (pixel_count % sub->rects[rect]->w > 0)
00148 av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
00149 pixel_count % sub->rects[rect]->w, sub->rects[rect]->w);
00150 line_count++;
00151 }
00152 }
00153
00154 if (pixel_count < sub->rects[rect]->w * sub->rects[rect]->h) {
00155 av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
00156 return -1;
00157 }
00158
00159 av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[rect]->w * sub->rects[rect]->h);
00160
00161 return 0;
00162 }
00163
00175 static int parse_picture_segment(AVCodecContext *avctx,
00176 const uint8_t *buf, int buf_size)
00177 {
00178 PGSSubContext *ctx = avctx->priv_data;
00179
00180 uint8_t sequence_desc;
00181 unsigned int rle_bitmap_len, width, height;
00182 uint16_t picture_id;
00183
00184 if (buf_size <= 4)
00185 return -1;
00186 buf_size -= 4;
00187
00188 picture_id = bytestream_get_be16(&buf);
00189
00190
00191 buf++;
00192
00193
00194 sequence_desc = bytestream_get_byte(&buf);
00195
00196 if (!(sequence_desc & 0x80)) {
00197
00198 if (buf_size > ctx->pictures[picture_id].rle_remaining_len)
00199 return -1;
00200
00201 memcpy(ctx->pictures[picture_id].rle + ctx->pictures[picture_id].rle_data_len, buf, buf_size);
00202 ctx->pictures[picture_id].rle_data_len += buf_size;
00203 ctx->pictures[picture_id].rle_remaining_len -= buf_size;
00204
00205 return 0;
00206 }
00207
00208 if (buf_size <= 7)
00209 return -1;
00210 buf_size -= 7;
00211
00212
00213 rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
00214
00215
00216 width = bytestream_get_be16(&buf);
00217 height = bytestream_get_be16(&buf);
00218
00219
00220 if (avctx->width < width || avctx->height < height) {
00221 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
00222 return -1;
00223 }
00224
00225 ctx->pictures[picture_id].w = width;
00226 ctx->pictures[picture_id].h = height;
00227
00228 av_fast_malloc(&ctx->pictures[picture_id].rle, &ctx->pictures[picture_id].rle_buffer_size, rle_bitmap_len);
00229
00230 if (!ctx->pictures[picture_id].rle)
00231 return -1;
00232
00233 memcpy(ctx->pictures[picture_id].rle, buf, buf_size);
00234 ctx->pictures[picture_id].rle_data_len = buf_size;
00235 ctx->pictures[picture_id].rle_remaining_len = rle_bitmap_len - buf_size;
00236
00237 return 0;
00238 }
00239
00250 static void parse_palette_segment(AVCodecContext *avctx,
00251 const uint8_t *buf, int buf_size)
00252 {
00253 PGSSubContext *ctx = avctx->priv_data;
00254
00255 const uint8_t *buf_end = buf + buf_size;
00256 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00257 int color_id;
00258 int y, cb, cr, alpha;
00259 int r, g, b, r_add, g_add, b_add;
00260
00261
00262 buf += 2;
00263
00264 while (buf < buf_end) {
00265 color_id = bytestream_get_byte(&buf);
00266 y = bytestream_get_byte(&buf);
00267 cr = bytestream_get_byte(&buf);
00268 cb = bytestream_get_byte(&buf);
00269 alpha = bytestream_get_byte(&buf);
00270
00271 YUV_TO_RGB1(cb, cr);
00272 YUV_TO_RGB2(r, g, b, y);
00273
00274 av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
00275
00276
00277 ctx->clut[color_id] = RGBA(r,g,b,alpha);
00278 }
00279 }
00280
00292 static void parse_presentation_segment(AVCodecContext *avctx,
00293 const uint8_t *buf, int buf_size)
00294 {
00295 PGSSubContext *ctx = avctx->priv_data;
00296
00297 int w = bytestream_get_be16(&buf);
00298 int h = bytestream_get_be16(&buf);
00299
00300 uint16_t object_index;
00301
00302 av_dlog(avctx, "Video Dimensions %dx%d\n",
00303 w, h);
00304 if (av_image_check_size(w, h, 0, avctx) >= 0)
00305 avcodec_set_dimensions(avctx, w, h);
00306
00307
00308 buf++;
00309
00310 ctx->presentation.id_number = bytestream_get_be16(&buf);
00311
00312
00313
00314
00315
00316
00317
00318 buf += 3;
00319
00320 ctx->presentation.object_count = bytestream_get_byte(&buf);
00321 if (!ctx->presentation.object_count)
00322 return;
00323
00324
00325 buf_size -= 11;
00326 if (buf_size < ctx->presentation.object_count * 8) {
00327 ctx->presentation.object_count = 0;
00328 return;
00329 }
00330
00331 av_freep(&ctx->presentation.objects);
00332 ctx->presentation.objects = av_malloc(sizeof(PGSSubPictureReference) * ctx->presentation.object_count);
00333 if (!ctx->presentation.objects) {
00334 ctx->presentation.object_count = 0;
00335 return;
00336 }
00337
00338 for (object_index = 0; object_index < ctx->presentation.object_count; ++object_index) {
00339 PGSSubPictureReference *reference = &ctx->presentation.objects[object_index];
00340 reference->picture_id = bytestream_get_be16(&buf);
00341
00342
00343 buf++;
00344
00345 reference->composition = bytestream_get_byte(&buf);
00346
00347 reference->x = bytestream_get_be16(&buf);
00348 reference->y = bytestream_get_be16(&buf);
00349
00350
00351 av_dlog(avctx, "Subtitle Placement ID=%d, x=%d, y=%d\n", reference->picture_id, reference->x, reference->y);
00352
00353 if (reference->x > avctx->width || reference->y > avctx->height) {
00354 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
00355 reference->x, reference->y, avctx->width, avctx->height);
00356 reference->x = 0;
00357 reference->y = 0;
00358 }
00359 }
00360 }
00361
00377 static int display_end_segment(AVCodecContext *avctx, void *data,
00378 const uint8_t *buf, int buf_size)
00379 {
00380 AVSubtitle *sub = data;
00381 PGSSubContext *ctx = avctx->priv_data;
00382 int64_t pts;
00383
00384 uint16_t rect;
00385
00386
00387
00388
00389
00390
00391
00392 pts = ctx->pts != AV_NOPTS_VALUE ? ctx->pts : sub->pts;
00393 memset(sub, 0, sizeof(*sub));
00394 sub->pts = pts;
00395 ctx->pts = AV_NOPTS_VALUE;
00396
00397
00398 if (!ctx->presentation.object_count)
00399 return 1;
00400
00401 sub->start_display_time = 0;
00402 sub->end_display_time = 20000;
00403 sub->format = 0;
00404
00405 sub->num_rects = ctx->presentation.object_count;
00406 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
00407
00408 for (rect = 0; rect < sub->num_rects; ++rect) {
00409 uint16_t picture_id = ctx->presentation.objects[rect].picture_id;
00410 sub->rects[rect] = av_mallocz(sizeof(*sub->rects[rect]));
00411 sub->rects[rect]->x = ctx->presentation.objects[rect].x;
00412 sub->rects[rect]->y = ctx->presentation.objects[rect].y;
00413 sub->rects[rect]->w = ctx->pictures[picture_id].w;
00414 sub->rects[rect]->h = ctx->pictures[picture_id].h;
00415 sub->rects[rect]->type = SUBTITLE_BITMAP;
00416
00417
00418 sub->rects[rect]->pict.linesize[0] = ctx->pictures[picture_id].w;
00419 if (ctx->pictures[picture_id].rle) {
00420 if (ctx->pictures[picture_id].rle_remaining_len)
00421 av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
00422 ctx->pictures[picture_id].rle_data_len, ctx->pictures[picture_id].rle_remaining_len);
00423 if (decode_rle(avctx, sub, rect, ctx->pictures[picture_id].rle, ctx->pictures[picture_id].rle_data_len) < 0)
00424 return 0;
00425 }
00426
00427
00428 sub->rects[rect]->nb_colors = 256;
00429 sub->rects[rect]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00430
00431
00432 sub->rects[rect]->forced = (ctx->presentation.objects[rect].composition & 0x40) != 0;
00433
00434 if (!ctx->forced_subs_only || ctx->presentation.objects[rect].composition & 0x40)
00435 memcpy(sub->rects[rect]->pict.data[1], ctx->clut, sub->rects[rect]->nb_colors * sizeof(uint32_t));
00436 }
00437
00438 return 1;
00439 }
00440
00441 static int decode(AVCodecContext *avctx, void *data, int *data_size,
00442 AVPacket *avpkt)
00443 {
00444 PGSSubContext *ctx = avctx->priv_data;
00445 const uint8_t *buf = avpkt->data;
00446 int buf_size = avpkt->size;
00447 AVSubtitle *sub = data;
00448
00449 const uint8_t *buf_end;
00450 uint8_t segment_type;
00451 int segment_length;
00452 int i;
00453
00454 av_dlog(avctx, "PGS sub packet:\n");
00455
00456 for (i = 0; i < buf_size; i++) {
00457 av_dlog(avctx, "%02x ", buf[i]);
00458 if (i % 16 == 15)
00459 av_dlog(avctx, "\n");
00460 }
00461
00462 if (i & 15)
00463 av_dlog(avctx, "\n");
00464
00465 *data_size = 0;
00466
00467
00468 if (buf_size < 3)
00469 return -1;
00470
00471 buf_end = buf + buf_size;
00472
00473
00474 while (buf < buf_end) {
00475 segment_type = bytestream_get_byte(&buf);
00476 segment_length = bytestream_get_be16(&buf);
00477
00478 av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
00479
00480 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
00481 break;
00482
00483 switch (segment_type) {
00484 case PALETTE_SEGMENT:
00485 parse_palette_segment(avctx, buf, segment_length);
00486 break;
00487 case PICTURE_SEGMENT:
00488 parse_picture_segment(avctx, buf, segment_length);
00489 break;
00490 case PRESENTATION_SEGMENT:
00491 parse_presentation_segment(avctx, buf, segment_length);
00492 ctx->pts = sub->pts;
00493 break;
00494 case WINDOW_SEGMENT:
00495
00496
00497
00498
00499
00500
00501
00502
00503 break;
00504 case DISPLAY_SEGMENT:
00505 *data_size = display_end_segment(avctx, data, buf, segment_length);
00506 break;
00507 default:
00508 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
00509 segment_type, segment_length);
00510 break;
00511 }
00512
00513 buf += segment_length;
00514 }
00515
00516 return buf_size;
00517 }
00518
00519 #define OFFSET(x) offsetof(PGSSubContext, x)
00520 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
00521 static const AVOption options[] = {
00522 {"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
00523 { NULL },
00524 };
00525
00526 static const AVClass pgsdec_class = {
00527 .class_name = "PGS subtitle decoder",
00528 .item_name = av_default_item_name,
00529 .option = options,
00530 .version = LIBAVUTIL_VERSION_INT,
00531 };
00532
00533 AVCodec ff_pgssub_decoder = {
00534 .name = "pgssub",
00535 .type = AVMEDIA_TYPE_SUBTITLE,
00536 .id = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
00537 .priv_data_size = sizeof(PGSSubContext),
00538 .init = init_decoder,
00539 .close = close_decoder,
00540 .decode = decode,
00541 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
00542 .priv_class = &pgsdec_class,
00543 };