2009-08-25 08:47:39 +00:00
|
|
|
/*
|
|
|
|
* PGS subtitle decoder
|
|
|
|
* Copyright (c) 2009 Stephen Backway
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2009-08-25 08:47:39 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2009-08-25 08:47:39 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2009-08-25 08:47:39 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2009-08-25 08:47:39 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2009-08-25 08:47:39 +00:00
|
|
|
* PGS subtitle decoder
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2013-10-27 09:02:26 +00:00
|
|
|
#include "internal.h"
|
2013-12-22 15:25:24 +00:00
|
|
|
#include "mathops.h"
|
2013-10-27 09:02:26 +00:00
|
|
|
|
2010-07-01 18:49:44 +00:00
|
|
|
#include "libavutil/colorspace.h"
|
2011-02-07 13:37:08 +00:00
|
|
|
#include "libavutil/imgutils.h"
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
#define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
|
2014-06-18 20:38:36 +00:00
|
|
|
#define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch
|
|
|
|
#define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch
|
|
|
|
#define MAX_OBJECT_REFS 2 // Max objects per display set
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
enum SegmentType {
|
|
|
|
PALETTE_SEGMENT = 0x14,
|
2014-06-18 20:38:36 +00:00
|
|
|
OBJECT_SEGMENT = 0x15,
|
2009-08-25 08:47:39 +00:00
|
|
|
PRESENTATION_SEGMENT = 0x16,
|
|
|
|
WINDOW_SEGMENT = 0x17,
|
|
|
|
DISPLAY_SEGMENT = 0x80,
|
|
|
|
};
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
typedef struct PGSSubObjectRef {
|
|
|
|
int id;
|
|
|
|
int window_id;
|
|
|
|
uint8_t composition_flag;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int crop_x;
|
|
|
|
int crop_y;
|
|
|
|
int crop_w;
|
|
|
|
int crop_h;
|
|
|
|
} PGSSubObjectRef;
|
|
|
|
|
2009-08-25 08:47:39 +00:00
|
|
|
typedef struct PGSSubPresentation {
|
|
|
|
int id_number;
|
2014-06-18 20:38:36 +00:00
|
|
|
int palette_id;
|
|
|
|
int object_count;
|
|
|
|
PGSSubObjectRef objects[MAX_OBJECT_REFS];
|
2012-11-02 16:30:39 +00:00
|
|
|
int64_t pts;
|
2009-08-25 08:47:39 +00:00
|
|
|
} PGSSubPresentation;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
typedef struct PGSSubObject {
|
|
|
|
int id;
|
2009-08-25 08:47:39 +00:00
|
|
|
int w;
|
|
|
|
int h;
|
|
|
|
uint8_t *rle;
|
|
|
|
unsigned int rle_buffer_size, rle_data_len;
|
2010-11-22 20:35:07 +00:00
|
|
|
unsigned int rle_remaining_len;
|
2014-06-18 20:38:36 +00:00
|
|
|
} PGSSubObject;
|
|
|
|
|
|
|
|
typedef struct PGSSubObjects {
|
|
|
|
int count;
|
|
|
|
PGSSubObject object[MAX_EPOCH_OBJECTS];
|
|
|
|
} PGSSubObjects;
|
|
|
|
|
|
|
|
typedef struct PGSSubPalette {
|
|
|
|
int id;
|
|
|
|
uint32_t clut[256];
|
|
|
|
} PGSSubPalette;
|
|
|
|
|
|
|
|
typedef struct PGSSubPalettes {
|
|
|
|
int count;
|
|
|
|
PGSSubPalette palette[MAX_EPOCH_PALETTES];
|
|
|
|
} PGSSubPalettes;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
typedef struct PGSSubContext {
|
|
|
|
PGSSubPresentation presentation;
|
2014-06-18 20:38:36 +00:00
|
|
|
PGSSubPalettes palettes;
|
|
|
|
PGSSubObjects objects;
|
2009-08-25 08:47:39 +00:00
|
|
|
} PGSSubContext;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
static void flush_cache(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->objects.count; i++) {
|
|
|
|
av_freep(&ctx->objects.object[i].rle);
|
|
|
|
ctx->objects.object[i].rle_buffer_size = 0;
|
|
|
|
ctx->objects.object[i].rle_remaining_len = 0;
|
|
|
|
}
|
|
|
|
ctx->objects.count = 0;
|
|
|
|
ctx->palettes.count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PGSSubObject * find_object(int id, PGSSubObjects *objects)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < objects->count; i++) {
|
|
|
|
if (objects->object[i].id == id)
|
|
|
|
return &objects->object[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < palettes->count; i++) {
|
|
|
|
if (palettes->palette[i].id == id)
|
|
|
|
return &palettes->palette[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-25 08:47:39 +00:00
|
|
|
static av_cold int init_decoder(AVCodecContext *avctx)
|
|
|
|
{
|
2012-10-06 10:10:34 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int close_decoder(AVCodecContext *avctx)
|
|
|
|
{
|
2014-06-18 20:38:36 +00:00
|
|
|
flush_cache(avctx);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 15:38:06 +00:00
|
|
|
* Decode the RLE data.
|
2009-08-25 08:47:39 +00:00
|
|
|
*
|
|
|
|
* The subtitle is stored as an Run Length Encoded image.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2014-06-18 20:38:36 +00:00
|
|
|
static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect,
|
2009-08-25 08:47:39 +00: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;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
rect->pict.data[0] = av_malloc(rect->w * rect->h);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
if (!rect->pict.data[0])
|
|
|
|
return AVERROR(ENOMEM);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
pixel_count = 0;
|
|
|
|
line_count = 0;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
while (buf < rle_bitmap_end && line_count < rect->h) {
|
2009-08-25 08:47:39 +00: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;
|
|
|
|
}
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
if (run > 0 && pixel_count + run <= rect->w * rect->h) {
|
|
|
|
memset(rect->pict.data[0] + pixel_count, color, run);
|
2009-08-25 08:47:39 +00: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.
|
|
|
|
*/
|
2014-06-18 20:38:36 +00:00
|
|
|
if (pixel_count % rect->w > 0) {
|
2009-08-25 08:47:39 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
|
2014-06-18 20:38:36 +00:00
|
|
|
pixel_count % rect->w, rect->w);
|
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) {
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
2009-08-25 08:47:39 +00:00
|
|
|
line_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
if (pixel_count < rect->w * rect->h) {
|
2010-11-22 20:39:18 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
|
2014-06-18 20:38:36 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-11-22 20:39:18 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, rect->w * rect->h);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 15:38:06 +00:00
|
|
|
* Parse the picture segment packet.
|
2009-08-25 08:47:39 +00: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
|
|
|
|
*/
|
2014-06-18 20:38:36 +00:00
|
|
|
static int parse_object_segment(AVCodecContext *avctx,
|
2009-08-25 08:47:39 +00:00
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
2014-06-18 20:38:36 +00:00
|
|
|
PGSSubObject *object;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
uint8_t sequence_desc;
|
|
|
|
unsigned int rle_bitmap_len, width, height;
|
2014-06-18 20:38:36 +00:00
|
|
|
int id;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2010-11-22 20:35:07 +00:00
|
|
|
if (buf_size <= 4)
|
2014-06-18 20:38:36 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-11-22 20:35:07 +00:00
|
|
|
buf_size -= 4;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
id = bytestream_get_be16(&buf);
|
|
|
|
object = find_object(id, &ctx->objects);
|
|
|
|
if (!object) {
|
|
|
|
if (ctx->objects.count >= MAX_EPOCH_OBJECTS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too many objects in epoch\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
object = &ctx->objects.object[ctx->objects.count++];
|
|
|
|
object->id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip object version number */
|
|
|
|
buf += 1;
|
2009-08-25 08:47:39 +00: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 20:35:07 +00:00
|
|
|
/* Additional RLE data */
|
2014-06-18 20:38:36 +00:00
|
|
|
if (buf_size > object->rle_remaining_len)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
memcpy(object->rle + object->rle_data_len, buf, buf_size);
|
|
|
|
object->rle_data_len += buf_size;
|
|
|
|
object->rle_remaining_len -= buf_size;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2010-11-22 20:35:07 +00:00
|
|
|
return 0;
|
2009-08-25 08:47:39 +00:00
|
|
|
}
|
|
|
|
|
2010-11-22 20:35:07 +00:00
|
|
|
if (buf_size <= 7)
|
2014-06-18 20:38:36 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2010-11-22 20:35:07 +00:00
|
|
|
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 08:47:39 +00: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 11:52:03 +00:00
|
|
|
if (avctx->width < width || avctx->height < height) {
|
2011-06-30 18:53:59 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
|
2014-06-18 20:38:36 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2009-08-25 08:47:39 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
object->w = width;
|
|
|
|
object->h = height;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
av_fast_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
if (!object->rle)
|
|
|
|
return AVERROR(ENOMEM);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
memcpy(object->rle, buf, buf_size);
|
|
|
|
object->rle_data_len = buf_size;
|
|
|
|
object->rle_remaining_len = rle_bitmap_len - buf_size;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 15:38:06 +00:00
|
|
|
* Parse the palette segment packet.
|
2009-08-25 08:47:39 +00: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
|
|
|
|
*/
|
2014-06-18 20:38:36 +00:00
|
|
|
static int parse_palette_segment(AVCodecContext *avctx,
|
2009-08-25 08:47:39 +00:00
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
2014-06-18 20:38:36 +00:00
|
|
|
PGSSubPalette *palette;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
const uint8_t *buf_end = buf + buf_size;
|
2013-12-22 13:32:11 +00:00
|
|
|
const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
|
2009-08-25 08:47:39 +00:00
|
|
|
int color_id;
|
|
|
|
int y, cb, cr, alpha;
|
|
|
|
int r, g, b, r_add, g_add, b_add;
|
2014-06-18 20:38:36 +00:00
|
|
|
int id;
|
|
|
|
|
|
|
|
id = bytestream_get_byte(&buf);
|
|
|
|
palette = find_palette(id, &ctx->palettes);
|
|
|
|
if (!palette) {
|
|
|
|
if (ctx->palettes.count >= MAX_EPOCH_PALETTES) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too many palettes in epoch\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
palette = &ctx->palettes.palette[ctx->palettes.count++];
|
|
|
|
palette->id = id;
|
|
|
|
}
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
/* Skip palette version */
|
|
|
|
buf += 1;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
while (buf < buf_end) {
|
|
|
|
color_id = bytestream_get_byte(&buf);
|
|
|
|
y = bytestream_get_byte(&buf);
|
|
|
|
cr = bytestream_get_byte(&buf);
|
2011-03-25 16:31:28 +00:00
|
|
|
cb = bytestream_get_byte(&buf);
|
2009-08-25 08:47:39 +00:00
|
|
|
alpha = bytestream_get_byte(&buf);
|
|
|
|
|
|
|
|
YUV_TO_RGB1(cb, cr);
|
|
|
|
YUV_TO_RGB2(r, g, b, y);
|
|
|
|
|
2011-01-29 16:46:18 +00:00
|
|
|
av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
/* Store color in palette */
|
2014-06-18 20:38:36 +00:00
|
|
|
palette->clut[color_id] = RGBA(r,g,b,alpha);
|
2009-08-25 08:47:39 +00:00
|
|
|
}
|
2014-06-18 20:38:36 +00:00
|
|
|
return 0;
|
2009-08-25 08:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 15:38:06 +00:00
|
|
|
* Parse the presentation segment packet.
|
2009-08-25 08:47:39 +00: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 09:02:26 +00:00
|
|
|
static int parse_presentation_segment(AVCodecContext *avctx,
|
|
|
|
const uint8_t *buf, int buf_size,
|
|
|
|
int64_t pts)
|
2009-08-25 08:47:39 +00:00
|
|
|
{
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
int i, state, ret;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
// Video descriptor
|
2010-08-21 11:52:03 +00:00
|
|
|
int w = bytestream_get_be16(&buf);
|
|
|
|
int h = bytestream_get_be16(&buf);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2012-11-02 16:30:39 +00:00
|
|
|
ctx->presentation.pts = pts;
|
|
|
|
|
2011-01-29 16:46:18 +00:00
|
|
|
av_dlog(avctx, "Video Dimensions %dx%d\n",
|
2010-08-21 11:52:03 +00:00
|
|
|
w, h);
|
2013-10-27 09:02:26 +00:00
|
|
|
ret = ff_set_dimensions(avctx, w, h);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
/* Skip 1 bytes of unknown, frame rate */
|
2009-08-25 08:47:39 +00:00
|
|
|
buf++;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
// Composition descriptor
|
2009-08-25 08:47:39 +00:00
|
|
|
ctx->presentation.id_number = bytestream_get_be16(&buf);
|
2010-11-06 16:48:41 +00:00
|
|
|
/*
|
2014-06-18 20:38:36 +00:00
|
|
|
* state is a 2 bit field that defines pgs epoch boundaries
|
|
|
|
* 00 - Normal, previously defined objects and palettes are still valid
|
|
|
|
* 01 - Acquisition point, previous objects and palettes can be released
|
|
|
|
* 10 - Epoch start, previous objects and palettes can be released
|
|
|
|
* 11 - Epoch continue, previous objects and palettes can be released
|
|
|
|
*
|
|
|
|
* reserved 6 bits discarded
|
2010-11-06 16:48:41 +00:00
|
|
|
*/
|
2014-06-18 20:38:36 +00:00
|
|
|
state = bytestream_get_byte(&buf) >> 6;
|
|
|
|
if (state != 0) {
|
|
|
|
flush_cache(avctx);
|
|
|
|
}
|
2010-11-06 16:48:41 +00:00
|
|
|
|
|
|
|
/*
|
2014-06-18 20:38:36 +00:00
|
|
|
* skip palette_update_flag (0x80),
|
2010-11-06 16:48:41 +00:00
|
|
|
*/
|
2014-06-18 20:38:36 +00:00
|
|
|
buf += 1;
|
|
|
|
ctx->presentation.palette_id = bytestream_get_byte(&buf);
|
|
|
|
ctx->presentation.object_count = bytestream_get_byte(&buf);
|
|
|
|
if (ctx->presentation.object_count > MAX_OBJECT_REFS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Invalid number of presentation objects %d\n",
|
|
|
|
ctx->presentation.object_count);
|
|
|
|
ctx->presentation.object_count = 2;
|
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) {
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
2010-11-06 16:48:41 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
for (i = 0; i < ctx->presentation.object_count; i++)
|
|
|
|
{
|
|
|
|
ctx->presentation.objects[i].id = bytestream_get_be16(&buf);
|
|
|
|
ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf);
|
|
|
|
ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf);
|
|
|
|
|
|
|
|
ctx->presentation.objects[i].x = bytestream_get_be16(&buf);
|
|
|
|
ctx->presentation.objects[i].y = bytestream_get_be16(&buf);
|
|
|
|
|
|
|
|
// If cropping
|
|
|
|
if (ctx->presentation.objects[i].composition_flag & 0x80) {
|
|
|
|
ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf);
|
|
|
|
ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf);
|
|
|
|
ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf);
|
|
|
|
ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf);
|
|
|
|
}
|
2010-11-06 16:48:41 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",
|
|
|
|
ctx->presentation.objects[i].x, ctx->presentation.objects[i].y);
|
|
|
|
|
|
|
|
if (ctx->presentation.objects[i].x > avctx->width ||
|
|
|
|
ctx->presentation.objects[i].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",
|
|
|
|
ctx->presentation.objects[i].x,
|
|
|
|
ctx->presentation.objects[i].y,
|
|
|
|
avctx->width, avctx->height);
|
|
|
|
ctx->presentation.objects[i].x = 0;
|
|
|
|
ctx->presentation.objects[i].y = 0;
|
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) {
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
2010-11-06 16:48:41 +00:00
|
|
|
}
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2013-10-27 09:02:26 +00:00
|
|
|
return 0;
|
2009-08-25 08:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-06-30 15:38:06 +00:00
|
|
|
* Parse the display segment packet.
|
2009-08-25 08:47:39 +00: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
|
|
|
|
*/
|
|
|
|
static int display_end_segment(AVCodecContext *avctx, void *data,
|
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
AVSubtitle *sub = data;
|
|
|
|
PGSSubContext *ctx = avctx->priv_data;
|
2014-06-18 20:38:36 +00:00
|
|
|
PGSSubPalette *palette;
|
|
|
|
int i, ret;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2009-08-26 01:27:58 +00:00
|
|
|
memset(sub, 0, sizeof(*sub));
|
2012-11-02 16:30:39 +00:00
|
|
|
sub->pts = ctx->presentation.pts;
|
2009-08-25 08:47:39 +00:00
|
|
|
sub->start_display_time = 0;
|
2014-06-18 20:38:36 +00:00
|
|
|
// There is no explicit end time for PGS subtitles. The end time
|
|
|
|
// is defined by the start of the next sub which may contain no
|
|
|
|
// objects (i.e. clears the previous sub)
|
|
|
|
sub->end_display_time = UINT32_MAX;
|
2009-08-25 08:47:39 +00:00
|
|
|
sub->format = 0;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
// Blank if last object_count was 0.
|
|
|
|
if (!ctx->presentation.object_count)
|
|
|
|
return 1;
|
|
|
|
sub->rects = av_mallocz(sizeof(*sub->rects) * ctx->presentation.object_count);
|
|
|
|
if (!sub->rects) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
palette = find_palette(ctx->presentation.palette_id, &ctx->palettes);
|
|
|
|
if (!palette) {
|
|
|
|
// Missing palette. Should only happen with damaged streams.
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid palette id %d\n",
|
|
|
|
ctx->presentation.palette_id);
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ctx->presentation.object_count; i++) {
|
|
|
|
PGSSubObject *object;
|
2012-10-20 16:56:11 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
sub->rects[i] = av_mallocz(sizeof(*sub->rects[0]));
|
|
|
|
if (!sub->rects[i]) {
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
sub->num_rects++;
|
|
|
|
sub->rects[i]->type = SUBTITLE_BITMAP;
|
|
|
|
|
|
|
|
/* Process bitmap */
|
|
|
|
object = find_object(ctx->presentation.objects[i].id, &ctx->objects);
|
|
|
|
if (!object) {
|
|
|
|
// Missing object. Should only happen with damaged streams.
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid object id %d\n",
|
|
|
|
ctx->presentation.objects[i].id);
|
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) {
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
// Leaves rect empty with 0 width and height.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ctx->presentation.objects[i].composition_flag & 0x40)
|
|
|
|
sub->rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED;
|
|
|
|
|
|
|
|
sub->rects[i]->x = ctx->presentation.objects[i].x;
|
|
|
|
sub->rects[i]->y = ctx->presentation.objects[i].y;
|
|
|
|
sub->rects[i]->w = object->w;
|
|
|
|
sub->rects[i]->h = object->h;
|
|
|
|
|
|
|
|
sub->rects[i]->pict.linesize[0] = object->w;
|
|
|
|
|
|
|
|
if (object->rle) {
|
|
|
|
if (object->rle_remaining_len) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
|
|
|
|
object->rle_data_len, object->rle_remaining_len);
|
|
|
|
if (avctx->err_recognition & AV_EF_EXPLODE) {
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = decode_rle(avctx, sub->rects[i], object->rle, object->rle_data_len);
|
|
|
|
if (ret < 0) {
|
|
|
|
if ((avctx->err_recognition & AV_EF_EXPLODE) ||
|
|
|
|
ret == AVERROR(ENOMEM)) {
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
sub->rects[i]->w = 0;
|
|
|
|
sub->rects[i]->h = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Allocate memory for colors */
|
|
|
|
sub->rects[i]->nb_colors = 256;
|
|
|
|
sub->rects[i]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
|
|
|
|
if (!sub->rects[i]->pict.data[1]) {
|
|
|
|
avsubtitle_free(sub);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
memcpy(sub->rects[i]->pict.data[1], palette->clut, sub->rects[i]->nb_colors * sizeof(uint32_t));
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2010-11-22 20:35:07 +00:00
|
|
|
}
|
2009-08-25 08:47:39 +00: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;
|
|
|
|
|
|
|
|
const uint8_t *buf_end;
|
|
|
|
uint8_t segment_type;
|
|
|
|
int segment_length;
|
2013-10-27 09:02:26 +00:00
|
|
|
int i, ret;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
2011-06-05 23:25:32 +00:00
|
|
|
av_dlog(avctx, "PGS sub packet:\n");
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
for (i = 0; i < buf_size; i++) {
|
2011-06-05 23:25:32 +00:00
|
|
|
av_dlog(avctx, "%02x ", buf[i]);
|
2009-08-25 08:47:39 +00:00
|
|
|
if (i % 16 == 15)
|
2011-06-05 23:25:32 +00:00
|
|
|
av_dlog(avctx, "\n");
|
2009-08-25 08:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i & 15)
|
2011-06-05 23:25:32 +00:00
|
|
|
av_dlog(avctx, "\n");
|
2009-08-25 08:47:39 +00: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 16:46:18 +00:00
|
|
|
av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
|
|
|
|
break;
|
|
|
|
|
2014-06-18 20:38:36 +00:00
|
|
|
ret = 0;
|
2009-08-25 08:47:39 +00:00
|
|
|
switch (segment_type) {
|
|
|
|
case PALETTE_SEGMENT:
|
2014-06-18 20:38:36 +00:00
|
|
|
ret = parse_palette_segment(avctx, buf, segment_length);
|
2009-08-25 08:47:39 +00:00
|
|
|
break;
|
2014-06-18 20:38:36 +00:00
|
|
|
case OBJECT_SEGMENT:
|
|
|
|
ret = parse_object_segment(avctx, buf, segment_length);
|
2009-08-25 08:47:39 +00:00
|
|
|
break;
|
|
|
|
case PRESENTATION_SEGMENT:
|
2013-10-27 09:02:26 +00:00
|
|
|
ret = parse_presentation_segment(avctx, buf, segment_length, avpkt->pts);
|
2009-08-25 08:47:39 +00:00
|
|
|
break;
|
|
|
|
case WINDOW_SEGMENT:
|
|
|
|
/*
|
|
|
|
* Window Segment Structure (No new information provided):
|
2012-01-25 11:25:11 +00:00
|
|
|
* 2 bytes: Unknown,
|
2009-08-25 08:47:39 +00: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:
|
2014-06-18 20:38:36 +00:00
|
|
|
ret = display_end_segment(avctx, data, buf, segment_length);
|
|
|
|
if (ret >= 0)
|
|
|
|
*data_size = ret;
|
2009-08-25 08:47:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
|
|
|
|
segment_type, segment_length);
|
2014-06-18 20:38:36 +00:00
|
|
|
ret = AVERROR_INVALIDDATA;
|
2009-08-25 08:47:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-06-18 20:38:36 +00:00
|
|
|
if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
|
|
|
|
return ret;
|
2009-08-25 08:47:39 +00:00
|
|
|
|
|
|
|
buf += segment_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2011-01-25 21:40:11 +00:00
|
|
|
AVCodec ff_pgssub_decoder = {
|
2011-07-17 10:54:31 +00:00
|
|
|
.name = "pgssub",
|
2013-10-03 20:57:53 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
|
2011-07-17 10:54:31 +00:00
|
|
|
.type = AVMEDIA_TYPE_SUBTITLE,
|
2012-08-05 09:11:04 +00:00
|
|
|
.id = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
|
2011-07-17 10:54:31 +00:00
|
|
|
.priv_data_size = sizeof(PGSSubContext),
|
|
|
|
.init = init_decoder,
|
|
|
|
.close = close_decoder,
|
|
|
|
.decode = decode,
|
2009-08-25 08:47:39 +00:00
|
|
|
};
|