c58b82a2 |
/*
* 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
*/
/** |
ba87f080 |
* @file |
c58b82a2 |
* PGS subtitle decoder
*/
#include "avcodec.h"
#include "bytestream.h" |
41ad353d |
#include "internal.h" |
f9279ee7 |
#include "mathops.h" |
41ad353d |
|
2b4abbd6 |
#include "libavutil/colorspace.h" |
7ffe76e5 |
#include "libavutil/imgutils.h" |
36436a40 |
#include "libavutil/opt.h" |
c58b82a2 |
|
4f241988 |
#define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b)) |
253d0be6 |
#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 |
c58b82a2 |
enum SegmentType {
PALETTE_SEGMENT = 0x14, |
376f353e |
OBJECT_SEGMENT = 0x15, |
c58b82a2 |
PRESENTATION_SEGMENT = 0x16,
WINDOW_SEGMENT = 0x17,
DISPLAY_SEGMENT = 0x80,
};
|
253d0be6 |
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; |
d150a147 |
typedef struct PGSSubPresentation { |
c58b82a2 |
int id_number; |
253d0be6 |
int palette_id;
int object_count;
PGSSubObjectRef objects[MAX_OBJECT_REFS]; |
1c580552 |
int64_t pts; |
c58b82a2 |
} PGSSubPresentation;
|
253d0be6 |
typedef struct PGSSubObject {
int id; |
c58b82a2 |
int w;
int h;
uint8_t *rle;
unsigned int rle_buffer_size, rle_data_len; |
83cd9112 |
unsigned int rle_remaining_len; |
253d0be6 |
} 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; |
c58b82a2 |
typedef struct PGSSubContext { |
36436a40 |
AVClass *class; |
c58b82a2 |
PGSSubPresentation presentation; |
253d0be6 |
PGSSubPalettes palettes;
PGSSubObjects objects; |
36436a40 |
int forced_subs_only; |
c58b82a2 |
} PGSSubContext;
|
4701f767 |
static void flush_cache(AVCodecContext *avctx) |
c58b82a2 |
{
PGSSubContext *ctx = avctx->priv_data; |
253d0be6 |
int i; |
c58b82a2 |
|
253d0be6 |
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;
} |
d150a147 |
|
253d0be6 |
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]; |
d150a147 |
} |
253d0be6 |
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; |
4701f767 |
}
static av_cold int init_decoder(AVCodecContext *avctx)
{
avctx->pix_fmt = AV_PIX_FMT_PAL8;
return 0;
}
static av_cold int close_decoder(AVCodecContext *avctx)
{
flush_cache(avctx); |
c58b82a2 |
return 0;
}
/** |
49bd8e4b |
* Decode the RLE data. |
c58b82a2 |
* |
f18ccb52 |
* The subtitle is stored as a Run Length Encoded image. |
c58b82a2 |
*
* @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
*/ |
5c019ec9 |
static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect, |
c58b82a2 |
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;
|
ee573b4d |
rect->data[0] = av_malloc_array(rect->w, rect->h); |
c58b82a2 |
|
a17a7661 |
if (!rect->data[0]) |
fc7da418 |
return AVERROR(ENOMEM); |
c58b82a2 |
pixel_count = 0;
line_count = 0;
|
5c019ec9 |
while (buf < rle_bitmap_end && line_count < rect->h) { |
c58b82a2 |
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;
}
|
5c019ec9 |
if (run > 0 && pixel_count + run <= rect->w * rect->h) { |
a17a7661 |
memset(rect->data[0] + pixel_count, color, run); |
c58b82a2 |
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.
*/ |
066a4819 |
if (pixel_count % rect->w > 0) { |
c58b82a2 |
av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n", |
5c019ec9 |
pixel_count % rect->w, rect->w); |
066a4819 |
if (avctx->err_recognition & AV_EF_EXPLODE) {
return AVERROR_INVALIDDATA;
}
} |
c58b82a2 |
line_count++;
}
}
|
5c019ec9 |
if (pixel_count < rect->w * rect->h) { |
6b133d7e |
av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n"); |
fc7da418 |
return AVERROR_INVALIDDATA; |
6b133d7e |
}
|
6a85dfc8 |
ff_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, rect->w * rect->h); |
c58b82a2 |
return 0;
}
/** |
49bd8e4b |
* Parse the picture segment packet. |
c58b82a2 |
*
* 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
*/ |
253d0be6 |
static int parse_object_segment(AVCodecContext *avctx, |
c58b82a2 |
const uint8_t *buf, int buf_size)
{
PGSSubContext *ctx = avctx->priv_data; |
253d0be6 |
PGSSubObject *object; |
c58b82a2 |
uint8_t sequence_desc;
unsigned int rle_bitmap_len, width, height; |
253d0be6 |
int id; |
c58b82a2 |
|
83cd9112 |
if (buf_size <= 4) |
fc7da418 |
return AVERROR_INVALIDDATA; |
83cd9112 |
buf_size -= 4;
|
253d0be6 |
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;
} |
d150a147 |
|
253d0be6 |
/* skip object version number */
buf += 1; |
c58b82a2 |
/* 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)) { |
83cd9112 |
/* Additional RLE data */ |
253d0be6 |
if (buf_size > object->rle_remaining_len) |
fc7da418 |
return AVERROR_INVALIDDATA; |
c58b82a2 |
|
253d0be6 |
memcpy(object->rle + object->rle_data_len, buf, buf_size);
object->rle_data_len += buf_size;
object->rle_remaining_len -= buf_size; |
c58b82a2 |
|
83cd9112 |
return 0; |
c58b82a2 |
}
|
83cd9112 |
if (buf_size <= 7) |
fc7da418 |
return AVERROR_INVALIDDATA; |
83cd9112 |
buf_size -= 7;
/* Decode rle bitmap length, stored size includes width/height data */
rle_bitmap_len = bytestream_get_be24(&buf) - 2*2; |
c58b82a2 |
|
d98e6c5d |
if (buf_size > rle_bitmap_len) {
av_log(avctx, AV_LOG_ERROR,
"Buffer dimension %d larger than the expected RLE data %d\n",
buf_size, rle_bitmap_len);
return AVERROR_INVALIDDATA;
}
|
c58b82a2 |
/* Get bitmap dimensions from data */
width = bytestream_get_be16(&buf);
height = bytestream_get_be16(&buf);
/* Make sure the bitmap is not too large */ |
ebf5264c |
if (avctx->width < width || avctx->height < height || !width || !height) {
av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions (%dx%d) invalid.\n", width, height); |
fc7da418 |
return AVERROR_INVALIDDATA; |
c58b82a2 |
}
|
253d0be6 |
object->w = width;
object->h = height; |
c58b82a2 |
|
ae9a73de |
av_fast_padded_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len); |
c58b82a2 |
|
83269fd1 |
if (!object->rle) {
object->rle_data_len = 0;
object->rle_remaining_len = 0; |
fc7da418 |
return AVERROR(ENOMEM); |
83269fd1 |
} |
c58b82a2 |
|
253d0be6 |
memcpy(object->rle, buf, buf_size);
object->rle_data_len = buf_size;
object->rle_remaining_len = rle_bitmap_len - buf_size; |
c58b82a2 |
return 0;
}
/** |
49bd8e4b |
* Parse the palette segment packet. |
c58b82a2 |
*
* 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
*/ |
ca7f2a73 |
static int parse_palette_segment(AVCodecContext *avctx, |
c58b82a2 |
const uint8_t *buf, int buf_size)
{
PGSSubContext *ctx = avctx->priv_data; |
253d0be6 |
PGSSubPalette *palette; |
c58b82a2 |
const uint8_t *buf_end = buf + buf_size; |
05563cca |
const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP; |
c58b82a2 |
int color_id;
int y, cb, cr, alpha;
int r, g, b, r_add, g_add, b_add; |
253d0be6 |
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;
} |
c58b82a2 |
|
253d0be6 |
/* Skip palette version */
buf += 1; |
c58b82a2 |
while (buf < buf_end) {
color_id = bytestream_get_byte(&buf);
y = bytestream_get_byte(&buf);
cr = bytestream_get_byte(&buf); |
5fd7bc25 |
cb = bytestream_get_byte(&buf); |
c58b82a2 |
alpha = bytestream_get_byte(&buf);
|
885a9d60 |
/* Default to BT.709 colorspace. In case of <= 576 height use BT.601 */ |
9779b626 |
if (avctx->height <= 0 || avctx->height > 576) {
YUV_TO_RGB1_CCIR_BT709(cb, cr);
} else {
YUV_TO_RGB1_CCIR(cb, cr);
}
YUV_TO_RGB2_CCIR(r, g, b, y); |
c58b82a2 |
|
6a85dfc8 |
ff_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha); |
c58b82a2 |
/* Store color in palette */ |
253d0be6 |
palette->clut[color_id] = RGBA(r,g,b,alpha); |
c58b82a2 |
} |
ca7f2a73 |
return 0; |
c58b82a2 |
}
/** |
49bd8e4b |
* Parse the presentation segment packet. |
c58b82a2 |
*
* 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
*/ |
41ad353d |
static int parse_presentation_segment(AVCodecContext *avctx,
const uint8_t *buf, int buf_size,
int64_t pts) |
c58b82a2 |
{
PGSSubContext *ctx = avctx->priv_data; |
253d0be6 |
int i, state, ret; |
89bcb777 |
const uint8_t *buf_end = buf + buf_size; |
c58b82a2 |
|
253d0be6 |
// Video descriptor |
a29b1700 |
int w = bytestream_get_be16(&buf);
int h = bytestream_get_be16(&buf); |
c58b82a2 |
|
1c580552 |
ctx->presentation.pts = pts;
|
6a85dfc8 |
ff_dlog(avctx, "Video Dimensions %dx%d\n", |
a29b1700 |
w, h); |
41ad353d |
ret = ff_set_dimensions(avctx, w, h);
if (ret < 0)
return ret; |
c58b82a2 |
|
253d0be6 |
/* Skip 1 bytes of unknown, frame rate */ |
c58b82a2 |
buf++;
|
253d0be6 |
// Composition descriptor |
c58b82a2 |
ctx->presentation.id_number = bytestream_get_be16(&buf); |
c4d5ee23 |
/* |
253d0be6 |
* 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 |
c4d5ee23 |
*/ |
253d0be6 |
state = bytestream_get_byte(&buf) >> 6;
if (state != 0) {
flush_cache(avctx);
} |
c4d5ee23 |
/* |
253d0be6 |
* skip palette_update_flag (0x80), |
c4d5ee23 |
*/ |
253d0be6 |
buf += 1;
ctx->presentation.palette_id = bytestream_get_byte(&buf); |
d150a147 |
ctx->presentation.object_count = bytestream_get_byte(&buf); |
253d0be6 |
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;
} |
d150a147 |
} |
c4d5ee23 |
|
253d0be6 |
for (i = 0; i < ctx->presentation.object_count; i++)
{ |
89bcb777 |
if (buf_end - buf < 8) {
av_log(avctx, AV_LOG_ERROR, "Insufficent space for object\n");
ctx->presentation.object_count = i;
return AVERROR_INVALIDDATA;
}
|
253d0be6 |
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); |
c4d5ee23 |
|
253d0be6 |
ctx->presentation.objects[i].x = bytestream_get_be16(&buf);
ctx->presentation.objects[i].y = bytestream_get_be16(&buf); |
c4d5ee23 |
|
253d0be6 |
// 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);
} |
c58b82a2 |
|
6a85dfc8 |
ff_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", |
253d0be6 |
ctx->presentation.objects[i].x, ctx->presentation.objects[i].y); |
d150a147 |
|
253d0be6 |
if (ctx->presentation.objects[i].x > avctx->width ||
ctx->presentation.objects[i].y > avctx->height) { |
d150a147 |
av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n", |
253d0be6 |
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;
} |
d150a147 |
}
} |
41ad353d |
return 0; |
c58b82a2 |
}
/** |
49bd8e4b |
* Parse the display segment packet. |
c58b82a2 |
*
* 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; |
37bbc9eb |
int64_t pts; |
253d0be6 |
PGSSubPalette *palette;
int i, ret; |
c58b82a2 |
|
4116151a |
pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts; |
d150a147 |
memset(sub, 0, sizeof(*sub)); |
37bbc9eb |
sub->pts = pts; |
4116151a |
ctx->presentation.pts = AV_NOPTS_VALUE; |
c58b82a2 |
sub->start_display_time = 0; |
0c911c8f |
// 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; |
c58b82a2 |
sub->format = 0;
|
253d0be6 |
// Blank if last object_count was 0.
if (!ctx->presentation.object_count)
return 1; |
4207c5a4 |
sub->rects = av_mallocz_array(ctx->presentation.object_count, sizeof(*sub->rects)); |
253d0be6 |
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; |
d150a147 |
|
253d0be6 |
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; |
d150a147 |
/* Process bitmap */ |
253d0be6 |
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; |
d150a147 |
} |
253d0be6 |
if (ctx->presentation.objects[i].composition_flag & 0x40)
sub->rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED; |
d150a147 |
|
253d0be6 |
sub->rects[i]->x = ctx->presentation.objects[i].x;
sub->rects[i]->y = ctx->presentation.objects[i].y;
if (object->rle) { |
1dc59aaf |
sub->rects[i]->w = object->w;
sub->rects[i]->h = object->h;
sub->rects[i]->linesize[0] = object->w;
|
253d0be6 |
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;
}
} |
d150a147 |
/* Allocate memory for colors */ |
253d0be6 |
sub->rects[i]->nb_colors = 256; |
a17a7661 |
sub->rects[i]->data[1] = av_mallocz(AVPALETTE_SIZE);
if (!sub->rects[i]->data[1]) { |
253d0be6 |
avsubtitle_free(sub);
return AVERROR(ENOMEM);
} |
c58b82a2 |
|
ae9a73de |
if (!ctx->forced_subs_only || ctx->presentation.objects[i].composition_flag & 0x40) |
a17a7661 |
memcpy(sub->rects[i]->data[1], palette->clut, sub->rects[i]->nb_colors * sizeof(uint32_t)); |
c58b82a2 |
|
ee573b4d |
#if FF_API_AVPICTURE
FF_DISABLE_DEPRECATION_WARNINGS |
b7e64fba |
{
AVSubtitleRect *rect;
int j; |
ee573b4d |
rect = sub->rects[i];
for (j = 0; j < 4; j++) {
rect->pict.data[j] = rect->data[j];
rect->pict.linesize[j] = rect->linesize[j];
} |
b7e64fba |
} |
ee573b4d |
FF_ENABLE_DEPRECATION_WARNINGS
#endif |
d150a147 |
} |
c58b82a2 |
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; |
41ad353d |
int i, ret; |
c58b82a2 |
|
6a85dfc8 |
ff_dlog(avctx, "PGS sub packet:\n"); |
c58b82a2 |
for (i = 0; i < buf_size; i++) { |
6a85dfc8 |
ff_dlog(avctx, "%02x ", buf[i]); |
c58b82a2 |
if (i % 16 == 15) |
6a85dfc8 |
ff_dlog(avctx, "\n"); |
c58b82a2 |
}
if (i & 15) |
6a85dfc8 |
ff_dlog(avctx, "\n"); |
c58b82a2 |
*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);
|
6a85dfc8 |
ff_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type); |
c58b82a2 |
if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
break;
|
ca7f2a73 |
ret = 0; |
c58b82a2 |
switch (segment_type) {
case PALETTE_SEGMENT: |
ca7f2a73 |
ret = parse_palette_segment(avctx, buf, segment_length); |
c58b82a2 |
break; |
376f353e |
case OBJECT_SEGMENT: |
253d0be6 |
ret = parse_object_segment(avctx, buf, segment_length); |
c58b82a2 |
break;
case PRESENTATION_SEGMENT: |
2cebd17e |
ret = parse_presentation_segment(avctx, buf, segment_length, ((AVSubtitle*)(data))->pts); |
c58b82a2 |
break;
case WINDOW_SEGMENT:
/*
* Window Segment Structure (No new information provided): |
ffae713a |
* 2 bytes: Unknown, |
c58b82a2 |
* 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: |
ca7f2a73 |
ret = display_end_segment(avctx, data, buf, segment_length);
if (ret >= 0)
*data_size = ret; |
c58b82a2 |
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
segment_type, segment_length); |
ca7f2a73 |
ret = AVERROR_INVALIDDATA; |
c58b82a2 |
break;
} |
ca7f2a73 |
if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
return ret; |
c58b82a2 |
buf += segment_length;
}
return buf_size;
}
|
36436a40 |
#define OFFSET(x) offsetof(PGSSubContext, x)
#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
static const AVOption options[] = { |
8e22becb |
{"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD}, |
36436a40 |
{ NULL },
};
static const AVClass pgsdec_class = {
.class_name = "PGS subtitle decoder",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
|
e7e2df27 |
AVCodec ff_pgssub_decoder = { |
ec6402b7 |
.name = "pgssub", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"), |
ec6402b7 |
.type = AVMEDIA_TYPE_SUBTITLE, |
36ef5369 |
.id = AV_CODEC_ID_HDMV_PGS_SUBTITLE, |
ec6402b7 |
.priv_data_size = sizeof(PGSSubContext),
.init = init_decoder,
.close = close_decoder,
.decode = decode, |
36436a40 |
.priv_class = &pgsdec_class, |
c58b82a2 |
}; |