pgssubdec.c
Go to the documentation of this file.
1 /*
2  * PGS subtitle decoder
3  * Copyright (c) 2009 Stephen Backway
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include "dsputil.h"
29 #include "bytestream.h"
30 #include "libavutil/colorspace.h"
31 #include "libavutil/imgutils.h"
32 
33 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
34 
41 };
42 
43 typedef struct PGSSubPresentation {
44  int x;
45  int y;
46  int id_number;
49  int64_t pts;
51 
52 typedef struct PGSSubPicture {
53  int w;
54  int h;
57  unsigned int rle_remaining_len;
59 
60 typedef struct PGSSubContext {
62  uint32_t clut[256];
65 
67 {
68  avctx->pix_fmt = AV_PIX_FMT_PAL8;
69 
70  return 0;
71 }
72 
74 {
75  PGSSubContext *ctx = avctx->priv_data;
76 
77  av_freep(&ctx->picture.rle);
78  ctx->picture.rle_buffer_size = 0;
79 
80  return 0;
81 }
82 
93 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
94  const uint8_t *buf, unsigned int buf_size)
95 {
96  const uint8_t *rle_bitmap_end;
97  int pixel_count, line_count;
98 
99  rle_bitmap_end = buf + buf_size;
100 
101  sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h);
102 
103  if (!sub->rects[0]->pict.data[0])
104  return -1;
105 
106  pixel_count = 0;
107  line_count = 0;
108 
109  while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
111  int run;
112 
113  color = bytestream_get_byte(&buf);
114  run = 1;
115 
116  if (color == 0x00) {
117  flags = bytestream_get_byte(&buf);
118  run = flags & 0x3f;
119  if (flags & 0x40)
120  run = (run << 8) + bytestream_get_byte(&buf);
121  color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
122  }
123 
124  if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) {
125  memset(sub->rects[0]->pict.data[0] + pixel_count, color, run);
126  pixel_count += run;
127  } else if (!run) {
128  /*
129  * New Line. Check if correct pixels decoded, if not display warning
130  * and adjust bitmap pointer to correct new line position.
131  */
132  if (pixel_count % sub->rects[0]->w > 0)
133  av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
134  pixel_count % sub->rects[0]->w, sub->rects[0]->w);
135  line_count++;
136  }
137  }
138 
139  if (pixel_count < sub->rects[0]->w * sub->rects[0]->h) {
140  av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
141  return -1;
142  }
143 
144  av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
145 
146  return 0;
147 }
148 
161  const uint8_t *buf, int buf_size)
162 {
163  PGSSubContext *ctx = avctx->priv_data;
164 
165  uint8_t sequence_desc;
166  unsigned int rle_bitmap_len, width, height;
167 
168  if (buf_size <= 4)
169  return -1;
170  buf_size -= 4;
171 
172  /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
173  buf += 3;
174 
175  /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
176  sequence_desc = bytestream_get_byte(&buf);
177 
178  if (!(sequence_desc & 0x80)) {
179  /* Additional RLE data */
180  if (buf_size > ctx->picture.rle_remaining_len)
181  return -1;
182 
183  memcpy(ctx->picture.rle + ctx->picture.rle_data_len, buf, buf_size);
184  ctx->picture.rle_data_len += buf_size;
185  ctx->picture.rle_remaining_len -= buf_size;
186 
187  return 0;
188  }
189 
190  if (buf_size <= 7)
191  return -1;
192  buf_size -= 7;
193 
194  /* Decode rle bitmap length, stored size includes width/height data */
195  rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
196 
197  if (buf_size > rle_bitmap_len) {
198  av_log(avctx, AV_LOG_ERROR,
199  "Buffer dimension %d larger than the expected RLE data %d\n",
200  buf_size, rle_bitmap_len);
201  return AVERROR_INVALIDDATA;
202  }
203 
204  /* Get bitmap dimensions from data */
205  width = bytestream_get_be16(&buf);
206  height = bytestream_get_be16(&buf);
207 
208  /* Make sure the bitmap is not too large */
209  if (avctx->width < width || avctx->height < height) {
210  av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
211  return -1;
212  }
213 
214  ctx->picture.w = width;
215  ctx->picture.h = height;
216 
217  av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
218 
219  if (!ctx->picture.rle)
220  return -1;
221 
222  memcpy(ctx->picture.rle, buf, buf_size);
223  ctx->picture.rle_data_len = buf_size;
224  ctx->picture.rle_remaining_len = rle_bitmap_len - buf_size;
225 
226  return 0;
227 }
228 
240  const uint8_t *buf, int buf_size)
241 {
242  PGSSubContext *ctx = avctx->priv_data;
243 
244  const uint8_t *buf_end = buf + buf_size;
245  const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
246  int color_id;
247  int y, cb, cr, alpha;
248  int r, g, b, r_add, g_add, b_add;
249 
250  /* Skip two null bytes */
251  buf += 2;
252 
253  while (buf < buf_end) {
254  color_id = bytestream_get_byte(&buf);
255  y = bytestream_get_byte(&buf);
256  cr = bytestream_get_byte(&buf);
257  cb = bytestream_get_byte(&buf);
258  alpha = bytestream_get_byte(&buf);
259 
260  YUV_TO_RGB1(cb, cr);
261  YUV_TO_RGB2(r, g, b, y);
262 
263  av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
264 
265  /* Store color in palette */
266  ctx->clut[color_id] = RGBA(r,g,b,alpha);
267  }
268 }
269 
283  const uint8_t *buf, int buf_size,
284  int64_t pts)
285 {
286  PGSSubContext *ctx = avctx->priv_data;
287 
288  int x, y;
289 
290  int w = bytestream_get_be16(&buf);
291  int h = bytestream_get_be16(&buf);
292 
293  ctx->presentation.pts = pts;
294 
295  av_dlog(avctx, "Video Dimensions %dx%d\n",
296  w, h);
297  if (av_image_check_size(w, h, 0, avctx) >= 0)
298  avcodec_set_dimensions(avctx, w, h);
299 
300  /* Skip 1 bytes of unknown, frame rate? */
301  buf++;
302 
303  ctx->presentation.id_number = bytestream_get_be16(&buf);
304 
305  /*
306  * Skip 3 bytes of unknown:
307  * state
308  * palette_update_flag (0x80),
309  * palette_id_to_use,
310  */
311  buf += 3;
312 
313  ctx->presentation.object_number = bytestream_get_byte(&buf);
315  if (!ctx->presentation.object_number)
316  return;
317 
318  /*
319  * Skip 3 bytes of unknown:
320  * object_id_ref (2 bytes),
321  * window_id_ref,
322  */
323  buf += 3;
324  ctx->presentation.composition_flag = bytestream_get_byte(&buf);
325 
326  x = bytestream_get_be16(&buf);
327  y = bytestream_get_be16(&buf);
328 
329  /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
330 
331  av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
332 
333  if (x > avctx->width || y > avctx->height) {
334  av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
335  x, y, avctx->width, avctx->height);
336  x = 0; y = 0;
337  }
338 
339  /* Fill in dimensions */
340  ctx->presentation.x = x;
341  ctx->presentation.y = y;
342 }
343 
359 static int display_end_segment(AVCodecContext *avctx, void *data,
360  const uint8_t *buf, int buf_size)
361 {
362  AVSubtitle *sub = data;
363  PGSSubContext *ctx = avctx->priv_data;
364 
365  /*
366  * The end display time is a timeout value and is only reached
367  * if the next subtitle is later then timeout or subtitle has
368  * not been cleared by a subsequent empty display command.
369  */
370 
371  memset(sub, 0, sizeof(*sub));
372  sub->pts = ctx->presentation.pts;
373 
374  // Blank if last object_number was 0.
375  // Note that this may be wrong for more complex subtitles.
376  if (!ctx->presentation.object_number)
377  return 1;
378  sub->start_display_time = 0;
379  sub->end_display_time = 20000;
380  sub->format = 0;
381 
382  sub->rects = av_mallocz(sizeof(*sub->rects));
383  sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
384  sub->num_rects = 1;
385 
386  if (ctx->presentation.composition_flag & 0x40)
387  sub->rects[0]->flags |= AV_SUBTITLE_FLAG_FORCED;
388 
389  sub->rects[0]->x = ctx->presentation.x;
390  sub->rects[0]->y = ctx->presentation.y;
391  sub->rects[0]->w = ctx->picture.w;
392  sub->rects[0]->h = ctx->picture.h;
393  sub->rects[0]->type = SUBTITLE_BITMAP;
394 
395  /* Process bitmap */
396  sub->rects[0]->pict.linesize[0] = ctx->picture.w;
397 
398  if (ctx->picture.rle) {
399  if (ctx->picture.rle_remaining_len)
400  av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
402  if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
403  return 0;
404  }
405  /* Allocate memory for colors */
406  sub->rects[0]->nb_colors = 256;
407  sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
408 
409  memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
410 
411  return 1;
412 }
413 
414 static int decode(AVCodecContext *avctx, void *data, int *data_size,
415  AVPacket *avpkt)
416 {
417  const uint8_t *buf = avpkt->data;
418  int buf_size = avpkt->size;
419 
420  const uint8_t *buf_end;
421  uint8_t segment_type;
422  int segment_length;
423  int i;
424 
425  av_dlog(avctx, "PGS sub packet:\n");
426 
427  for (i = 0; i < buf_size; i++) {
428  av_dlog(avctx, "%02x ", buf[i]);
429  if (i % 16 == 15)
430  av_dlog(avctx, "\n");
431  }
432 
433  if (i & 15)
434  av_dlog(avctx, "\n");
435 
436  *data_size = 0;
437 
438  /* Ensure that we have received at a least a segment code and segment length */
439  if (buf_size < 3)
440  return -1;
441 
442  buf_end = buf + buf_size;
443 
444  /* Step through buffer to identify segments */
445  while (buf < buf_end) {
446  segment_type = bytestream_get_byte(&buf);
447  segment_length = bytestream_get_be16(&buf);
448 
449  av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
450 
451  if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
452  break;
453 
454  switch (segment_type) {
455  case PALETTE_SEGMENT:
456  parse_palette_segment(avctx, buf, segment_length);
457  break;
458  case PICTURE_SEGMENT:
459  parse_picture_segment(avctx, buf, segment_length);
460  break;
462  parse_presentation_segment(avctx, buf, segment_length, avpkt->pts);
463  break;
464  case WINDOW_SEGMENT:
465  /*
466  * Window Segment Structure (No new information provided):
467  * 2 bytes: Unknown,
468  * 2 bytes: X position of subtitle,
469  * 2 bytes: Y position of subtitle,
470  * 2 bytes: Width of subtitle,
471  * 2 bytes: Height of subtitle.
472  */
473  break;
474  case DISPLAY_SEGMENT:
475  *data_size = display_end_segment(avctx, data, buf, segment_length);
476  break;
477  default:
478  av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
479  segment_type, segment_length);
480  break;
481  }
482 
483  buf += segment_length;
484  }
485 
486  return buf_size;
487 }
488 
490  .name = "pgssub",
491  .type = AVMEDIA_TYPE_SUBTITLE,
493  .priv_data_size = sizeof(PGSSubContext),
494  .init = init_decoder,
495  .close = close_decoder,
496  .decode = decode,
497  .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
498 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
#define ff_cropTbl
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:3155
int x
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:3186
misc image utilities
unsigned int rle_remaining_len
Definition: pgssubdec.c:57
#define MAX_NEG_CROP
Definition: dsputil.h:83
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int nb_colors
number of colors in pict, undefined when pict is not set
Definition: avcodec.h:3190
int size
Definition: avcodec.h:916
Various defines for YUV<->RGB conversion.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
unsigned num_rects
Definition: avcodec.h:3214
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
uint8_t run
Definition: svq3.c:124
AVCodec ff_pgssub_decoder
Definition: pgssubdec.c:489
PGSSubPresentation presentation
Definition: pgssubdec.c:61
AVCodec.
Definition: avcodec.h:2960
static void parse_presentation_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int64_t pts)
Parse the presentation segment packet.
Definition: pgssubdec.c:282
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
AVSubtitleRect ** rects
Definition: avcodec.h:3215
int w
width of pict, undefined when pict is not set
Definition: avcodec.h:3188
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: utils.c:72
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: avcodec.h:3154
#define b
Definition: input.c:52
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int flags
Definition: log.c:42
static int decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: pgssubdec.c:414
int h
height of pict, undefined when pict is not set
Definition: avcodec.h:3189
uint32_t clut[256]
Definition: pgssubdec.c:62
struct PGSSubPresentation PGSSubPresentation
#define cm
Definition: dvbsubdec.c:34
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define r
Definition: input.c:51
static int parse_picture_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the picture segment packet.
Definition: pgssubdec.c:160
#define RGBA(r, g, b, a)
Definition: pgssubdec.c:33
#define YUV_TO_RGB2(r, g, b, y1)
Definition: colorspace.h:61
int y
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:3187
unsigned int rle_buffer_size
Definition: pgssubdec.c:56
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
g
Definition: yuv2rgb.c:540
#define YUV_TO_RGB1(cb1, cr1)
Definition: colorspace.h:52
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
uint32_t end_display_time
Definition: avcodec.h:3213
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:3216
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:220
A bitmap, pict will be set.
Definition: avcodec.h:3168
AVPicture pict
data+linesize for the bitmap of this subtitle.
Definition: avcodec.h:3196
static av_cold int close_decoder(AVCodecContext *avctx)
Definition: pgssubdec.c:73
int width
picture width / height.
Definition: avcodec.h:1508
uint16_t format
Definition: avcodec.h:3211
struct PGSSubContext PGSSubContext
static av_cold int init_decoder(AVCodecContext *avctx)
Definition: pgssubdec.c:66
static int display_end_segment(AVCodecContext *avctx, void *data, const uint8_t *buf, int buf_size)
Parse the display segment packet.
Definition: pgssubdec.c:359
static int width
Definition: utils.c:156
external API header
static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub, const uint8_t *buf, unsigned int buf_size)
Decode the RLE data.
Definition: pgssubdec.c:93
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
PGSSubPicture picture
Definition: pgssubdec.c:63
struct PGSSubPicture PGSSubPicture
int height
Definition: gxfenc.c:72
uint32_t start_display_time
Definition: avcodec.h:3212
static void parse_palette_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the palette segment packet.
Definition: pgssubdec.c:239
static const uint8_t color[]
Definition: log.c:52
DSP utils.
void * priv_data
Definition: avcodec.h:1382
uint8_t * rle
Definition: pgssubdec.c:55
SegmentType
Definition: pgssubdec.c:35
unsigned int rle_data_len
Definition: pgssubdec.c:56
uint8_t composition_flag
Definition: pgssubdec.c:48
enum AVSubtitleType type
Definition: avcodec.h:3197
This structure stores compressed data.
Definition: avcodec.h:898
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908