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