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  if (buf_size > rle_bitmap_len) {
200  av_log(avctx, AV_LOG_ERROR,
201  "Buffer dimension %d larger than the expected RLE data %d\n",
202  buf_size, rle_bitmap_len);
203  return AVERROR_INVALIDDATA;
204  }
205 
206  /* Get bitmap dimensions from data */
207  width = bytestream_get_be16(&buf);
208  height = bytestream_get_be16(&buf);
209 
210  /* Make sure the bitmap is not too large */
211  if (avctx->width < width || avctx->height < height) {
212  av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
213  return -1;
214  }
215 
216  ctx->picture.w = width;
217  ctx->picture.h = height;
218 
219  av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
220 
221  if (!ctx->picture.rle)
222  return -1;
223 
224  memcpy(ctx->picture.rle, buf, buf_size);
225  ctx->picture.rle_data_len = buf_size;
226  ctx->picture.rle_remaining_len = rle_bitmap_len - buf_size;
227 
228  return 0;
229 }
230 
242  const uint8_t *buf, int buf_size)
243 {
244  PGSSubContext *ctx = avctx->priv_data;
245 
246  const uint8_t *buf_end = buf + buf_size;
247  const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
248  int color_id;
249  int y, cb, cr, alpha;
250  int r, g, b, r_add, g_add, b_add;
251 
252  /* Skip two null bytes */
253  buf += 2;
254 
255  while (buf < buf_end) {
256  color_id = bytestream_get_byte(&buf);
257  y = bytestream_get_byte(&buf);
258  cr = bytestream_get_byte(&buf);
259  cb = bytestream_get_byte(&buf);
260  alpha = bytestream_get_byte(&buf);
261 
262  YUV_TO_RGB1(cb, cr);
263  YUV_TO_RGB2(r, g, b, y);
264 
265  av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
266 
267  /* Store color in palette */
268  ctx->clut[color_id] = RGBA(r,g,b,alpha);
269  }
270 }
271 
285  const uint8_t *buf, int buf_size,
286  int64_t pts)
287 {
288  PGSSubContext *ctx = avctx->priv_data;
289 
290  int x, y, ret;
291 
292  int w = bytestream_get_be16(&buf);
293  int h = bytestream_get_be16(&buf);
294 
295  ctx->presentation.pts = pts;
296 
297  av_dlog(avctx, "Video Dimensions %dx%d\n",
298  w, h);
299  ret = ff_set_dimensions(avctx, w, h);
300  if (ret < 0)
301  return ret;
302 
303  /* Skip 1 bytes of unknown, frame rate? */
304  buf++;
305 
306  ctx->presentation.id_number = bytestream_get_be16(&buf);
307 
308  /*
309  * Skip 3 bytes of unknown:
310  * state
311  * palette_update_flag (0x80),
312  * palette_id_to_use,
313  */
314  buf += 3;
315 
316  ctx->presentation.object_number = bytestream_get_byte(&buf);
318  if (!ctx->presentation.object_number)
319  return 0;
320 
321  /*
322  * Skip 3 bytes of unknown:
323  * object_id_ref (2 bytes),
324  * window_id_ref,
325  */
326  buf += 3;
327  ctx->presentation.composition_flag = bytestream_get_byte(&buf);
328 
329  x = bytestream_get_be16(&buf);
330  y = bytestream_get_be16(&buf);
331 
332  /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
333 
334  av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
335 
336  if (x > avctx->width || y > avctx->height) {
337  av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
338  x, y, avctx->width, avctx->height);
339  x = 0; y = 0;
340  }
341 
342  /* Fill in dimensions */
343  ctx->presentation.x = x;
344  ctx->presentation.y = y;
345 
346  return 0;
347 }
348 
364 static int display_end_segment(AVCodecContext *avctx, void *data,
365  const uint8_t *buf, int buf_size)
366 {
367  AVSubtitle *sub = data;
368  PGSSubContext *ctx = avctx->priv_data;
369 
370  /*
371  * The end display time is a timeout value and is only reached
372  * if the next subtitle is later then timeout or subtitle has
373  * not been cleared by a subsequent empty display command.
374  */
375 
376  memset(sub, 0, sizeof(*sub));
377  sub->pts = ctx->presentation.pts;
378 
379  // Blank if last object_number was 0.
380  // Note that this may be wrong for more complex subtitles.
381  if (!ctx->presentation.object_number)
382  return 1;
383  sub->start_display_time = 0;
384  sub->end_display_time = 20000;
385  sub->format = 0;
386 
387  sub->rects = av_mallocz(sizeof(*sub->rects));
388  sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
389  sub->num_rects = 1;
390 
391  if (ctx->presentation.composition_flag & 0x40)
392  sub->rects[0]->flags |= AV_SUBTITLE_FLAG_FORCED;
393 
394  sub->rects[0]->x = ctx->presentation.x;
395  sub->rects[0]->y = ctx->presentation.y;
396  sub->rects[0]->w = ctx->picture.w;
397  sub->rects[0]->h = ctx->picture.h;
398  sub->rects[0]->type = SUBTITLE_BITMAP;
399 
400  /* Process bitmap */
401  sub->rects[0]->pict.linesize[0] = ctx->picture.w;
402 
403  if (ctx->picture.rle) {
404  if (ctx->picture.rle_remaining_len)
405  av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
407  if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
408  return 0;
409  }
410  /* Allocate memory for colors */
411  sub->rects[0]->nb_colors = 256;
412  sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
413 
414  memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
415 
416  return 1;
417 }
418 
419 static int decode(AVCodecContext *avctx, void *data, int *data_size,
420  AVPacket *avpkt)
421 {
422  const uint8_t *buf = avpkt->data;
423  int buf_size = avpkt->size;
424 
425  const uint8_t *buf_end;
426  uint8_t segment_type;
427  int segment_length;
428  int i, ret;
429 
430  av_dlog(avctx, "PGS sub packet:\n");
431 
432  for (i = 0; i < buf_size; i++) {
433  av_dlog(avctx, "%02x ", buf[i]);
434  if (i % 16 == 15)
435  av_dlog(avctx, "\n");
436  }
437 
438  if (i & 15)
439  av_dlog(avctx, "\n");
440 
441  *data_size = 0;
442 
443  /* Ensure that we have received at a least a segment code and segment length */
444  if (buf_size < 3)
445  return -1;
446 
447  buf_end = buf + buf_size;
448 
449  /* Step through buffer to identify segments */
450  while (buf < buf_end) {
451  segment_type = bytestream_get_byte(&buf);
452  segment_length = bytestream_get_be16(&buf);
453 
454  av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
455 
456  if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
457  break;
458 
459  switch (segment_type) {
460  case PALETTE_SEGMENT:
461  parse_palette_segment(avctx, buf, segment_length);
462  break;
463  case PICTURE_SEGMENT:
464  parse_picture_segment(avctx, buf, segment_length);
465  break;
467  ret = parse_presentation_segment(avctx, buf, segment_length, avpkt->pts);
468  if (ret < 0)
469  return ret;
470  break;
471  case WINDOW_SEGMENT:
472  /*
473  * Window Segment Structure (No new information provided):
474  * 2 bytes: Unknown,
475  * 2 bytes: X position of subtitle,
476  * 2 bytes: Y position of subtitle,
477  * 2 bytes: Width of subtitle,
478  * 2 bytes: Height of subtitle.
479  */
480  break;
481  case DISPLAY_SEGMENT:
482  *data_size = display_end_segment(avctx, data, buf, segment_length);
483  break;
484  default:
485  av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
486  segment_type, segment_length);
487  break;
488  }
489 
490  buf += segment_length;
491  }
492 
493  return buf_size;
494 }
495 
497  .name = "pgssub",
498  .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
499  .type = AVMEDIA_TYPE_SUBTITLE,
501  .priv_data_size = sizeof(PGSSubContext),
502  .init = init_decoder,
503  .close = close_decoder,
504  .decode = decode,
505 };