Libav
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 "internal.h"
31 
32 #include "libavutil/colorspace.h"
33 #include "libavutil/imgutils.h"
34 
35 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
36 
43 };
44 
45 typedef struct PGSSubPresentation {
46  int x;
47  int y;
48  int id_number;
51  int64_t pts;
53 
54 typedef struct PGSSubPicture {
55  int w;
56  int h;
59  unsigned int rle_remaining_len;
61 
62 typedef struct PGSSubContext {
64  uint32_t clut[256];
67 
69 {
70  avctx->pix_fmt = AV_PIX_FMT_PAL8;
71 
72  return 0;
73 }
74 
76 {
77  PGSSubContext *ctx = avctx->priv_data;
78 
79  av_freep(&ctx->picture.rle);
80  ctx->picture.rle_buffer_size = 0;
81 
82  return 0;
83 }
84 
95 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
96  const uint8_t *buf, unsigned int buf_size)
97 {
98  const uint8_t *rle_bitmap_end;
99  int pixel_count, line_count;
100 
101  rle_bitmap_end = buf + buf_size;
102 
103  sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h);
104 
105  if (!sub->rects[0]->pict.data[0])
106  return -1;
107 
108  pixel_count = 0;
109  line_count = 0;
110 
111  while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
113  int run;
114 
115  color = bytestream_get_byte(&buf);
116  run = 1;
117 
118  if (color == 0x00) {
119  flags = bytestream_get_byte(&buf);
120  run = flags & 0x3f;
121  if (flags & 0x40)
122  run = (run << 8) + bytestream_get_byte(&buf);
123  color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
124  }
125 
126  if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) {
127  memset(sub->rects[0]->pict.data[0] + pixel_count, color, run);
128  pixel_count += run;
129  } else if (!run) {
130  /*
131  * New Line. Check if correct pixels decoded, if not display warning
132  * and adjust bitmap pointer to correct new line position.
133  */
134  if (pixel_count % sub->rects[0]->w > 0)
135  av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
136  pixel_count % sub->rects[0]->w, sub->rects[0]->w);
137  line_count++;
138  }
139  }
140 
141  if (pixel_count < sub->rects[0]->w * sub->rects[0]->h) {
142  av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
143  return -1;
144  }
145 
146  av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
147 
148  return 0;
149 }
150 
163  const uint8_t *buf, int buf_size)
164 {
165  PGSSubContext *ctx = avctx->priv_data;
166 
167  uint8_t sequence_desc;
168  unsigned int rle_bitmap_len, width, height;
169 
170  if (buf_size <= 4)
171  return -1;
172  buf_size -= 4;
173 
174  /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
175  buf += 3;
176 
177  /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
178  sequence_desc = bytestream_get_byte(&buf);
179 
180  if (!(sequence_desc & 0x80)) {
181  /* Additional RLE data */
182  if (buf_size > ctx->picture.rle_remaining_len)
183  return -1;
184 
185  memcpy(ctx->picture.rle + ctx->picture.rle_data_len, buf, buf_size);
186  ctx->picture.rle_data_len += buf_size;
187  ctx->picture.rle_remaining_len -= buf_size;
188 
189  return 0;
190  }
191 
192  if (buf_size <= 7)
193  return -1;
194  buf_size -= 7;
195 
196  /* Decode rle bitmap length, stored size includes width/height data */
197  rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
198 
199  /* Get bitmap dimensions from data */
200  width = bytestream_get_be16(&buf);
201  height = bytestream_get_be16(&buf);
202 
203  /* Make sure the bitmap is not too large */
204  if (avctx->width < width || avctx->height < height) {
205  av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
206  return -1;
207  }
208 
209  ctx->picture.w = width;
210  ctx->picture.h = height;
211 
212  av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
213 
214  if (!ctx->picture.rle)
215  return -1;
216 
217  memcpy(ctx->picture.rle, buf, buf_size);
218  ctx->picture.rle_data_len = buf_size;
219  ctx->picture.rle_remaining_len = rle_bitmap_len - buf_size;
220 
221  return 0;
222 }
223 
235  const uint8_t *buf, int buf_size)
236 {
237  PGSSubContext *ctx = avctx->priv_data;
238 
239  const uint8_t *buf_end = buf + buf_size;
240  const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
241  int color_id;
242  int y, cb, cr, alpha;
243  int r, g, b, r_add, g_add, b_add;
244 
245  /* Skip two null bytes */
246  buf += 2;
247 
248  while (buf < buf_end) {
249  color_id = bytestream_get_byte(&buf);
250  y = bytestream_get_byte(&buf);
251  cr = bytestream_get_byte(&buf);
252  cb = bytestream_get_byte(&buf);
253  alpha = bytestream_get_byte(&buf);
254 
255  YUV_TO_RGB1(cb, cr);
256  YUV_TO_RGB2(r, g, b, y);
257 
258  av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
259 
260  /* Store color in palette */
261  ctx->clut[color_id] = RGBA(r,g,b,alpha);
262  }
263 }
264 
278  const uint8_t *buf, int buf_size,
279  int64_t pts)
280 {
281  PGSSubContext *ctx = avctx->priv_data;
282 
283  int x, y, ret;
284 
285  int w = bytestream_get_be16(&buf);
286  int h = bytestream_get_be16(&buf);
287 
288  ctx->presentation.pts = pts;
289 
290  av_dlog(avctx, "Video Dimensions %dx%d\n",
291  w, h);
292  ret = ff_set_dimensions(avctx, w, h);
293  if (ret < 0)
294  return ret;
295 
296  /* Skip 1 bytes of unknown, frame rate? */
297  buf++;
298 
299  ctx->presentation.id_number = bytestream_get_be16(&buf);
300 
301  /*
302  * Skip 3 bytes of unknown:
303  * state
304  * palette_update_flag (0x80),
305  * palette_id_to_use,
306  */
307  buf += 3;
308 
309  ctx->presentation.object_number = bytestream_get_byte(&buf);
311  if (!ctx->presentation.object_number)
312  return 0;
313 
314  /*
315  * Skip 3 bytes of unknown:
316  * object_id_ref (2 bytes),
317  * window_id_ref,
318  */
319  buf += 3;
320  ctx->presentation.composition_flag = bytestream_get_byte(&buf);
321 
322  x = bytestream_get_be16(&buf);
323  y = bytestream_get_be16(&buf);
324 
325  /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
326 
327  av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
328 
329  if (x > avctx->width || y > avctx->height) {
330  av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
331  x, y, avctx->width, avctx->height);
332  x = 0; y = 0;
333  }
334 
335  /* Fill in dimensions */
336  ctx->presentation.x = x;
337  ctx->presentation.y = y;
338 
339  return 0;
340 }
341 
357 static int display_end_segment(AVCodecContext *avctx, void *data,
358  const uint8_t *buf, int buf_size)
359 {
360  AVSubtitle *sub = data;
361  PGSSubContext *ctx = avctx->priv_data;
362 
363  /*
364  * The end display time is a timeout value and is only reached
365  * if the next subtitle is later then timeout or subtitle has
366  * not been cleared by a subsequent empty display command.
367  */
368 
369  memset(sub, 0, sizeof(*sub));
370  sub->pts = ctx->presentation.pts;
371 
372  // Blank if last object_number was 0.
373  // Note that this may be wrong for more complex subtitles.
374  if (!ctx->presentation.object_number)
375  return 1;
376  sub->start_display_time = 0;
377  sub->end_display_time = 20000;
378  sub->format = 0;
379 
380  sub->rects = av_mallocz(sizeof(*sub->rects));
381  sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
382  sub->num_rects = 1;
383 
384  if (ctx->presentation.composition_flag & 0x40)
385  sub->rects[0]->flags |= AV_SUBTITLE_FLAG_FORCED;
386 
387  sub->rects[0]->x = ctx->presentation.x;
388  sub->rects[0]->y = ctx->presentation.y;
389  sub->rects[0]->w = ctx->picture.w;
390  sub->rects[0]->h = ctx->picture.h;
391  sub->rects[0]->type = SUBTITLE_BITMAP;
392 
393  /* Process bitmap */
394  sub->rects[0]->pict.linesize[0] = ctx->picture.w;
395 
396  if (ctx->picture.rle) {
397  if (ctx->picture.rle_remaining_len)
398  av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
400  if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
401  return 0;
402  }
403  /* Allocate memory for colors */
404  sub->rects[0]->nb_colors = 256;
405  sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
406 
407  memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
408 
409  return 1;
410 }
411 
412 static int decode(AVCodecContext *avctx, void *data, int *data_size,
413  AVPacket *avpkt)
414 {
415  const uint8_t *buf = avpkt->data;
416  int buf_size = avpkt->size;
417 
418  const uint8_t *buf_end;
419  uint8_t segment_type;
420  int segment_length;
421  int i, ret;
422 
423  av_dlog(avctx, "PGS sub packet:\n");
424 
425  for (i = 0; i < buf_size; i++) {
426  av_dlog(avctx, "%02x ", buf[i]);
427  if (i % 16 == 15)
428  av_dlog(avctx, "\n");
429  }
430 
431  if (i & 15)
432  av_dlog(avctx, "\n");
433 
434  *data_size = 0;
435 
436  /* Ensure that we have received at a least a segment code and segment length */
437  if (buf_size < 3)
438  return -1;
439 
440  buf_end = buf + buf_size;
441 
442  /* Step through buffer to identify segments */
443  while (buf < buf_end) {
444  segment_type = bytestream_get_byte(&buf);
445  segment_length = bytestream_get_be16(&buf);
446 
447  av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
448 
449  if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
450  break;
451 
452  switch (segment_type) {
453  case PALETTE_SEGMENT:
454  parse_palette_segment(avctx, buf, segment_length);
455  break;
456  case PICTURE_SEGMENT:
457  parse_picture_segment(avctx, buf, segment_length);
458  break;
460  ret = parse_presentation_segment(avctx, buf, segment_length, avpkt->pts);
461  if (ret < 0)
462  return ret;
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  .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
492  .type = AVMEDIA_TYPE_SUBTITLE,
494  .priv_data_size = sizeof(PGSSubContext),
495  .init = init_decoder,
496  .close = close_decoder,
497  .decode = decode,
498 };
#define AVPALETTE_SIZE
Definition: avcodec.h:2959
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:62
#define ff_cropTbl
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:2952
int x
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:2983
misc image utilities
static int parse_presentation_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int64_t pts)
Parse the presentation segment packet.
Definition: pgssubdec.c:277
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.
Definition: utils.c:144
unsigned int rle_remaining_len
Definition: pgssubdec.c:59
#define MAX_NEG_CROP
Definition: dsputil.h:44
int nb_colors
number of colors in pict, undefined when pict is not set
Definition: avcodec.h:2987
int size
Definition: avcodec.h:974
Various defines for YUV<->RGB conversion.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
unsigned num_rects
Definition: avcodec.h:3011
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:142
AVCodec ff_pgssub_decoder
Definition: pgssubdec.c:489
PGSSubPresentation presentation
Definition: pgssubdec.c:63
AVCodec.
Definition: avcodec.h:2755
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:198
AVSubtitleRect ** rects
Definition: avcodec.h:3012
int w
width of pict, undefined when pict is not set
Definition: avcodec.h:2985
uint8_t
#define av_cold
Definition: attributes.h:66
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: avcodec.h:2951
#define b
Definition: input.c:52
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
static int decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: pgssubdec.c:412
int h
height of pict, undefined when pict is not set
Definition: avcodec.h:2986
uint32_t clut[256]
Definition: pgssubdec.c:64
#define cm
Definition: dvbsubdec.c:34
#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:162
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define RGBA(r, g, b, a)
Definition: pgssubdec.c:35
#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:2984
unsigned int rle_buffer_size
Definition: pgssubdec.c:58
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
g
Definition: yuv2rgb.c:535
#define YUV_TO_RGB1(cb1, cr1)
Definition: colorspace.h:52
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
uint32_t end_display_time
Definition: avcodec.h:3010
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:3013
A bitmap, pict will be set.
Definition: avcodec.h:2965
#define AV_SUBTITLE_FLAG_FORCED
Definition: avcodec.h:2980
AVPicture pict
data+linesize for the bitmap of this subtitle.
Definition: avcodec.h:2993
static av_cold int close_decoder(AVCodecContext *avctx)
Definition: pgssubdec.c:75
int width
picture width / height.
Definition: avcodec.h:1217
uint16_t format
Definition: avcodec.h:3008
static av_cold int init_decoder(AVCodecContext *avctx)
Definition: pgssubdec.c:68
static int display_end_segment(AVCodecContext *avctx, void *data, const uint8_t *buf, int buf_size)
Parse the display segment packet.
Definition: pgssubdec.c:357
static int width
Definition: utils.c:156
Libavcodec 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:95
main external API structure.
Definition: avcodec.h:1054
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
PGSSubPicture picture
Definition: pgssubdec.c:65
int height
Definition: gxfenc.c:72
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:368
common internal api header.
uint32_t start_display_time
Definition: avcodec.h:3009
static void parse_palette_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the palette segment packet.
Definition: pgssubdec.c:234
static const uint8_t color[]
Definition: log.c:54
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
DSP utils.
void * priv_data
Definition: avcodec.h:1090
uint8_t * rle
Definition: pgssubdec.c:57
SegmentType
Definition: pgssubdec.c:37
unsigned int rle_data_len
Definition: pgssubdec.c:58
uint8_t composition_flag
Definition: pgssubdec.c:50
enum AVSubtitleType type
Definition: avcodec.h:2994
This structure stores compressed data.
Definition: avcodec.h:950
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:205
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966