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
00033 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
00034
00035 enum SegmentType {
00036 PALETTE_SEGMENT = 0x14,
00037 PICTURE_SEGMENT = 0x15,
00038 PRESENTATION_SEGMENT = 0x16,
00039 WINDOW_SEGMENT = 0x17,
00040 DISPLAY_SEGMENT = 0x80,
00041 };
00042
00043 typedef struct PGSSubPresentation {
00044 int x;
00045 int y;
00046 int id_number;
00047 int object_number;
00048 } PGSSubPresentation;
00049
00050 typedef struct PGSSubPicture {
00051 int w;
00052 int h;
00053 uint8_t *rle;
00054 unsigned int rle_buffer_size, rle_data_len;
00055 unsigned int rle_remaining_len;
00056 } PGSSubPicture;
00057
00058 typedef struct PGSSubContext {
00059 PGSSubPresentation presentation;
00060 uint32_t clut[256];
00061 PGSSubPicture picture;
00062 } PGSSubContext;
00063
00064 static av_cold int init_decoder(AVCodecContext *avctx)
00065 {
00066 avctx->pix_fmt = PIX_FMT_PAL8;
00067
00068 return 0;
00069 }
00070
00071 static av_cold int close_decoder(AVCodecContext *avctx)
00072 {
00073 PGSSubContext *ctx = avctx->priv_data;
00074
00075 av_freep(&ctx->picture.rle);
00076 ctx->picture.rle_buffer_size = 0;
00077
00078 return 0;
00079 }
00080
00091 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
00092 const uint8_t *buf, unsigned int buf_size)
00093 {
00094 const uint8_t *rle_bitmap_end;
00095 int pixel_count, line_count;
00096
00097 rle_bitmap_end = buf + buf_size;
00098
00099 sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h);
00100
00101 if (!sub->rects[0]->pict.data[0])
00102 return -1;
00103
00104 pixel_count = 0;
00105 line_count = 0;
00106
00107 while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
00108 uint8_t flags, color;
00109 int run;
00110
00111 color = bytestream_get_byte(&buf);
00112 run = 1;
00113
00114 if (color == 0x00) {
00115 flags = bytestream_get_byte(&buf);
00116 run = flags & 0x3f;
00117 if (flags & 0x40)
00118 run = (run << 8) + bytestream_get_byte(&buf);
00119 color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
00120 }
00121
00122 if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) {
00123 memset(sub->rects[0]->pict.data[0] + pixel_count, color, run);
00124 pixel_count += run;
00125 } else if (!run) {
00126
00127
00128
00129
00130 if (pixel_count % sub->rects[0]->w > 0)
00131 av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
00132 pixel_count % sub->rects[0]->w, sub->rects[0]->w);
00133 line_count++;
00134 }
00135 }
00136
00137 if (pixel_count < sub->rects[0]->w * sub->rects[0]->h) {
00138 av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
00139 return -1;
00140 }
00141
00142 av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
00143
00144 return 0;
00145 }
00146
00158 static int parse_picture_segment(AVCodecContext *avctx,
00159 const uint8_t *buf, int buf_size)
00160 {
00161 PGSSubContext *ctx = avctx->priv_data;
00162
00163 uint8_t sequence_desc;
00164 unsigned int rle_bitmap_len, width, height;
00165
00166 if (buf_size <= 4)
00167 return -1;
00168 buf_size -= 4;
00169
00170
00171 buf += 3;
00172
00173
00174 sequence_desc = bytestream_get_byte(&buf);
00175
00176 if (!(sequence_desc & 0x80)) {
00177
00178 if (buf_size > ctx->picture.rle_remaining_len)
00179 return -1;
00180
00181 memcpy(ctx->picture.rle + ctx->picture.rle_data_len, buf, buf_size);
00182 ctx->picture.rle_data_len += buf_size;
00183 ctx->picture.rle_remaining_len -= buf_size;
00184
00185 return 0;
00186 }
00187
00188 if (buf_size <= 7)
00189 return -1;
00190 buf_size -= 7;
00191
00192
00193 rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
00194
00195 if (buf_size > rle_bitmap_len) {
00196 av_log(avctx, AV_LOG_ERROR,
00197 "Buffer dimension %d larger than the expected RLE data %d\n",
00198 buf_size, rle_bitmap_len);
00199 return AVERROR_INVALIDDATA;
00200 }
00201
00202
00203 width = bytestream_get_be16(&buf);
00204 height = bytestream_get_be16(&buf);
00205
00206
00207 if (avctx->width < width || avctx->height < height) {
00208 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
00209 return -1;
00210 }
00211
00212 ctx->picture.w = width;
00213 ctx->picture.h = height;
00214
00215 av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
00216
00217 if (!ctx->picture.rle)
00218 return -1;
00219
00220 memcpy(ctx->picture.rle, buf, buf_size);
00221 ctx->picture.rle_data_len = buf_size;
00222 ctx->picture.rle_remaining_len = rle_bitmap_len - buf_size;
00223
00224 return 0;
00225 }
00226
00237 static void parse_palette_segment(AVCodecContext *avctx,
00238 const uint8_t *buf, int buf_size)
00239 {
00240 PGSSubContext *ctx = avctx->priv_data;
00241
00242 const uint8_t *buf_end = buf + buf_size;
00243 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00244 int color_id;
00245 int y, cb, cr, alpha;
00246 int r, g, b, r_add, g_add, b_add;
00247
00248
00249 buf += 2;
00250
00251 while (buf < buf_end) {
00252 color_id = bytestream_get_byte(&buf);
00253 y = bytestream_get_byte(&buf);
00254 cr = bytestream_get_byte(&buf);
00255 cb = bytestream_get_byte(&buf);
00256 alpha = bytestream_get_byte(&buf);
00257
00258 YUV_TO_RGB1(cb, cr);
00259 YUV_TO_RGB2(r, g, b, y);
00260
00261 av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
00262
00263
00264 ctx->clut[color_id] = RGBA(r,g,b,alpha);
00265 }
00266 }
00267
00280 static void parse_presentation_segment(AVCodecContext *avctx,
00281 const uint8_t *buf, int buf_size)
00282 {
00283 PGSSubContext *ctx = avctx->priv_data;
00284
00285 int x, y;
00286
00287 int w = bytestream_get_be16(&buf);
00288 int h = bytestream_get_be16(&buf);
00289
00290 av_dlog(avctx, "Video Dimensions %dx%d\n",
00291 w, h);
00292 if (av_image_check_size(w, h, 0, avctx) >= 0)
00293 avcodec_set_dimensions(avctx, w, h);
00294
00295
00296 buf++;
00297
00298 ctx->presentation.id_number = bytestream_get_be16(&buf);
00299
00300
00301
00302
00303
00304
00305
00306 buf += 3;
00307
00308 ctx->presentation.object_number = bytestream_get_byte(&buf);
00309 if (!ctx->presentation.object_number)
00310 return;
00311
00312
00313
00314
00315
00316
00317
00318 buf += 4;
00319
00320 x = bytestream_get_be16(&buf);
00321 y = bytestream_get_be16(&buf);
00322
00323
00324
00325 av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
00326
00327 if (x > avctx->width || y > avctx->height) {
00328 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
00329 x, y, avctx->width, avctx->height);
00330 x = 0; y = 0;
00331 }
00332
00333
00334 ctx->presentation.x = x;
00335 ctx->presentation.y = y;
00336 }
00337
00353 static int display_end_segment(AVCodecContext *avctx, void *data,
00354 const uint8_t *buf, int buf_size)
00355 {
00356 AVSubtitle *sub = data;
00357 PGSSubContext *ctx = avctx->priv_data;
00358
00359
00360
00361
00362
00363
00364
00365 memset(sub, 0, sizeof(*sub));
00366
00367
00368 if (!ctx->presentation.object_number)
00369 return 1;
00370 sub->start_display_time = 0;
00371 sub->end_display_time = 20000;
00372 sub->format = 0;
00373
00374 sub->rects = av_mallocz(sizeof(*sub->rects));
00375 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
00376 sub->num_rects = 1;
00377
00378 sub->rects[0]->x = ctx->presentation.x;
00379 sub->rects[0]->y = ctx->presentation.y;
00380 sub->rects[0]->w = ctx->picture.w;
00381 sub->rects[0]->h = ctx->picture.h;
00382 sub->rects[0]->type = SUBTITLE_BITMAP;
00383
00384
00385 sub->rects[0]->pict.linesize[0] = ctx->picture.w;
00386
00387 if (ctx->picture.rle) {
00388 if (ctx->picture.rle_remaining_len)
00389 av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
00390 ctx->picture.rle_data_len, ctx->picture.rle_remaining_len);
00391 if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
00392 return 0;
00393 }
00394
00395 sub->rects[0]->nb_colors = 256;
00396 sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00397
00398 memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
00399
00400 return 1;
00401 }
00402
00403 static int decode(AVCodecContext *avctx, void *data, int *data_size,
00404 AVPacket *avpkt)
00405 {
00406 const uint8_t *buf = avpkt->data;
00407 int buf_size = avpkt->size;
00408
00409 const uint8_t *buf_end;
00410 uint8_t segment_type;
00411 int segment_length;
00412 int i;
00413
00414 av_dlog(avctx, "PGS sub packet:\n");
00415
00416 for (i = 0; i < buf_size; i++) {
00417 av_dlog(avctx, "%02x ", buf[i]);
00418 if (i % 16 == 15)
00419 av_dlog(avctx, "\n");
00420 }
00421
00422 if (i & 15)
00423 av_dlog(avctx, "\n");
00424
00425 *data_size = 0;
00426
00427
00428 if (buf_size < 3)
00429 return -1;
00430
00431 buf_end = buf + buf_size;
00432
00433
00434 while (buf < buf_end) {
00435 segment_type = bytestream_get_byte(&buf);
00436 segment_length = bytestream_get_be16(&buf);
00437
00438 av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
00439
00440 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
00441 break;
00442
00443 switch (segment_type) {
00444 case PALETTE_SEGMENT:
00445 parse_palette_segment(avctx, buf, segment_length);
00446 break;
00447 case PICTURE_SEGMENT:
00448 parse_picture_segment(avctx, buf, segment_length);
00449 break;
00450 case PRESENTATION_SEGMENT:
00451 parse_presentation_segment(avctx, buf, segment_length);
00452 break;
00453 case WINDOW_SEGMENT:
00454
00455
00456
00457
00458
00459
00460
00461
00462 break;
00463 case DISPLAY_SEGMENT:
00464 *data_size = display_end_segment(avctx, data, buf, segment_length);
00465 break;
00466 default:
00467 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
00468 segment_type, segment_length);
00469 break;
00470 }
00471
00472 buf += segment_length;
00473 }
00474
00475 return buf_size;
00476 }
00477
00478 AVCodec ff_pgssub_decoder = {
00479 .name = "pgssub",
00480 .type = AVMEDIA_TYPE_SUBTITLE,
00481 .id = CODEC_ID_HDMV_PGS_SUBTITLE,
00482 .priv_data_size = sizeof(PGSSubContext),
00483 .init = init_decoder,
00484 .close = close_decoder,
00485 .decode = decode,
00486 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
00487 };