2009-08-25 10:47:39 +02:00
|
|
|
/*
|
|
|
|
* PGS subtitle decoder
|
|
|
|
* Copyright (c) 2009 Stephen Backway
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2009-08-25 10:47:39 +02:00
|
|
|
* PGS subtitle decoder
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "dsputil.h"
|
|
|
|
#include "bytestream.h"
|
2013-10-27 10:02:26 +01:00
|
|
|
#include "internal.h"
|
|
|
|
|
2010-07-01 20:49:44 +02:00
|
|
|
#include "libavutil/colorspace.h"
|
2011-02-07 14:37:08 +01:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-01-30 11:25:59 +01:00
|
|
|
#include "libavutil/opt.h"
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
|
|
|
|
|
|
|
|
enum SegmentType {
|
|
|
|
PALETTE_SEGMENT = 0x14,
|
|
|
|
PICTURE_SEGMENT = 0x15,
|
|
|
|
PRESENTATION_SEGMENT = 0x16,
|
|
|
|
WINDOW_SEGMENT = 0x17,
|
|
|
|
DISPLAY_SEGMENT = 0x80,
|
|
|
|
};
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
typedef struct PGSSubPictureReference {
|
2009-08-25 10:47:39 +02:00
|
|
|
int x;
|
|
|
|
int y;
|
2012-01-19 16:31:01 +01:00
|
|
|
int picture_id;
|
2012-01-30 11:25:59 +01:00
|
|
|
int composition;
|
2012-01-19 16:31:01 +01:00
|
|
|
} PGSSubPictureReference;
|
|
|
|
|
|
|
|
typedef struct PGSSubPresentation {
|
|
|
|
int id_number;
|
|
|
|
int object_count;
|
|
|
|
PGSSubPictureReference *objects;
|
2012-11-02 17:30:39 +01:00
|
|
|
int64_t pts;
|
2009-08-25 10:47:39 +02:00
|
|
|
} PGSSubPresentation;
|
|
|
|
|
|
|
|
typedef struct PGSSubPicture {
|
|
|
|
int w;
|
|
|
|
int h;
|
|
|
|
uint8_t *rle;
|
|
|
|
unsigned int rle_buffer_size, rle_data_len;
|
2010-11-22 21:35:07 +01:00
|
|
|
unsigned int rle_remaining_len;
|
2009-08-25 10:47:39 +02:00
|
|
|
} PGSSubPicture;
|
|
|
|
|
|
|
|
typedef struct PGSSubContext {
|
2012-01-30 11:25:59 +01:00
|
|
|
AVClass *class;
|
2009-08-25 10:47:39 +02:00
|
|
|
PGSSubPresentation presentation;
|
|
|
|
uint32_t clut[256];
|
2012-01-19 16:31:01 +01:00
|
|
|
PGSSubPicture pictures[UINT16_MAX];
|
2012-01-30 11:25:59 +01:00
|
|
|
int forced_subs_only;
|
2009-08-25 10:47:39 +02:00
|
|
|
} PGSSubContext;
|
|
|
|
|
|
|
|
static av_cold int init_decoder(AVCodecContext *avctx)
|
|
|
|
{
|
2012-10-08 20:54:00 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int close_decoder(AVCodecContext *avctx)
|
|
|
|
{
|
2012-01-19 16:31:01 +01:00
|
|
|
uint16_t picture;
|
|
|
|
|
2009-08-25 10:47:39 +02:00
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
av_freep(&ctx->presentation.objects);
|
|
|
|
ctx->presentation.object_count = 0;
|
|
|
|
|
|
|
|
for (picture = 0; picture < UINT16_MAX; ++picture) {
|
|
|
|
av_freep(&ctx->pictures[picture].rle);
|
|
|
|
ctx->pictures[picture].rle_buffer_size = 0;
|
|
|
|
}
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 17:38:06 +02:00
|
|
|
* Decode the RLE data.
|
2009-08-25 10:47:39 +02:00
|
|
|
*
|
2013-08-11 21:21:53 +02:00
|
|
|
* The subtitle is stored as a Run Length Encoded image.
|
2009-08-25 10:47:39 +02:00
|
|
|
*
|
|
|
|
* @param avctx contains the current codec context
|
|
|
|
* @param sub pointer to the processed subtitle data
|
|
|
|
* @param buf pointer to the RLE data to process
|
|
|
|
* @param buf_size size of the RLE data to process
|
|
|
|
*/
|
2012-01-19 16:31:01 +01:00
|
|
|
static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub, int rect,
|
2009-08-25 10:47:39 +02:00
|
|
|
const uint8_t *buf, unsigned int buf_size)
|
|
|
|
{
|
|
|
|
const uint8_t *rle_bitmap_end;
|
|
|
|
int pixel_count, line_count;
|
|
|
|
|
|
|
|
rle_bitmap_end = buf + buf_size;
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
sub->rects[rect]->pict.data[0] = av_malloc(sub->rects[rect]->w * sub->rects[rect]->h);
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
if (!sub->rects[rect]->pict.data[0])
|
2009-08-25 10:47:39 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
pixel_count = 0;
|
|
|
|
line_count = 0;
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
while (buf < rle_bitmap_end && line_count < sub->rects[rect]->h) {
|
2009-08-25 10:47:39 +02:00
|
|
|
uint8_t flags, color;
|
|
|
|
int run;
|
|
|
|
|
|
|
|
color = bytestream_get_byte(&buf);
|
|
|
|
run = 1;
|
|
|
|
|
|
|
|
if (color == 0x00) {
|
|
|
|
flags = bytestream_get_byte(&buf);
|
|
|
|
run = flags & 0x3f;
|
|
|
|
if (flags & 0x40)
|
|
|
|
run = (run << 8) + bytestream_get_byte(&buf);
|
|
|
|
color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
if (run > 0 && pixel_count + run <= sub->rects[rect]->w * sub->rects[rect]->h) {
|
|
|
|
memset(sub->rects[rect]->pict.data[0] + pixel_count, color, run);
|
2009-08-25 10:47:39 +02:00
|
|
|
pixel_count += run;
|
|
|
|
} else if (!run) {
|
|
|
|
/*
|
|
|
|
* New Line. Check if correct pixels decoded, if not display warning
|
|
|
|
* and adjust bitmap pointer to correct new line position.
|
|
|
|
*/
|
2012-01-19 16:31:01 +01:00
|
|
|
if (pixel_count % sub->rects[rect]->w > 0)
|
2009-08-25 10:47:39 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
|
2012-01-19 16:31:01 +01:00
|
|
|
pixel_count % sub->rects[rect]->w, sub->rects[rect]->w);
|
2009-08-25 10:47:39 +02:00
|
|
|
line_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
if (pixel_count < sub->rects[rect]->w * sub->rects[rect]->h) {
|
2010-11-22 21:39:18 +01:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[rect]->w * sub->rects[rect]->h);
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 17:38:06 +02:00
|
|
|
* Parse the picture segment packet.
|
2009-08-25 10:47:39 +02:00
|
|
|
*
|
|
|
|
* The picture segment contains details on the sequence id,
|
|
|
|
* width, height and Run Length Encoded (RLE) bitmap data.
|
|
|
|
*
|
|
|
|
* @param avctx contains the current codec context
|
|
|
|
* @param buf pointer to the packet to process
|
|
|
|
* @param buf_size size of packet to process
|
|
|
|
* @todo TODO: Enable support for RLE data over multiple packets
|
|
|
|
*/
|
|
|
|
static int parse_picture_segment(AVCodecContext *avctx,
|
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
uint8_t sequence_desc;
|
|
|
|
unsigned int rle_bitmap_len, width, height;
|
2012-01-19 16:31:01 +01:00
|
|
|
uint16_t picture_id;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2010-11-22 21:35:07 +01:00
|
|
|
if (buf_size <= 4)
|
|
|
|
return -1;
|
|
|
|
buf_size -= 4;
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
picture_id = bytestream_get_be16(&buf);
|
|
|
|
|
|
|
|
/* skip 1 unknown byte: Version Number */
|
|
|
|
buf++;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
/* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
|
|
|
|
sequence_desc = bytestream_get_byte(&buf);
|
|
|
|
|
|
|
|
if (!(sequence_desc & 0x80)) {
|
2010-11-22 21:35:07 +01:00
|
|
|
/* Additional RLE data */
|
2012-01-19 16:31:01 +01:00
|
|
|
if (buf_size > ctx->pictures[picture_id].rle_remaining_len)
|
2010-11-22 21:35:07 +01:00
|
|
|
return -1;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
memcpy(ctx->pictures[picture_id].rle + ctx->pictures[picture_id].rle_data_len, buf, buf_size);
|
|
|
|
ctx->pictures[picture_id].rle_data_len += buf_size;
|
|
|
|
ctx->pictures[picture_id].rle_remaining_len -= buf_size;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2010-11-22 21:35:07 +01:00
|
|
|
return 0;
|
2009-08-25 10:47:39 +02:00
|
|
|
}
|
|
|
|
|
2010-11-22 21:35:07 +01:00
|
|
|
if (buf_size <= 7)
|
|
|
|
return -1;
|
|
|
|
buf_size -= 7;
|
|
|
|
|
|
|
|
/* Decode rle bitmap length, stored size includes width/height data */
|
|
|
|
rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
/* Get bitmap dimensions from data */
|
|
|
|
width = bytestream_get_be16(&buf);
|
|
|
|
height = bytestream_get_be16(&buf);
|
|
|
|
|
|
|
|
/* Make sure the bitmap is not too large */
|
2010-08-21 13:52:03 +02:00
|
|
|
if (avctx->width < width || avctx->height < height) {
|
2011-06-30 20:53:59 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
|
2009-08-25 10:47:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-15 16:41:28 +01:00
|
|
|
if (buf_size > rle_bitmap_len) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "too much RLE data\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
ctx->pictures[picture_id].w = width;
|
|
|
|
ctx->pictures[picture_id].h = height;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2014-01-15 23:06:30 +01:00
|
|
|
av_fast_padded_malloc(&ctx->pictures[picture_id].rle, &ctx->pictures[picture_id].rle_buffer_size, rle_bitmap_len);
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
if (!ctx->pictures[picture_id].rle)
|
2009-08-25 10:47:39 +02:00
|
|
|
return -1;
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
memcpy(ctx->pictures[picture_id].rle, buf, buf_size);
|
|
|
|
ctx->pictures[picture_id].rle_data_len = buf_size;
|
|
|
|
ctx->pictures[picture_id].rle_remaining_len = rle_bitmap_len - buf_size;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 17:38:06 +02:00
|
|
|
* Parse the palette segment packet.
|
2009-08-25 10:47:39 +02:00
|
|
|
*
|
|
|
|
* The palette segment contains details of the palette,
|
|
|
|
* a maximum of 256 colors can be defined.
|
|
|
|
*
|
|
|
|
* @param avctx contains the current codec context
|
|
|
|
* @param buf pointer to the packet to process
|
|
|
|
* @param buf_size size of packet to process
|
|
|
|
*/
|
|
|
|
static void parse_palette_segment(AVCodecContext *avctx,
|
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
const uint8_t *buf_end = buf + buf_size;
|
2013-12-22 14:32:11 +01:00
|
|
|
const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
|
2009-08-25 10:47:39 +02:00
|
|
|
int color_id;
|
|
|
|
int y, cb, cr, alpha;
|
|
|
|
int r, g, b, r_add, g_add, b_add;
|
|
|
|
|
|
|
|
/* Skip two null bytes */
|
|
|
|
buf += 2;
|
|
|
|
|
|
|
|
while (buf < buf_end) {
|
|
|
|
color_id = bytestream_get_byte(&buf);
|
|
|
|
y = bytestream_get_byte(&buf);
|
|
|
|
cr = bytestream_get_byte(&buf);
|
2011-03-25 17:31:28 +01:00
|
|
|
cb = bytestream_get_byte(&buf);
|
2009-08-25 10:47:39 +02:00
|
|
|
alpha = bytestream_get_byte(&buf);
|
|
|
|
|
|
|
|
YUV_TO_RGB1(cb, cr);
|
|
|
|
YUV_TO_RGB2(r, g, b, y);
|
|
|
|
|
2011-01-29 17:46:18 +01:00
|
|
|
av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
/* Store color in palette */
|
|
|
|
ctx->clut[color_id] = RGBA(r,g,b,alpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 17:38:06 +02:00
|
|
|
* Parse the presentation segment packet.
|
2009-08-25 10:47:39 +02:00
|
|
|
*
|
|
|
|
* The presentation segment contains details on the video
|
|
|
|
* width, video height, x & y subtitle position.
|
|
|
|
*
|
|
|
|
* @param avctx contains the current codec context
|
|
|
|
* @param buf pointer to the packet to process
|
|
|
|
* @param buf_size size of packet to process
|
|
|
|
* @todo TODO: Implement cropping
|
|
|
|
*/
|
2013-10-27 10:02:26 +01:00
|
|
|
static int parse_presentation_segment(AVCodecContext *avctx,
|
|
|
|
const uint8_t *buf, int buf_size,
|
|
|
|
int64_t pts)
|
2009-08-25 10:47:39 +02:00
|
|
|
{
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
2013-11-01 15:25:09 +01:00
|
|
|
int ret;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2010-08-21 13:52:03 +02:00
|
|
|
int w = bytestream_get_be16(&buf);
|
|
|
|
int h = bytestream_get_be16(&buf);
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
uint16_t object_index;
|
|
|
|
|
2012-11-02 17:30:39 +01:00
|
|
|
ctx->presentation.pts = pts;
|
|
|
|
|
2011-01-29 17:46:18 +01:00
|
|
|
av_dlog(avctx, "Video Dimensions %dx%d\n",
|
2010-08-21 13:52:03 +02:00
|
|
|
w, h);
|
2013-10-27 10:02:26 +01:00
|
|
|
ret = ff_set_dimensions(avctx, w, h);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
/* Skip 1 bytes of unknown, frame rate? */
|
|
|
|
buf++;
|
|
|
|
|
|
|
|
ctx->presentation.id_number = bytestream_get_be16(&buf);
|
|
|
|
|
2010-11-06 17:48:41 +01:00
|
|
|
/*
|
|
|
|
* Skip 3 bytes of unknown:
|
|
|
|
* state
|
|
|
|
* palette_update_flag (0x80),
|
|
|
|
* palette_id_to_use,
|
|
|
|
*/
|
|
|
|
buf += 3;
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
ctx->presentation.object_count = bytestream_get_byte(&buf);
|
|
|
|
if (!ctx->presentation.object_count)
|
2013-10-27 10:02:26 +01:00
|
|
|
return 0;
|
2010-11-06 17:48:41 +01:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
/* Verify that enough bytes are remaining for all of the objects. */
|
|
|
|
buf_size -= 11;
|
|
|
|
if (buf_size < ctx->presentation.object_count * 8) {
|
|
|
|
ctx->presentation.object_count = 0;
|
2013-11-01 15:25:09 +01:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-01-19 16:31:01 +01:00
|
|
|
}
|
2010-11-06 17:48:41 +01:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
av_freep(&ctx->presentation.objects);
|
|
|
|
ctx->presentation.objects = av_malloc(sizeof(PGSSubPictureReference) * ctx->presentation.object_count);
|
|
|
|
if (!ctx->presentation.objects) {
|
|
|
|
ctx->presentation.object_count = 0;
|
2013-11-01 15:25:09 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2012-01-19 16:31:01 +01:00
|
|
|
}
|
2010-11-06 17:48:41 +01:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
for (object_index = 0; object_index < ctx->presentation.object_count; ++object_index) {
|
|
|
|
PGSSubPictureReference *reference = &ctx->presentation.objects[object_index];
|
|
|
|
reference->picture_id = bytestream_get_be16(&buf);
|
2010-11-06 17:48:41 +01:00
|
|
|
|
2012-01-30 11:25:59 +01:00
|
|
|
/* Skip window_id_ref */
|
|
|
|
buf++;
|
|
|
|
/* composition_flag (0x80 - object cropped, 0x40 - object forced) */
|
|
|
|
reference->composition = bytestream_get_byte(&buf);
|
2010-11-06 17:48:41 +01:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
reference->x = bytestream_get_be16(&buf);
|
|
|
|
reference->y = bytestream_get_be16(&buf);
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
/* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
|
|
|
|
av_dlog(avctx, "Subtitle Placement ID=%d, x=%d, y=%d\n", reference->picture_id, reference->x, reference->y);
|
|
|
|
|
|
|
|
if (reference->x > avctx->width || reference->y > avctx->height) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
|
|
|
|
reference->x, reference->y, avctx->width, avctx->height);
|
|
|
|
reference->x = 0;
|
|
|
|
reference->y = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-10-27 10:02:26 +01:00
|
|
|
|
|
|
|
return 0;
|
2009-08-25 10:47:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 17:38:06 +02:00
|
|
|
* Parse the display segment packet.
|
2009-08-25 10:47:39 +02:00
|
|
|
*
|
|
|
|
* The display segment controls the updating of the display.
|
|
|
|
*
|
|
|
|
* @param avctx contains the current codec context
|
|
|
|
* @param data pointer to the data pertaining the subtitle to display
|
|
|
|
* @param buf pointer to the packet to process
|
|
|
|
* @param buf_size size of packet to process
|
|
|
|
* @todo TODO: Fix start time, relies on correct PTS, currently too late
|
|
|
|
*
|
|
|
|
* @todo TODO: Fix end time, normally cleared by a second display
|
|
|
|
* @todo segment, which is currently ignored as it clears
|
|
|
|
* @todo the subtitle too early.
|
|
|
|
*/
|
|
|
|
static int display_end_segment(AVCodecContext *avctx, void *data,
|
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
AVSubtitle *sub = data;
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
2012-09-09 16:08:39 +02:00
|
|
|
int64_t pts;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
uint16_t rect;
|
|
|
|
|
2009-08-25 10:47:39 +02:00
|
|
|
/*
|
|
|
|
* The end display time is a timeout value and is only reached
|
2012-01-19 16:31:01 +01:00
|
|
|
* if the next subtitle is later than timeout or subtitle has
|
2009-08-25 10:47:39 +02:00
|
|
|
* not been cleared by a subsequent empty display command.
|
|
|
|
*/
|
|
|
|
|
2012-11-19 14:05:41 +01:00
|
|
|
pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts;
|
2012-01-19 16:31:01 +01:00
|
|
|
memset(sub, 0, sizeof(*sub));
|
2012-09-09 16:08:39 +02:00
|
|
|
sub->pts = pts;
|
2012-11-19 14:05:41 +01:00
|
|
|
ctx->presentation.pts = AV_NOPTS_VALUE;
|
2012-01-19 16:31:01 +01:00
|
|
|
|
|
|
|
// Blank if last object_count was 0.
|
|
|
|
if (!ctx->presentation.object_count)
|
2010-11-06 17:46:03 +01:00
|
|
|
return 1;
|
2012-01-19 16:31:01 +01:00
|
|
|
|
2009-08-25 10:47:39 +02:00
|
|
|
sub->start_display_time = 0;
|
|
|
|
sub->end_display_time = 20000;
|
|
|
|
sub->format = 0;
|
|
|
|
|
2012-01-19 16:31:01 +01:00
|
|
|
sub->num_rects = ctx->presentation.object_count;
|
|
|
|
sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
|
|
|
|
|
|
|
|
for (rect = 0; rect < sub->num_rects; ++rect) {
|
|
|
|
uint16_t picture_id = ctx->presentation.objects[rect].picture_id;
|
|
|
|
sub->rects[rect] = av_mallocz(sizeof(*sub->rects[rect]));
|
|
|
|
sub->rects[rect]->x = ctx->presentation.objects[rect].x;
|
|
|
|
sub->rects[rect]->y = ctx->presentation.objects[rect].y;
|
|
|
|
sub->rects[rect]->w = ctx->pictures[picture_id].w;
|
|
|
|
sub->rects[rect]->h = ctx->pictures[picture_id].h;
|
|
|
|
sub->rects[rect]->type = SUBTITLE_BITMAP;
|
|
|
|
|
|
|
|
/* Process bitmap */
|
|
|
|
sub->rects[rect]->pict.linesize[0] = ctx->pictures[picture_id].w;
|
|
|
|
if (ctx->pictures[picture_id].rle) {
|
|
|
|
if (ctx->pictures[picture_id].rle_remaining_len)
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
|
|
|
|
ctx->pictures[picture_id].rle_data_len, ctx->pictures[picture_id].rle_remaining_len);
|
|
|
|
if (decode_rle(avctx, sub, rect, ctx->pictures[picture_id].rle, ctx->pictures[picture_id].rle_data_len) < 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory for colors */
|
|
|
|
sub->rects[rect]->nb_colors = 256;
|
|
|
|
sub->rects[rect]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2012-04-16 03:58:40 +02:00
|
|
|
/* Copy the forced flag */
|
2012-11-03 14:13:45 +01:00
|
|
|
sub->rects[rect]->flags = (ctx->presentation.objects[rect].composition & 0x40) != 0 ? AV_SUBTITLE_FLAG_FORCED : 0;
|
2012-04-16 03:58:40 +02:00
|
|
|
|
2012-01-30 11:25:59 +01:00
|
|
|
if (!ctx->forced_subs_only || ctx->presentation.objects[rect].composition & 0x40)
|
2012-01-19 16:31:01 +01:00
|
|
|
memcpy(sub->rects[rect]->pict.data[1], ctx->clut, sub->rects[rect]->nb_colors * sizeof(uint32_t));
|
|
|
|
}
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int decode(AVCodecContext *avctx, void *data, int *data_size,
|
|
|
|
AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2012-09-09 16:08:39 +02:00
|
|
|
AVSubtitle *sub = data;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
const uint8_t *buf_end;
|
|
|
|
uint8_t segment_type;
|
|
|
|
int segment_length;
|
2013-10-27 10:02:26 +01:00
|
|
|
int i, ret;
|
2009-08-25 10:47:39 +02:00
|
|
|
|
2011-06-06 01:25:32 +02:00
|
|
|
av_dlog(avctx, "PGS sub packet:\n");
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
for (i = 0; i < buf_size; i++) {
|
2011-06-06 01:25:32 +02:00
|
|
|
av_dlog(avctx, "%02x ", buf[i]);
|
2009-08-25 10:47:39 +02:00
|
|
|
if (i % 16 == 15)
|
2011-06-06 01:25:32 +02:00
|
|
|
av_dlog(avctx, "\n");
|
2009-08-25 10:47:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i & 15)
|
2011-06-06 01:25:32 +02:00
|
|
|
av_dlog(avctx, "\n");
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
*data_size = 0;
|
|
|
|
|
|
|
|
/* Ensure that we have received at a least a segment code and segment length */
|
|
|
|
if (buf_size < 3)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
buf_end = buf + buf_size;
|
|
|
|
|
|
|
|
/* Step through buffer to identify segments */
|
|
|
|
while (buf < buf_end) {
|
|
|
|
segment_type = bytestream_get_byte(&buf);
|
|
|
|
segment_length = bytestream_get_be16(&buf);
|
|
|
|
|
2011-01-29 17:46:18 +01:00
|
|
|
av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
|
2009-08-25 10:47:39 +02:00
|
|
|
|
|
|
|
if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (segment_type) {
|
|
|
|
case PALETTE_SEGMENT:
|
|
|
|
parse_palette_segment(avctx, buf, segment_length);
|
|
|
|
break;
|
|
|
|
case PICTURE_SEGMENT:
|
|
|
|
parse_picture_segment(avctx, buf, segment_length);
|
|
|
|
break;
|
|
|
|
case PRESENTATION_SEGMENT:
|
2013-11-01 15:25:09 +01:00
|
|
|
ret = parse_presentation_segment(avctx, buf, segment_length, sub->pts);
|
2013-10-27 10:02:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2009-08-25 10:47:39 +02:00
|
|
|
break;
|
|
|
|
case WINDOW_SEGMENT:
|
|
|
|
/*
|
|
|
|
* Window Segment Structure (No new information provided):
|
2012-01-25 12:25:11 +01:00
|
|
|
* 2 bytes: Unknown,
|
2009-08-25 10:47:39 +02:00
|
|
|
* 2 bytes: X position of subtitle,
|
|
|
|
* 2 bytes: Y position of subtitle,
|
|
|
|
* 2 bytes: Width of subtitle,
|
|
|
|
* 2 bytes: Height of subtitle.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case DISPLAY_SEGMENT:
|
|
|
|
*data_size = display_end_segment(avctx, data, buf, segment_length);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
|
|
|
|
segment_type, segment_length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf += segment_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2012-01-30 11:25:59 +01:00
|
|
|
#define OFFSET(x) offsetof(PGSSubContext, x)
|
|
|
|
#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2012-09-05 14:26:01 +02:00
|
|
|
{"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
|
2012-01-30 11:25:59 +01:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass pgsdec_class = {
|
|
|
|
.class_name = "PGS subtitle decoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_pgssub_decoder = {
|
2011-07-17 12:54:31 +02:00
|
|
|
.name = "pgssub",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
|
2011-07-17 12:54:31 +02:00
|
|
|
.type = AVMEDIA_TYPE_SUBTITLE,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
|
2011-07-17 12:54:31 +02:00
|
|
|
.priv_data_size = sizeof(PGSSubContext),
|
|
|
|
.init = init_decoder,
|
|
|
|
.close = close_decoder,
|
|
|
|
.decode = decode,
|
2012-01-30 11:25:59 +01:00
|
|
|
.priv_class = &pgsdec_class,
|
2009-08-25 10:47:39 +02:00
|
|
|
};
|