1 /*
2 * PGS subtitle decoder
3 * Copyright (c) 2009 Stephen Backway
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * PGS subtitle decoder
25 */
26
31
35
36 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
37 #define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch
38 #define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch
39 #define MAX_OBJECT_REFS 2 // Max objects per display set
40
47 };
48
60
68
77
82
87
92
100
102 {
104 int i;
105
110 }
113 }
114
116 {
117 int i;
118
119 for (i = 0; i < objects->
count; i++) {
121 return &objects->
object[i];
122 }
124 }
125
127 {
128 int i;
129
130 for (i = 0; i < palettes->
count; i++) {
133 }
135 }
136
138 {
140
141 return 0;
142 }
143
145 {
147
148 return 0;
149 }
150
151 /**
152 * Decode the RLE data.
153 *
154 * The subtitle is stored as a Run Length Encoded image.
155 *
156 * @param avctx contains the current codec context
157 * @param sub pointer to the processed subtitle data
158 * @param buf pointer to the RLE data to process
159 * @param buf_size size of the RLE data to process
160 */
163 {
165 int pixel_count, line_count;
166
167 rle_bitmap_end = buf + buf_size;
168
170
173
174 pixel_count = 0;
175 line_count = 0;
176
177 while (buf < rle_bitmap_end && line_count < rect->
h) {
180
181 color = bytestream_get_byte(&buf);
182 run = 1;
183
184 if (color == 0x00) {
185 flags = bytestream_get_byte(&buf);
186 run = flags & 0x3f;
187 if (flags & 0x40)
188 run = (run << 8) + bytestream_get_byte(&buf);
189 color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
190 }
191
192 if (run > 0 && pixel_count + run <= rect->
w * rect->
h) {
193 memset(rect->
data[0] + pixel_count, color, run);
195 } else if (!run) {
196 /*
197 * New Line. Check if correct pixels decoded, if not display warning
198 * and adjust bitmap pointer to correct new line position.
199 */
200 if (pixel_count % rect->
w > 0) {
202 pixel_count % rect->
w, rect->
w);
205 }
206 }
207 line_count++;
208 }
209 }
210
211 if (pixel_count < rect->
w * rect->
h) {
214 }
215
216 ff_dlog(avctx,
"Pixel Count = %d, Area = %d\n", pixel_count, rect->
w * rect->
h);
217
218 return 0;
219 }
220
221 /**
222 * Parse the picture segment packet.
223 *
224 * The picture segment contains details on the sequence id,
225 * width, height and Run Length Encoded (RLE) bitmap data.
226 *
227 * @param avctx contains the current codec context
228 * @param buf pointer to the packet to process
229 * @param buf_size size of packet to process
230 */
233 {
236
240
241 if (buf_size <= 4)
243 buf_size -= 4;
244
245 id = bytestream_get_be16(&buf);
247 if (!object) {
251 }
254 }
255
256 /* skip object version number */
257 buf += 1;
258
259 /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
260 sequence_desc = bytestream_get_byte(&buf);
261
262 if (!(sequence_desc & 0x80)) {
263 /* Additional RLE data */
266
268 object->rle_data_len += buf_size;
269 object->rle_remaining_len -= buf_size;
270
271 return 0;
272 }
273
274 if (buf_size <= 7)
276 buf_size -= 7;
277
278 /* Decode rle bitmap length, stored size includes width/height data */
279 rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
280
281 if (buf_size > rle_bitmap_len) {
283 "Buffer dimension %d larger than the expected RLE data %d\n",
284 buf_size, rle_bitmap_len);
286 }
287
288 /* Get bitmap dimensions from data */
289 width = bytestream_get_be16(&buf);
290 height = bytestream_get_be16(&buf);
291
292 /* Make sure the bitmap is not too large */
293 if (avctx->
width < width || avctx->
height < height || !width || !height) {
296 }
297
300
302
304 object->rle_data_len = 0;
305 object->rle_remaining_len = 0;
307 }
308
309 memcpy(object->
rle, buf, buf_size);
310 object->rle_data_len = buf_size;
311 object->rle_remaining_len = rle_bitmap_len - buf_size;
312
313 return 0;
314 }
315
316 /**
317 * Parse the palette segment packet.
318 *
319 * The palette segment contains details of the palette,
320 * a maximum of 256 colors can be defined.
321 *
322 * @param avctx contains the current codec context
323 * @param buf pointer to the packet to process
324 * @param buf_size size of packet to process
325 */
328 {
331
332 const uint8_t *buf_end = buf + buf_size;
334 int color_id;
336 int r,
g,
b, r_add, g_add, b_add;
338
339 id = bytestream_get_byte(&buf);
341 if (!palette) {
345 }
348 }
349
350 /* Skip palette version */
351 buf += 1;
352
353 while (buf < buf_end) {
354 color_id = bytestream_get_byte(&buf);
355 y = bytestream_get_byte(&buf);
356 cr = bytestream_get_byte(&buf);
357 cb = bytestream_get_byte(&buf);
358 alpha = bytestream_get_byte(&buf);
359
360 /* Default to BT.709 colorspace. In case of <= 576 height use BT.601 */
363 } else {
365 }
366
368
369 ff_dlog(avctx,
"Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
370
371 /* Store color in palette */
372 palette->
clut[color_id] =
RGBA(r,g,b,alpha);
373 }
374 return 0;
375 }
376
377 /**
378 * Parse the presentation segment packet.
379 *
380 * The presentation segment contains details on the video
381 * width, video height, x & y subtitle position.
382 *
383 * @param avctx contains the current codec context
384 * @param buf pointer to the packet to process
385 * @param buf_size size of packet to process
386 * @todo TODO: Implement cropping
387 */
391 {
394 const uint8_t *buf_end = buf + buf_size;
395
396 // Video descriptor
397 int w = bytestream_get_be16(&buf);
398 int h = bytestream_get_be16(&buf);
399
401
402 ff_dlog(avctx,
"Video Dimensions %dx%d\n",
403 w, h);
405 if (ret < 0)
406 return ret;
407
408 /* Skip 1 bytes of unknown, frame rate */
409 buf++;
410
411 // Composition descriptor
413 /*
414 * state is a 2 bit field that defines pgs epoch boundaries
415 * 00 - Normal, previously defined objects and palettes are still valid
416 * 01 - Acquisition point, previous objects and palettes can be released
417 * 10 - Epoch start, previous objects and palettes can be released
418 * 11 - Epoch continue, previous objects and palettes can be released
419 *
420 * reserved 6 bits discarded
421 */
422 state = bytestream_get_byte(&buf) >> 6;
423 if (state != 0) {
425 }
426
427 /*
428 * skip palette_update_flag (0x80),
429 */
430 buf += 1;
435 "Invalid number of presentation objects %d\n",
440 }
441 }
442
443
445 {
446
447 if (buf_end - buf < 8) {
451 }
452
456
459
460 // If cropping
466 }
467
468 ff_dlog(avctx,
"Subtitle Placement x=%d, y=%d\n",
470
473 av_log(avctx,
AV_LOG_ERROR,
"Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
481 }
482 }
483 }
484
485 return 0;
486 }
487
488 /**
489 * Parse the display segment packet.
490 *
491 * The display segment controls the updating of the display.
492 *
493 * @param avctx contains the current codec context
494 * @param data pointer to the data pertaining the subtitle to display
495 * @param buf pointer to the packet to process
496 * @param buf_size size of packet to process
497 */
500 {
505 int i, ret;
506
508 memset(sub, 0, sizeof(*sub));
512 // There is no explicit end time for PGS subtitles. The end time
513 // is defined by the start of the next sub which may contain no
514 // objects (i.e. clears the previous sub)
517
518 // Blank if last object_count was 0.
520 return 1;
524 }
526 if (!palette) {
527 // Missing palette. Should only happen with damaged streams.
532 }
535
537 if (!sub->
rects[i]) {
540 }
543
544 /* Process bitmap */
546 if (!object) {
547 // Missing object. Should only happen with damaged streams.
553 }
554 // Leaves rect empty with 0 width and height.
555 continue;
556 }
559
562
564 sub->
rects[i]->
w =
object->w;
565 sub->
rects[i]->
h =
object->h;
566
568
575 }
576 }
578 if (ret < 0) {
582 return ret;
583 }
586 continue;
587 }
588 }
589 /* Allocate memory for colors */
595 }
596
599
600 #if FF_API_AVPICTURE
602 {
604 int j;
605 rect = sub->
rects[i];
606 for (j = 0; j < 4; j++) {
609 }
610 }
612 #endif
613 }
614 return 1;
615 }
616
619 {
621 int buf_size = avpkt->
size;
622
625 int segment_length;
626 int i, ret;
627
628 ff_dlog(avctx,
"PGS sub packet:\n");
629
630 for (i = 0; i < buf_size; i++) {
631 ff_dlog(avctx,
"%02x ", buf[i]);
632 if (i % 16 == 15)
634 }
635
636 if (i & 15)
638
639 *data_size = 0;
640
641 /* Ensure that we have received at a least a segment code and segment length */
642 if (buf_size < 3)
643 return -1;
644
645 buf_end = buf + buf_size;
646
647 /* Step through buffer to identify segments */
648 while (buf < buf_end) {
649 segment_type = bytestream_get_byte(&buf);
650 segment_length = bytestream_get_be16(&buf);
651
652 ff_dlog(avctx,
"Segment Length %d, Segment Type %x\n", segment_length, segment_type);
653
655 break;
656
657 ret = 0;
658 switch (segment_type) {
661 break;
664 break;
667 break;
669 /*
670 * Window Segment Structure (No new information provided):
671 * 2 bytes: Unknown,
672 * 2 bytes: X position of subtitle,
673 * 2 bytes: Y position of subtitle,
674 * 2 bytes: Width of subtitle,
675 * 2 bytes: Height of subtitle.
676 */
677 break;
680 if (ret >= 0)
681 *data_size = ret;
682 break;
683 default:
685 segment_type, segment_length);
687 break;
688 }
690 return ret;
691
692 buf += segment_length;
693 }
694
695 return buf_size;
696 }
697
698 #define OFFSET(x) offsetof(PGSSubContext, x)
699 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
701 {
"forced_subs_only",
"Only show forced subtitles",
OFFSET(forced_subs_only),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
SD},
703 };
704
710 };
711
721 .priv_class = &pgsdec_class,
722 };
static int parse_object_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the picture segment packet.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static float alpha(float a)
int x
top left corner of pict, undefined when pict is not set
ptrdiff_t const GLvoid * data
#define LIBAVUTIL_VERSION_INT
static int parse_presentation_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int64_t pts)
Parse the presentation segment packet.
#define YUV_TO_RGB1_CCIR(cb1, cr1)
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
static av_cold int init(AVCodecContext *avctx)
int nb_colors
number of colors in pict, undefined when pict is not set
const char * av_default_item_name(void *ptr)
Return the context name.
Various defines for YUV<->RGB conversion.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
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...
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
AVCodec ff_pgssub_decoder
PGSSubPresentation presentation
attribute_deprecated AVPicture pict
int w
width of pict, undefined when pict is not set
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static double cb(void *priv, double x, double y)
8 bits with AV_PIX_FMT_RGB32 palette
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
attribute_deprecated int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
#define YUV_TO_RGB1_CCIR_BT709(cb1, cr1)
static const AVClass pgsdec_class
static int decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
int h
height of pict, undefined when pict is not set
static int parse_palette_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the palette segment packet.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int y
top left corner of pict, undefined when pict is not set
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect, const uint8_t *buf, unsigned int buf_size)
Decode the RLE data.
const char * name
Name of the codec implementation.
static const AVOption options[]
uint32_t end_display_time
int64_t pts
Same as packet pts, in AV_TIME_BASE.
PGSSubObject object[MAX_EPOCH_OBJECTS]
A bitmap, pict will be set.
#define AV_SUBTITLE_FLAG_FORCED
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
static av_cold int close_decoder(AVCodecContext *avctx)
int width
picture width / height.
static PGSSubObject * find_object(int id, PGSSubObjects *objects)
attribute_deprecated uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
#define AV_EF_EXPLODE
abort decoding on minor error detection
static av_cold int init_decoder(AVCodecContext *avctx)
static int display_end_segment(AVCodecContext *avctx, void *data, const uint8_t *buf, int buf_size)
Parse the display segment packet.
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
#define MAX_EPOCH_OBJECTS
Libavcodec external API header.
main external API structure.
static void flush_cache(AVCodecContext *avctx)
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Describe the class of an AVClass context structure.
PGSSubPalette palette[MAX_EPOCH_PALETTES]
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
PGSSubObjectRef objects[MAX_OBJECT_REFS]
unsigned int rle_remaining_len
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
unsigned int rle_data_len
uint32_t start_display_time
static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
#define FF_ENABLE_DEPRECATION_WARNINGS
unsigned int rle_buffer_size
#define av_malloc_array(a, b)
static double cr(void *priv, double x, double y)
This structure stores compressed data.
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define MAX_EPOCH_PALETTES
void * av_mallocz_array(size_t nmemb, size_t size)