37#define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
38#define MAX_EPOCH_PALETTES 8
39#define MAX_EPOCH_OBJECTS 64
40#define MAX_OBJECT_REFS 2
107 for (
i = 0;
i <
ctx->objects.count;
i++) {
109 ctx->objects.object[
i].rle_buffer_size = 0;
110 ctx->objects.object[
i].rle_remaining_len = 0;
112 ctx->objects.count = 0;
113 ctx->palettes.count = 0;
120 for (
i = 0;
i < objects->
count;
i++) {
131 for (
i = 0;
i < palettes->
count;
i++) {
163 const uint8_t *buf,
unsigned int buf_size)
165 const uint8_t *rle_bitmap_end;
166 int pixel_count, line_count;
168 rle_bitmap_end = buf + buf_size;
178 while (buf < rle_bitmap_end && line_count < rect->
h) {
182 color = bytestream_get_byte(&buf);
186 flags = bytestream_get_byte(&buf);
189 run = (
run << 8) + bytestream_get_byte(&buf);
190 color =
flags & 0x80 ? bytestream_get_byte(&buf) : 0;
201 if (pixel_count %
rect->
w > 0) {
212 if (pixel_count < rect->
w *
rect->
h) {
233 const uint8_t *buf,
int buf_size)
238 uint8_t sequence_desc;
246 id = bytestream_get_be16(&buf);
253 object = &
ctx->objects.object[
ctx->objects.count++];
261 sequence_desc = bytestream_get_byte(&buf);
263 if (!(sequence_desc & 0x80)) {
269 object->rle_data_len += buf_size;
270 object->rle_remaining_len -= buf_size;
280 rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
282 if (buf_size > rle_bitmap_len) {
284 "Buffer dimension %d larger than the expected RLE data %d\n",
285 buf_size, rle_bitmap_len);
290 width = bytestream_get_be16(&buf);
291 height = bytestream_get_be16(&buf);
305 object->rle_data_len = 0;
306 object->rle_remaining_len = 0;
310 memcpy(object->
rle, buf, buf_size);
311 object->rle_data_len = buf_size;
312 object->rle_remaining_len = rle_bitmap_len - buf_size;
328 const uint8_t *buf,
int buf_size)
333 const uint8_t *buf_end = buf + buf_size;
337 int r,
g,
b, r_add, g_add, b_add;
340 id = bytestream_get_byte(&buf);
347 palette = &
ctx->palettes.palette[
ctx->palettes.count++];
354 while (buf < buf_end) {
355 color_id = bytestream_get_byte(&buf);
356 y = bytestream_get_byte(&buf);
357 cr = bytestream_get_byte(&buf);
358 cb = bytestream_get_byte(&buf);
359 alpha = bytestream_get_byte(&buf);
370 ff_dlog(avctx,
"Color %d := (%d,%d,%d,%d)\n", color_id,
r,
g,
b,
alpha);
390 const uint8_t *buf,
int buf_size,
395 const uint8_t *buf_end = buf + buf_size;
398 int w = bytestream_get_be16(&buf);
399 int h = bytestream_get_be16(&buf);
401 ctx->presentation.pts =
pts;
403 ff_dlog(avctx,
"Video Dimensions %dx%d\n",
413 ctx->presentation.id_number = bytestream_get_be16(&buf);
423 state = bytestream_get_byte(&buf) >> 6;
432 ctx->presentation.palette_id = bytestream_get_byte(&buf);
433 ctx->presentation.object_count = bytestream_get_byte(&buf);
436 "Invalid number of presentation objects %d\n",
437 ctx->presentation.object_count);
438 ctx->presentation.object_count = 2;
445 for (
i = 0;
i <
ctx->presentation.object_count;
i++)
449 if (buf_end - buf < 8) {
451 ctx->presentation.object_count =
i;
455 object->id = bytestream_get_be16(&buf);
456 object->window_id = bytestream_get_byte(&buf);
457 object->composition_flag = bytestream_get_byte(&buf);
459 object->x = bytestream_get_be16(&buf);
460 object->y = bytestream_get_be16(&buf);
464 object->crop_x = bytestream_get_be16(&buf);
465 object->crop_y = bytestream_get_be16(&buf);
466 object->crop_w = bytestream_get_be16(&buf);
467 object->crop_h = bytestream_get_be16(&buf);
470 ff_dlog(avctx,
"Subtitle Placement x=%d, y=%d\n",
471 object->
x, object->
y);
474 av_log(avctx,
AV_LOG_ERROR,
"Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
475 object->
x, object->
y,
477 object->y =
object->x = 0;
498 const uint8_t *buf,
int buf_size)
506 memset(sub, 0,
sizeof(*sub));
517 if (!
ctx->presentation.object_count)
527 ctx->presentation.palette_id);
531 for (
i = 0;
i <
ctx->presentation.object_count;
i++) {
543 rect->nb_colors = 256;
547 if (!
ctx->forced_subs_only ||
ctx->presentation.objects[
i].composition_flag & 0x40)
548 memcpy(
rect->data[1], palette->
clut,
rect->nb_colors *
sizeof(uint32_t));
555 ctx->presentation.objects[
i].id);
561 if (
ctx->presentation.objects[
i].composition_flag & 0x40)
564 rect->
x =
ctx->presentation.objects[
i].x;
565 rect->
y =
ctx->presentation.objects[
i].y;
571 rect->linesize[0] =
object->
w;
595 int *got_sub_ptr,
const AVPacket *avpkt)
597 const uint8_t *buf = avpkt->
data;
598 int buf_size = avpkt->
size;
600 const uint8_t *buf_end;
601 uint8_t segment_type;
605 ff_dlog(avctx,
"PGS sub packet:\n");
607 for (
i = 0;
i < buf_size;
i++) {
622 buf_end = buf + buf_size;
625 while (buf < buf_end) {
626 segment_type = bytestream_get_byte(&buf);
627 segment_length = bytestream_get_be16(&buf);
629 ff_dlog(avctx,
"Segment Length %d, Segment Type %x\n", segment_length, segment_type);
635 switch (segment_type) {
667 segment_type, segment_length);
671 if (ret < 0 && (ret ==
AVERROR(ENOMEM) ||
675 buf += segment_length;
681#define OFFSET(x) offsetof(PGSSubContext, x)
682#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
684 {
"forced_subs_only",
"Only show forced subtitles",
OFFSET(forced_subs_only),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
SD},
689 .class_name =
"PGS subtitle decoder",
const FFCodec ff_pgssub_decoder
static av_cold void close(AVCodecParserContext *s)
Libavcodec external API header.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static av_cold int close_decoder(AVCodecContext *avctx)
static av_cold int init_decoder(AVCodecContext *avctx)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_DECODE_SUB_CB(func)
int ff_set_dimensions(AVCodecContext *s, int width, int height)
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
#define AV_EF_EXPLODE
abort decoding on minor error detection
int(* init)(AVBSFContext *ctx)
static struct @346255127015250356166251341105367306144006377143 state
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AV_SUBTITLE_FLAG_FORCED
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
@ SUBTITLE_BITMAP
A bitmap, pict will be set.
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define LIBAVUTIL_VERSION_INT
static const int16_t alpha[]
Various defines for YUV<->RGB conversion.
#define YUV_TO_RGB1_CCIR_BT709(cb1, cr1)
#define YUV_TO_RGB1_CCIR(cb1, cr1)
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
static av_cold int close_decoder(AVCodecContext *avctx)
static void flush_cache(AVCodecContext *avctx)
#define MAX_EPOCH_PALETTES
#define MAX_EPOCH_OBJECTS
static int parse_object_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the picture segment packet.
static av_cold int init_decoder(AVCodecContext *avctx)
static int display_end_segment(AVCodecContext *avctx, AVSubtitle *sub, const uint8_t *buf, int buf_size)
Parse the display segment packet.
static int decode(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *avpkt)
static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect, const uint8_t *buf, unsigned int buf_size)
Decode the RLE data.
static PGSSubObject * find_object(int id, PGSSubObjects *objects)
static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
static const AVClass pgsdec_class
static int parse_presentation_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int64_t pts)
Parse the presentation segment packet.
static int parse_palette_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the palette segment packet.
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Describe the class of an AVClass context structure.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
This structure stores compressed data.
uint32_t start_display_time
uint32_t end_display_time
int64_t pts
Same as packet pts, in AV_TIME_BASE.
PGSSubPresentation presentation
unsigned int rle_buffer_size
unsigned int rle_remaining_len
unsigned int rle_data_len
PGSSubObject object[MAX_EPOCH_OBJECTS]
PGSSubPalette palette[MAX_EPOCH_PALETTES]
PGSSubObjectRef objects[MAX_OBJECT_REFS]
#define av_malloc_array(a, b)
static AVFormatContext * ctx
static double cr(void *priv, double x, double y)
static double cb(void *priv, double x, double y)