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 
50 typedef struct PGSSubPicture {
51  int w;
52  int h;
53  uint8_t *rle;
55  unsigned int rle_remaining_len;
57 
58 typedef struct PGSSubContext {
60  uint32_t clut[256];
63 
65 {
66  avctx->pix_fmt = PIX_FMT_PAL8;
67 
68  return 0;
69 }
70 
72 {
73  PGSSubContext *ctx = avctx->priv_data;
74 
75  av_freep(&ctx->picture.rle);
76  ctx->picture.rle_buffer_size = 0;
77 
78  return 0;
79 }
80 
91 static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub,
92  const uint8_t *buf, unsigned int buf_size)
93 {
94  const uint8_t *rle_bitmap_end;
95  int pixel_count, line_count;
96 
97  rle_bitmap_end = buf + buf_size;
98 
99  sub->rects[0]->pict.data[0] = av_malloc(sub->rects[0]->w * sub->rects[0]->h);
100 
101  if (!sub->rects[0]->pict.data[0])
102  return -1;
103 
104  pixel_count = 0;
105  line_count = 0;
106 
107  while (buf < rle_bitmap_end && line_count < sub->rects[0]->h) {
108  uint8_t flags, color;
109  int run;
110 
111  color = bytestream_get_byte(&buf);
112  run = 1;
113 
114  if (color == 0x00) {
115  flags = bytestream_get_byte(&buf);
116  run = flags & 0x3f;
117  if (flags & 0x40)
118  run = (run << 8) + bytestream_get_byte(&buf);
119  color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
120  }
121 
122  if (run > 0 && pixel_count + run <= sub->rects[0]->w * sub->rects[0]->h) {
123  memset(sub->rects[0]->pict.data[0] + pixel_count, color, run);
124  pixel_count += run;
125  } else if (!run) {
126  /*
127  * New Line. Check if correct pixels decoded, if not display warning
128  * and adjust bitmap pointer to correct new line position.
129  */
130  if (pixel_count % sub->rects[0]->w > 0)
131  av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
132  pixel_count % sub->rects[0]->w, sub->rects[0]->w);
133  line_count++;
134  }
135  }
136 
137  if (pixel_count < sub->rects[0]->w * sub->rects[0]->h) {
138  av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
139  return -1;
140  }
141 
142  av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[0]->w * sub->rects[0]->h);
143 
144  return 0;
145 }
146 
159  const uint8_t *buf, int buf_size)
160 {
161  PGSSubContext *ctx = avctx->priv_data;
162 
163  uint8_t sequence_desc;
164  unsigned int rle_bitmap_len, width, height;
165 
166  if (buf_size <= 4)
167  return -1;
168  buf_size -= 4;
169 
170  /* skip 3 unknown bytes: Object ID (2 bytes), Version Number */
171  buf += 3;
172 
173  /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
174  sequence_desc = bytestream_get_byte(&buf);
175 
176  if (!(sequence_desc & 0x80)) {
177  /* Additional RLE data */
178  if (buf_size > ctx->picture.rle_remaining_len)
179  return -1;
180 
181  memcpy(ctx->picture.rle + ctx->picture.rle_data_len, buf, buf_size);
182  ctx->picture.rle_data_len += buf_size;
183  ctx->picture.rle_remaining_len -= buf_size;
184 
185  return 0;
186  }
187 
188  if (buf_size <= 7)
189  return -1;
190  buf_size -= 7;
191 
192  /* Decode rle bitmap length, stored size includes width/height data */
193  rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
194 
195  if (buf_size > rle_bitmap_len) {
196  av_log(avctx, AV_LOG_ERROR,
197  "Buffer dimension %d larger than the expected RLE data %d\n",
198  buf_size, rle_bitmap_len);
199  return AVERROR_INVALIDDATA;
200  }
201 
202  /* Get bitmap dimensions from data */
203  width = bytestream_get_be16(&buf);
204  height = bytestream_get_be16(&buf);
205 
206  /* Make sure the bitmap is not too large */
207  if (avctx->width < width || avctx->height < height) {
208  av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
209  return -1;
210  }
211 
212  ctx->picture.w = width;
213  ctx->picture.h = height;
214 
215  av_fast_malloc(&ctx->picture.rle, &ctx->picture.rle_buffer_size, rle_bitmap_len);
216 
217  if (!ctx->picture.rle)
218  return -1;
219 
220  memcpy(ctx->picture.rle, buf, buf_size);
221  ctx->picture.rle_data_len = buf_size;
222  ctx->picture.rle_remaining_len = rle_bitmap_len - buf_size;
223 
224  return 0;
225 }
226 
238  const uint8_t *buf, int buf_size)
239 {
240  PGSSubContext *ctx = avctx->priv_data;
241 
242  const uint8_t *buf_end = buf + buf_size;
243  const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
244  int color_id;
245  int y, cb, cr, alpha;
246  int r, g, b, r_add, g_add, b_add;
247 
248  /* Skip two null bytes */
249  buf += 2;
250 
251  while (buf < buf_end) {
252  color_id = bytestream_get_byte(&buf);
253  y = bytestream_get_byte(&buf);
254  cr = bytestream_get_byte(&buf);
255  cb = bytestream_get_byte(&buf);
256  alpha = bytestream_get_byte(&buf);
257 
258  YUV_TO_RGB1(cb, cr);
259  YUV_TO_RGB2(r, g, b, y);
260 
261  av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
262 
263  /* Store color in palette */
264  ctx->clut[color_id] = RGBA(r,g,b,alpha);
265  }
266 }
267 
281  const uint8_t *buf, int buf_size)
282 {
283  PGSSubContext *ctx = avctx->priv_data;
284 
285  int x, y;
286 
287  int w = bytestream_get_be16(&buf);
288  int h = bytestream_get_be16(&buf);
289 
290  av_dlog(avctx, "Video Dimensions %dx%d\n",
291  w, h);
292  if (av_image_check_size(w, h, 0, avctx) >= 0)
293  avcodec_set_dimensions(avctx, w, h);
294 
295  /* Skip 1 bytes of unknown, frame rate? */
296  buf++;
297 
298  ctx->presentation.id_number = bytestream_get_be16(&buf);
299 
300  /*
301  * Skip 3 bytes of unknown:
302  * state
303  * palette_update_flag (0x80),
304  * palette_id_to_use,
305  */
306  buf += 3;
307 
308  ctx->presentation.object_number = bytestream_get_byte(&buf);
309  if (!ctx->presentation.object_number)
310  return;
311 
312  /*
313  * Skip 4 bytes of unknown:
314  * object_id_ref (2 bytes),
315  * window_id_ref,
316  * composition_flag (0x80 - object cropped, 0x40 - object forced)
317  */
318  buf += 4;
319 
320  x = bytestream_get_be16(&buf);
321  y = bytestream_get_be16(&buf);
322 
323  /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
324 
325  av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
326 
327  if (x > avctx->width || y > avctx->height) {
328  av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
329  x, y, avctx->width, avctx->height);
330  x = 0; y = 0;
331  }
332 
333  /* Fill in dimensions */
334  ctx->presentation.x = x;
335  ctx->presentation.y = y;
336 }
337 
353 static int display_end_segment(AVCodecContext *avctx, void *data,
354  const uint8_t *buf, int buf_size)
355 {
356  AVSubtitle *sub = data;
357  PGSSubContext *ctx = avctx->priv_data;
358 
359  /*
360  * The end display time is a timeout value and is only reached
361  * if the next subtitle is later then timeout or subtitle has
362  * not been cleared by a subsequent empty display command.
363  */
364 
365  memset(sub, 0, sizeof(*sub));
366  // Blank if last object_number was 0.
367  // Note that this may be wrong for more complex subtitles.
368  if (!ctx->presentation.object_number)
369  return 1;
370  sub->start_display_time = 0;
371  sub->end_display_time = 20000;
372  sub->format = 0;
373 
374  sub->rects = av_mallocz(sizeof(*sub->rects));
375  sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
376  sub->num_rects = 1;
377 
378  sub->rects[0]->x = ctx->presentation.x;
379  sub->rects[0]->y = ctx->presentation.y;
380  sub->rects[0]->w = ctx->picture.w;
381  sub->rects[0]->h = ctx->picture.h;
382  sub->rects[0]->type = SUBTITLE_BITMAP;
383 
384  /* Process bitmap */
385  sub->rects[0]->pict.linesize[0] = ctx->picture.w;
386 
387  if (ctx->picture.rle) {
388  if (ctx->picture.rle_remaining_len)
389  av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
391  if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
392  return 0;
393  }
394  /* Allocate memory for colors */
395  sub->rects[0]->nb_colors = 256;
396  sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
397 
398  memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
399 
400  return 1;
401 }
402 
403 static int decode(AVCodecContext *avctx, void *data, int *data_size,
404  AVPacket *avpkt)
405 {
406  const uint8_t *buf = avpkt->data;
407  int buf_size = avpkt->size;
408 
409  const uint8_t *buf_end;
410  uint8_t segment_type;
411  int segment_length;
412  int i;
413 
414  av_dlog(avctx, "PGS sub packet:\n");
415 
416  for (i = 0; i < buf_size; i++) {
417  av_dlog(avctx, "%02x ", buf[i]);
418  if (i % 16 == 15)
419  av_dlog(avctx, "\n");
420  }
421 
422  if (i & 15)
423  av_dlog(avctx, "\n");
424 
425  *data_size = 0;
426 
427  /* Ensure that we have received at a least a segment code and segment length */
428  if (buf_size < 3)
429  return -1;
430 
431  buf_end = buf + buf_size;
432 
433  /* Step through buffer to identify segments */
434  while (buf < buf_end) {
435  segment_type = bytestream_get_byte(&buf);
436  segment_length = bytestream_get_be16(&buf);
437 
438  av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
439 
440  if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
441  break;
442 
443  switch (segment_type) {
444  case PALETTE_SEGMENT:
445  parse_palette_segment(avctx, buf, segment_length);
446  break;
447  case PICTURE_SEGMENT:
448  parse_picture_segment(avctx, buf, segment_length);
449  break;
451  parse_presentation_segment(avctx, buf, segment_length);
452  break;
453  case WINDOW_SEGMENT:
454  /*
455  * Window Segment Structure (No new information provided):
456  * 2 bytes: Unkown,
457  * 2 bytes: X position of subtitle,
458  * 2 bytes: Y position of subtitle,
459  * 2 bytes: Width of subtitle,
460  * 2 bytes: Height of subtitle.
461  */
462  break;
463  case DISPLAY_SEGMENT:
464  *data_size = display_end_segment(avctx, data, buf, segment_length);
465  break;
466  default:
467  av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
468  segment_type, segment_length);
469  break;
470  }
471 
472  buf += segment_length;
473  }
474 
475  return buf_size;
476 }
477 
479  .name = "pgssub",
480  .type = AVMEDIA_TYPE_SUBTITLE,
482  .priv_data_size = sizeof(PGSSubContext),
483  .init = init_decoder,
484  .close = close_decoder,
485  .decode = decode,
486  .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
487 };