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 int64_t pts;
00056 } PGSSubPresentation;
00057
00058 typedef struct PGSSubPicture {
00059 int w;
00060 int h;
00061 uint8_t *rle;
00062 unsigned int rle_buffer_size, rle_data_len;
00063 unsigned int rle_remaining_len;
00064 } PGSSubPicture;
00065
00066 typedef struct PGSSubContext {
00067 AVClass *class;
00068 PGSSubPresentation presentation;
00069 uint32_t clut[256];
00070 PGSSubPicture pictures[UINT16_MAX];
00071 int forced_subs_only;
00072 } PGSSubContext;
00073
00074 static av_cold int init_decoder(AVCodecContext *avctx)
00075 {
00076 avctx->pix_fmt = AV_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 if (buf_size > rle_bitmap_len) {
00226 av_log(avctx, AV_LOG_ERROR, "too much RLE data\n");
00227 return AVERROR_INVALIDDATA;
00228 }
00229
00230 ctx->pictures[picture_id].w = width;
00231 ctx->pictures[picture_id].h = height;
00232
00233 av_fast_malloc(&ctx->pictures[picture_id].rle, &ctx->pictures[picture_id].rle_buffer_size, rle_bitmap_len);
00234
00235 if (!ctx->pictures[picture_id].rle)
00236 return -1;
00237
00238 memcpy(ctx->pictures[picture_id].rle, buf, buf_size);
00239 ctx->pictures[picture_id].rle_data_len = buf_size;
00240 ctx->pictures[picture_id].rle_remaining_len = rle_bitmap_len - buf_size;
00241
00242 return 0;
00243 }
00244
00255 static void parse_palette_segment(AVCodecContext *avctx,
00256 const uint8_t *buf, int buf_size)
00257 {
00258 PGSSubContext *ctx = avctx->priv_data;
00259
00260 const uint8_t *buf_end = buf + buf_size;
00261 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00262 int color_id;
00263 int y, cb, cr, alpha;
00264 int r, g, b, r_add, g_add, b_add;
00265
00266
00267 buf += 2;
00268
00269 while (buf < buf_end) {
00270 color_id = bytestream_get_byte(&buf);
00271 y = bytestream_get_byte(&buf);
00272 cr = bytestream_get_byte(&buf);
00273 cb = bytestream_get_byte(&buf);
00274 alpha = bytestream_get_byte(&buf);
00275
00276 YUV_TO_RGB1(cb, cr);
00277 YUV_TO_RGB2(r, g, b, y);
00278
00279 av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
00280
00281
00282 ctx->clut[color_id] = RGBA(r,g,b,alpha);
00283 }
00284 }
00285
00297 static void parse_presentation_segment(AVCodecContext *avctx,
00298 const uint8_t *buf, int buf_size,
00299 int64_t pts)
00300 {
00301 PGSSubContext *ctx = avctx->priv_data;
00302
00303 int w = bytestream_get_be16(&buf);
00304 int h = bytestream_get_be16(&buf);
00305
00306 uint16_t object_index;
00307
00308 ctx->presentation.pts = pts;
00309
00310 av_dlog(avctx, "Video Dimensions %dx%d\n",
00311 w, h);
00312 if (av_image_check_size(w, h, 0, avctx) >= 0)
00313 avcodec_set_dimensions(avctx, w, h);
00314
00315
00316 buf++;
00317
00318 ctx->presentation.id_number = bytestream_get_be16(&buf);
00319
00320
00321
00322
00323
00324
00325
00326 buf += 3;
00327
00328 ctx->presentation.object_count = bytestream_get_byte(&buf);
00329 if (!ctx->presentation.object_count)
00330 return;
00331
00332
00333 buf_size -= 11;
00334 if (buf_size < ctx->presentation.object_count * 8) {
00335 ctx->presentation.object_count = 0;
00336 return;
00337 }
00338
00339 av_freep(&ctx->presentation.objects);
00340 ctx->presentation.objects = av_malloc(sizeof(PGSSubPictureReference) * ctx->presentation.object_count);
00341 if (!ctx->presentation.objects) {
00342 ctx->presentation.object_count = 0;
00343 return;
00344 }
00345
00346 for (object_index = 0; object_index < ctx->presentation.object_count; ++object_index) {
00347 PGSSubPictureReference *reference = &ctx->presentation.objects[object_index];
00348 reference->picture_id = bytestream_get_be16(&buf);
00349
00350
00351 buf++;
00352
00353 reference->composition = bytestream_get_byte(&buf);
00354
00355 reference->x = bytestream_get_be16(&buf);
00356 reference->y = bytestream_get_be16(&buf);
00357
00358
00359 av_dlog(avctx, "Subtitle Placement ID=%d, x=%d, y=%d\n", reference->picture_id, reference->x, reference->y);
00360
00361 if (reference->x > avctx->width || reference->y > avctx->height) {
00362 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
00363 reference->x, reference->y, avctx->width, avctx->height);
00364 reference->x = 0;
00365 reference->y = 0;
00366 }
00367 }
00368 }
00369
00385 static int display_end_segment(AVCodecContext *avctx, void *data,
00386 const uint8_t *buf, int buf_size)
00387 {
00388 AVSubtitle *sub = data;
00389 PGSSubContext *ctx = avctx->priv_data;
00390 int64_t pts;
00391
00392 uint16_t rect;
00393
00394
00395
00396
00397
00398
00399
00400 pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts;
00401 memset(sub, 0, sizeof(*sub));
00402 sub->pts = pts;
00403 ctx->presentation.pts = AV_NOPTS_VALUE;
00404
00405
00406 if (!ctx->presentation.object_count)
00407 return 1;
00408
00409 sub->start_display_time = 0;
00410 sub->end_display_time = 20000;
00411 sub->format = 0;
00412
00413 sub->num_rects = ctx->presentation.object_count;
00414 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
00415
00416 for (rect = 0; rect < sub->num_rects; ++rect) {
00417 uint16_t picture_id = ctx->presentation.objects[rect].picture_id;
00418 sub->rects[rect] = av_mallocz(sizeof(*sub->rects[rect]));
00419 sub->rects[rect]->x = ctx->presentation.objects[rect].x;
00420 sub->rects[rect]->y = ctx->presentation.objects[rect].y;
00421 sub->rects[rect]->w = ctx->pictures[picture_id].w;
00422 sub->rects[rect]->h = ctx->pictures[picture_id].h;
00423 sub->rects[rect]->type = SUBTITLE_BITMAP;
00424
00425
00426 sub->rects[rect]->pict.linesize[0] = ctx->pictures[picture_id].w;
00427 if (ctx->pictures[picture_id].rle) {
00428 if (ctx->pictures[picture_id].rle_remaining_len)
00429 av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
00430 ctx->pictures[picture_id].rle_data_len, ctx->pictures[picture_id].rle_remaining_len);
00431 if (decode_rle(avctx, sub, rect, ctx->pictures[picture_id].rle, ctx->pictures[picture_id].rle_data_len) < 0)
00432 return 0;
00433 }
00434
00435
00436 sub->rects[rect]->nb_colors = 256;
00437 sub->rects[rect]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00438
00439
00440 sub->rects[rect]->flags = (ctx->presentation.objects[rect].composition & 0x40) != 0 ? AV_SUBTITLE_FLAG_FORCED : 0;
00441
00442 if (!ctx->forced_subs_only || ctx->presentation.objects[rect].composition & 0x40)
00443 memcpy(sub->rects[rect]->pict.data[1], ctx->clut, sub->rects[rect]->nb_colors * sizeof(uint32_t));
00444 }
00445
00446 return 1;
00447 }
00448
00449 static int decode(AVCodecContext *avctx, void *data, int *data_size,
00450 AVPacket *avpkt)
00451 {
00452 const uint8_t *buf = avpkt->data;
00453 int buf_size = avpkt->size;
00454 AVSubtitle *sub = data;
00455
00456 const uint8_t *buf_end;
00457 uint8_t segment_type;
00458 int segment_length;
00459 int i;
00460
00461 av_dlog(avctx, "PGS sub packet:\n");
00462
00463 for (i = 0; i < buf_size; i++) {
00464 av_dlog(avctx, "%02x ", buf[i]);
00465 if (i % 16 == 15)
00466 av_dlog(avctx, "\n");
00467 }
00468
00469 if (i & 15)
00470 av_dlog(avctx, "\n");
00471
00472 *data_size = 0;
00473
00474
00475 if (buf_size < 3)
00476 return -1;
00477
00478 buf_end = buf + buf_size;
00479
00480
00481 while (buf < buf_end) {
00482 segment_type = bytestream_get_byte(&buf);
00483 segment_length = bytestream_get_be16(&buf);
00484
00485 av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
00486
00487 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
00488 break;
00489
00490 switch (segment_type) {
00491 case PALETTE_SEGMENT:
00492 parse_palette_segment(avctx, buf, segment_length);
00493 break;
00494 case PICTURE_SEGMENT:
00495 parse_picture_segment(avctx, buf, segment_length);
00496 break;
00497 case PRESENTATION_SEGMENT:
00498 parse_presentation_segment(avctx, buf, segment_length, sub->pts);
00499 break;
00500 case WINDOW_SEGMENT:
00501
00502
00503
00504
00505
00506
00507
00508
00509 break;
00510 case DISPLAY_SEGMENT:
00511 *data_size = display_end_segment(avctx, data, buf, segment_length);
00512 break;
00513 default:
00514 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
00515 segment_type, segment_length);
00516 break;
00517 }
00518
00519 buf += segment_length;
00520 }
00521
00522 return buf_size;
00523 }
00524
00525 #define OFFSET(x) offsetof(PGSSubContext, x)
00526 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
00527 static const AVOption options[] = {
00528 {"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
00529 { NULL },
00530 };
00531
00532 static const AVClass pgsdec_class = {
00533 .class_name = "PGS subtitle decoder",
00534 .item_name = av_default_item_name,
00535 .option = options,
00536 .version = LIBAVUTIL_VERSION_INT,
00537 };
00538
00539 AVCodec ff_pgssub_decoder = {
00540 .name = "pgssub",
00541 .type = AVMEDIA_TYPE_SUBTITLE,
00542 .id = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
00543 .priv_data_size = sizeof(PGSSubContext),
00544 .init = init_decoder,
00545 .close = close_decoder,
00546 .decode = decode,
00547 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
00548 .priv_class = &pgsdec_class,
00549 };