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
00196 width = bytestream_get_be16(&buf);
00197 height = bytestream_get_be16(&buf);
00198
00199
00200 if (avctx->width < width || avctx->height < height) {
00201 av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
00202 return -1;
00203 }
00204
00205 ctx->picture.w = width;
00206 ctx->picture.h = height;
00207
00208 av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
00209
00210 if (!ctx->picture.rle)
00211 return -1;
00212
00213 memcpy(ctx->picture.rle, buf, buf_size);
00214 ctx->picture.rle_data_len = buf_size;
00215 ctx->picture.rle_remaining_len = rle_bitmap_len - buf_size;
00216
00217 return 0;
00218 }
00219
00230 static void parse_palette_segment(AVCodecContext *avctx,
00231 const uint8_t *buf, int buf_size)
00232 {
00233 PGSSubContext *ctx = avctx->priv_data;
00234
00235 const uint8_t *buf_end = buf + buf_size;
00236 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00237 int color_id;
00238 int y, cb, cr, alpha;
00239 int r, g, b, r_add, g_add, b_add;
00240
00241
00242 buf += 2;
00243
00244 while (buf < buf_end) {
00245 color_id = bytestream_get_byte(&buf);
00246 y = bytestream_get_byte(&buf);
00247 cr = bytestream_get_byte(&buf);
00248 cb = bytestream_get_byte(&buf);
00249 alpha = bytestream_get_byte(&buf);
00250
00251 YUV_TO_RGB1(cb, cr);
00252 YUV_TO_RGB2(r, g, b, y);
00253
00254 av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
00255
00256
00257 ctx->clut[color_id] = RGBA(r,g,b,alpha);
00258 }
00259 }
00260
00273 static void parse_presentation_segment(AVCodecContext *avctx,
00274 const uint8_t *buf, int buf_size)
00275 {
00276 PGSSubContext *ctx = avctx->priv_data;
00277
00278 int x, y;
00279
00280 int w = bytestream_get_be16(&buf);
00281 int h = bytestream_get_be16(&buf);
00282
00283 av_dlog(avctx, "Video Dimensions %dx%d\n",
00284 w, h);
00285 if (av_image_check_size(w, h, 0, avctx) >= 0)
00286 avcodec_set_dimensions(avctx, w, h);
00287
00288
00289 buf++;
00290
00291 ctx->presentation.id_number = bytestream_get_be16(&buf);
00292
00293
00294
00295
00296
00297
00298
00299 buf += 3;
00300
00301 ctx->presentation.object_number = bytestream_get_byte(&buf);
00302 if (!ctx->presentation.object_number)
00303 return;
00304
00305
00306
00307
00308
00309
00310
00311 buf += 4;
00312
00313 x = bytestream_get_be16(&buf);
00314 y = bytestream_get_be16(&buf);
00315
00316
00317
00318 av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
00319
00320 if (x > avctx->width || y > avctx->height) {
00321 av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
00322 x, y, avctx->width, avctx->height);
00323 x = 0; y = 0;
00324 }
00325
00326
00327 ctx->presentation.x = x;
00328 ctx->presentation.y = y;
00329 }
00330
00346 static int display_end_segment(AVCodecContext *avctx, void *data,
00347 const uint8_t *buf, int buf_size)
00348 {
00349 AVSubtitle *sub = data;
00350 PGSSubContext *ctx = avctx->priv_data;
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 if (!ctx->presentation.object_number)
00361 return 1;
00362 sub->start_display_time = 0;
00363 sub->end_display_time = 20000;
00364 sub->format = 0;
00365
00366 sub->rects = av_mallocz(sizeof(*sub->rects));
00367 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
00368 sub->num_rects = 1;
00369
00370 sub->rects[0]->x = ctx->presentation.x;
00371 sub->rects[0]->y = ctx->presentation.y;
00372 sub->rects[0]->w = ctx->picture.w;
00373 sub->rects[0]->h = ctx->picture.h;
00374 sub->rects[0]->type = SUBTITLE_BITMAP;
00375
00376
00377 sub->rects[0]->pict.linesize[0] = ctx->picture.w;
00378
00379 if (ctx->picture.rle) {
00380 if (ctx->picture.rle_remaining_len)
00381 av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
00382 ctx->picture.rle_data_len, ctx->picture.rle_remaining_len);
00383 if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
00384 return 0;
00385 }
00386
00387 sub->rects[0]->nb_colors = 256;
00388 sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00389
00390 memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
00391
00392 return 1;
00393 }
00394
00395 static int decode(AVCodecContext *avctx, void *data, int *data_size,
00396 AVPacket *avpkt)
00397 {
00398 const uint8_t *buf = avpkt->data;
00399 int buf_size = avpkt->size;
00400
00401 const uint8_t *buf_end;
00402 uint8_t segment_type;
00403 int segment_length;
00404 int i;
00405
00406 av_dlog(avctx, "PGS sub packet:\n");
00407
00408 for (i = 0; i < buf_size; i++) {
00409 av_dlog(avctx, "%02x ", buf[i]);
00410 if (i % 16 == 15)
00411 av_dlog(avctx, "\n");
00412 }
00413
00414 if (i & 15)
00415 av_dlog(avctx, "\n");
00416
00417 *data_size = 0;
00418
00419
00420 if (buf_size < 3)
00421 return -1;
00422
00423 buf_end = buf + buf_size;
00424
00425
00426 while (buf < buf_end) {
00427 segment_type = bytestream_get_byte(&buf);
00428 segment_length = bytestream_get_be16(&buf);
00429
00430 av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
00431
00432 if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
00433 break;
00434
00435 switch (segment_type) {
00436 case PALETTE_SEGMENT:
00437 parse_palette_segment(avctx, buf, segment_length);
00438 break;
00439 case PICTURE_SEGMENT:
00440 parse_picture_segment(avctx, buf, segment_length);
00441 break;
00442 case PRESENTATION_SEGMENT:
00443 parse_presentation_segment(avctx, buf, segment_length);
00444 break;
00445 case WINDOW_SEGMENT:
00446
00447
00448
00449
00450
00451
00452
00453
00454 break;
00455 case DISPLAY_SEGMENT:
00456 *data_size = display_end_segment(avctx, data, buf, segment_length);
00457 break;
00458 default:
00459 av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
00460 segment_type, segment_length);
00461 break;
00462 }
00463
00464 buf += segment_length;
00465 }
00466
00467 return buf_size;
00468 }
00469
00470 AVCodec ff_pgssub_decoder = {
00471 .name = "pgssub",
00472 .type = AVMEDIA_TYPE_SUBTITLE,
00473 .id = CODEC_ID_HDMV_PGS_SUBTITLE,
00474 .priv_data_size = sizeof(PGSSubContext),
00475 .init = init_decoder,
00476 .close = close_decoder,
00477 .decode = decode,
00478 .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
00479 };