FFmpeg  4.4.9
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 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 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30 #include "mathops.h"
31 
32 #include "libavutil/colorspace.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/opt.h"
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 
49 typedef struct PGSSubObjectRef {
50  int id;
51  int window_id;
53  int x;
54  int y;
55  int crop_x;
56  int crop_y;
57  int crop_w;
58  int crop_h;
60 
61 typedef struct PGSSubPresentation {
62  int id_number;
68 
69 typedef struct PGSSubObject {
70  int id;
71  int w;
72  int h;
75  unsigned int rle_remaining_len;
76 } PGSSubObject;
77 
78 typedef struct PGSSubObjects {
79  int count;
82 
83 typedef struct PGSSubPalette {
84  int id;
85  uint32_t clut[256];
87 
88 typedef struct PGSSubPalettes {
89  int count;
92 
93 typedef struct PGSSubContext {
94  AVClass *class;
100 
101 static void flush_cache(AVCodecContext *avctx)
102 {
103  PGSSubContext *ctx = avctx->priv_data;
104  int i;
105 
106  for (i = 0; i < ctx->objects.count; i++) {
107  av_freep(&ctx->objects.object[i].rle);
108  ctx->objects.object[i].rle_buffer_size = 0;
109  ctx->objects.object[i].rle_remaining_len = 0;
110  }
111  ctx->objects.count = 0;
112  ctx->palettes.count = 0;
113 }
114 
115 static PGSSubObject * find_object(int id, PGSSubObjects *objects)
116 {
117  int i;
118 
119  for (i = 0; i < objects->count; i++) {
120  if (objects->object[i].id == id)
121  return &objects->object[i];
122  }
123  return NULL;
124 }
125 
126 static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
127 {
128  int i;
129 
130  for (i = 0; i < palettes->count; i++) {
131  if (palettes->palette[i].id == id)
132  return &palettes->palette[i];
133  }
134  return NULL;
135 }
136 
138 {
139  avctx->pix_fmt = AV_PIX_FMT_PAL8;
140 
141  return 0;
142 }
143 
145 {
146  flush_cache(avctx);
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  */
162  const uint8_t *buf, unsigned int buf_size)
163 {
164  const uint8_t *rle_bitmap_end;
165  int pixel_count, line_count;
166 
167  rle_bitmap_end = buf + buf_size;
168 
169  rect->data[0] = av_malloc_array(rect->w, rect->h);
170 
171  if (!rect->data[0])
172  return AVERROR(ENOMEM);
173 
174  pixel_count = 0;
175  line_count = 0;
176 
177  while (buf < rle_bitmap_end && line_count < rect->h) {
179  int run;
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);
194  pixel_count += 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) {
201  av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
202  pixel_count % rect->w, rect->w);
203  if (avctx->err_recognition & AV_EF_EXPLODE) {
204  return AVERROR_INVALIDDATA;
205  }
206  }
207  line_count++;
208  }
209  }
210 
211  if (pixel_count < rect->w * rect->h) {
212  av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
213  return AVERROR_INVALIDDATA;
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  */
232  const uint8_t *buf, int buf_size)
233 {
234  PGSSubContext *ctx = avctx->priv_data;
235  PGSSubObject *object;
236 
237  uint8_t sequence_desc;
238  int rle_bitmap_len, width, height;
239  int id;
240 
241  if (buf_size <= 4)
242  return AVERROR_INVALIDDATA;
243  buf_size -= 4;
244 
245  id = bytestream_get_be16(&buf);
246  object = find_object(id, &ctx->objects);
247  if (!object) {
248  if (ctx->objects.count >= MAX_EPOCH_OBJECTS) {
249  av_log(avctx, AV_LOG_ERROR, "Too many objects in epoch\n");
250  return AVERROR_INVALIDDATA;
251  }
252  object = &ctx->objects.object[ctx->objects.count++];
253  object->id = id;
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 */
264  if (buf_size > object->rle_remaining_len)
265  return AVERROR_INVALIDDATA;
266 
267  uint8_t *rle = av_fast_realloc(object->rle, &object->rle_buffer_size,
268  object->rle_data_len + buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
269  if (!rle)
270  return AVERROR(ENOMEM);
271  object->rle = rle;
272 
273  memcpy(object->rle + object->rle_data_len, buf, buf_size);
274  memset(object->rle + object->rle_data_len + buf_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
275  object->rle_data_len += buf_size;
276  object->rle_remaining_len -= buf_size;
277 
278  return 0;
279  }
280 
281  if (buf_size <= 7)
282  return AVERROR_INVALIDDATA;
283  buf_size -= 7;
284 
285  /* Decode rle bitmap length, stored size includes width/height data */
286  rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
287 
288  if (buf_size > rle_bitmap_len) {
289  av_log(avctx, AV_LOG_ERROR,
290  "Buffer dimension %d larger than the expected RLE data %d\n",
291  buf_size, rle_bitmap_len);
292  return AVERROR_INVALIDDATA;
293  }
294 
295  /* Get bitmap dimensions from data */
296  width = bytestream_get_be16(&buf);
297  height = bytestream_get_be16(&buf);
298 
299  /* Make sure the bitmap is not too large */
300  if (avctx->width < width || avctx->height < height || !width || !height) {
301  av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions (%dx%d) invalid.\n", width, height);
302  return AVERROR_INVALIDDATA;
303  }
304 
305  object->w = width;
306  object->h = height;
307 
308  av_fast_padded_malloc(&object->rle, &object->rle_buffer_size, buf_size);
309 
310  if (!object->rle) {
311  object->rle_data_len = 0;
312  object->rle_remaining_len = 0;
313  return AVERROR(ENOMEM);
314  }
315 
316  memcpy(object->rle, buf, buf_size);
317  object->rle_data_len = buf_size;
318  object->rle_remaining_len = rle_bitmap_len - buf_size;
319 
320  return 0;
321 }
322 
323 /**
324  * Parse the palette segment packet.
325  *
326  * The palette segment contains details of the palette,
327  * a maximum of 256 colors can be defined.
328  *
329  * @param avctx contains the current codec context
330  * @param buf pointer to the packet to process
331  * @param buf_size size of packet to process
332  */
334  const uint8_t *buf, int buf_size)
335 {
336  PGSSubContext *ctx = avctx->priv_data;
337  PGSSubPalette *palette;
338 
339  const uint8_t *buf_end = buf + buf_size;
340  const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
341  int color_id;
342  int y, cb, cr, alpha;
343  int r, g, b, r_add, g_add, b_add;
344  int id;
345 
346  id = bytestream_get_byte(&buf);
347  palette = find_palette(id, &ctx->palettes);
348  if (!palette) {
349  if (ctx->palettes.count >= MAX_EPOCH_PALETTES) {
350  av_log(avctx, AV_LOG_ERROR, "Too many palettes in epoch\n");
351  return AVERROR_INVALIDDATA;
352  }
353  palette = &ctx->palettes.palette[ctx->palettes.count++];
354  palette->id = id;
355  }
356 
357  /* Skip palette version */
358  buf += 1;
359 
360  while (buf < buf_end) {
361  color_id = bytestream_get_byte(&buf);
362  y = bytestream_get_byte(&buf);
363  cr = bytestream_get_byte(&buf);
364  cb = bytestream_get_byte(&buf);
365  alpha = bytestream_get_byte(&buf);
366 
367  /* Default to BT.709 colorspace. In case of <= 576 height use BT.601 */
368  if (avctx->height <= 0 || avctx->height > 576) {
370  } else {
372  }
373 
374  YUV_TO_RGB2_CCIR(r, g, b, y);
375 
376  ff_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
377 
378  /* Store color in palette */
379  palette->clut[color_id] = RGBA(r,g,b,alpha);
380  }
381  return 0;
382 }
383 
384 /**
385  * Parse the presentation segment packet.
386  *
387  * The presentation segment contains details on the video
388  * width, video height, x & y subtitle position.
389  *
390  * @param avctx contains the current codec context
391  * @param buf pointer to the packet to process
392  * @param buf_size size of packet to process
393  * @todo TODO: Implement cropping
394  */
396  const uint8_t *buf, int buf_size,
397  int64_t pts)
398 {
399  PGSSubContext *ctx = avctx->priv_data;
400  int i, state, ret;
401  const uint8_t *buf_end = buf + buf_size;
402 
403  // Video descriptor
404  int w = bytestream_get_be16(&buf);
405  int h = bytestream_get_be16(&buf);
406 
407  ctx->presentation.pts = pts;
408 
409  ff_dlog(avctx, "Video Dimensions %dx%d\n",
410  w, h);
411  ret = ff_set_dimensions(avctx, w, h);
412  if (ret < 0)
413  return ret;
414 
415  /* Skip 1 bytes of unknown, frame rate */
416  buf++;
417 
418  // Composition descriptor
419  ctx->presentation.id_number = bytestream_get_be16(&buf);
420  /*
421  * state is a 2 bit field that defines pgs epoch boundaries
422  * 00 - Normal, previously defined objects and palettes are still valid
423  * 01 - Acquisition point, previous objects and palettes can be released
424  * 10 - Epoch start, previous objects and palettes can be released
425  * 11 - Epoch continue, previous objects and palettes can be released
426  *
427  * reserved 6 bits discarded
428  */
429  state = bytestream_get_byte(&buf) >> 6;
430  if (state != 0) {
431  flush_cache(avctx);
432  }
433 
434  /*
435  * skip palette_update_flag (0x80),
436  */
437  buf += 1;
438  ctx->presentation.palette_id = bytestream_get_byte(&buf);
439  ctx->presentation.object_count = bytestream_get_byte(&buf);
440  if (ctx->presentation.object_count > MAX_OBJECT_REFS) {
441  av_log(avctx, AV_LOG_ERROR,
442  "Invalid number of presentation objects %d\n",
443  ctx->presentation.object_count);
444  ctx->presentation.object_count = 2;
445  if (avctx->err_recognition & AV_EF_EXPLODE) {
446  return AVERROR_INVALIDDATA;
447  }
448  }
449 
450 
451  for (i = 0; i < ctx->presentation.object_count; i++)
452  {
453 
454  if (buf_end - buf < 8) {
455  av_log(avctx, AV_LOG_ERROR, "Insufficent space for object\n");
456  ctx->presentation.object_count = i;
457  return AVERROR_INVALIDDATA;
458  }
459 
460  ctx->presentation.objects[i].id = bytestream_get_be16(&buf);
461  ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf);
462  ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf);
463 
464  ctx->presentation.objects[i].x = bytestream_get_be16(&buf);
465  ctx->presentation.objects[i].y = bytestream_get_be16(&buf);
466 
467  // If cropping
468  if (ctx->presentation.objects[i].composition_flag & 0x80) {
469  ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf);
470  ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf);
471  ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf);
472  ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf);
473  }
474 
475  ff_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",
476  ctx->presentation.objects[i].x, ctx->presentation.objects[i].y);
477 
478  if (ctx->presentation.objects[i].x > avctx->width ||
479  ctx->presentation.objects[i].y > avctx->height) {
480  av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
481  ctx->presentation.objects[i].x,
482  ctx->presentation.objects[i].y,
483  avctx->width, avctx->height);
484  ctx->presentation.objects[i].x = 0;
485  ctx->presentation.objects[i].y = 0;
486  if (avctx->err_recognition & AV_EF_EXPLODE) {
487  return AVERROR_INVALIDDATA;
488  }
489  }
490  }
491 
492  return 0;
493 }
494 
495 /**
496  * Parse the display segment packet.
497  *
498  * The display segment controls the updating of the display.
499  *
500  * @param avctx contains the current codec context
501  * @param data pointer to the data pertaining the subtitle to display
502  * @param buf pointer to the packet to process
503  * @param buf_size size of packet to process
504  */
505 static int display_end_segment(AVCodecContext *avctx, void *data,
506  const uint8_t *buf, int buf_size)
507 {
508  AVSubtitle *sub = data;
509  PGSSubContext *ctx = avctx->priv_data;
510  int64_t pts;
511  PGSSubPalette *palette;
512  int i, ret;
513 
514  pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts;
515  memset(sub, 0, sizeof(*sub));
516  sub->pts = pts;
517  ctx->presentation.pts = AV_NOPTS_VALUE;
518  sub->start_display_time = 0;
519  // There is no explicit end time for PGS subtitles. The end time
520  // is defined by the start of the next sub which may contain no
521  // objects (i.e. clears the previous sub)
522  sub->end_display_time = UINT32_MAX;
523  sub->format = 0;
524 
525  // Blank if last object_count was 0.
526  if (!ctx->presentation.object_count)
527  return 1;
528  sub->rects = av_mallocz_array(ctx->presentation.object_count, sizeof(*sub->rects));
529  if (!sub->rects) {
530  return AVERROR(ENOMEM);
531  }
532  palette = find_palette(ctx->presentation.palette_id, &ctx->palettes);
533  if (!palette) {
534  // Missing palette. Should only happen with damaged streams.
535  av_log(avctx, AV_LOG_ERROR, "Invalid palette id %d\n",
536  ctx->presentation.palette_id);
538  return AVERROR_INVALIDDATA;
539  }
540  for (i = 0; i < ctx->presentation.object_count; i++) {
541  PGSSubObject *object;
542 
543  sub->rects[i] = av_mallocz(sizeof(*sub->rects[0]));
544  if (!sub->rects[i]) {
546  return AVERROR(ENOMEM);
547  }
548  sub->num_rects++;
549  sub->rects[i]->type = SUBTITLE_BITMAP;
550 
551  /* Allocate the palette now so that the error paths below, which
552  * leave the rect empty, still hand consumers a complete bitmap
553  * rect rather than one with a NULL palette. */
554  sub->rects[i]->nb_colors = 256;
555  sub->rects[i]->data[1] = av_mallocz(AVPALETTE_SIZE);
556  if (!sub->rects[i]->data[1]) {
558  return AVERROR(ENOMEM);
559  }
560  if (!ctx->forced_subs_only || ctx->presentation.objects[i].composition_flag & 0x40)
561  memcpy(sub->rects[i]->data[1], palette->clut, sub->rects[i]->nb_colors * sizeof(uint32_t));
562 
563  /* Process bitmap */
564  object = find_object(ctx->presentation.objects[i].id, &ctx->objects);
565  if (!object) {
566  // Missing object. Should only happen with damaged streams.
567  av_log(avctx, AV_LOG_ERROR, "Invalid object id %d\n",
568  ctx->presentation.objects[i].id);
569  if (avctx->err_recognition & AV_EF_EXPLODE) {
571  return AVERROR_INVALIDDATA;
572  }
573  // Leaves rect empty with 0 width and height.
574  continue;
575  }
576  if (ctx->presentation.objects[i].composition_flag & 0x40)
577  sub->rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED;
578 
579  sub->rects[i]->x = ctx->presentation.objects[i].x;
580  sub->rects[i]->y = ctx->presentation.objects[i].y;
581 
582  if (object->rle) {
583  sub->rects[i]->w = object->w;
584  sub->rects[i]->h = object->h;
585 
586  sub->rects[i]->linesize[0] = object->w;
587 
588  if (object->rle_remaining_len) {
589  av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
590  object->rle_data_len, object->rle_remaining_len);
591  if (avctx->err_recognition & AV_EF_EXPLODE) {
593  return AVERROR_INVALIDDATA;
594  }
595  }
596  ret = decode_rle(avctx, sub->rects[i], object->rle, object->rle_data_len);
597  if (ret < 0) {
598  if ((avctx->err_recognition & AV_EF_EXPLODE) ||
599  ret == AVERROR(ENOMEM)) {
601  return ret;
602  }
603  sub->rects[i]->w = 0;
604  sub->rects[i]->h = 0;
605  continue;
606  }
607  }
608 #if FF_API_AVPICTURE
610 {
612  int j;
613  rect = sub->rects[i];
614  for (j = 0; j < 4; j++) {
615  rect->pict.data[j] = rect->data[j];
616  rect->pict.linesize[j] = rect->linesize[j];
617  }
618 }
620 #endif
621  }
622  return 1;
623 }
624 
625 static int decode(AVCodecContext *avctx, void *data, int *got_sub_ptr,
626  AVPacket *avpkt)
627 {
628  const uint8_t *buf = avpkt->data;
629  int buf_size = avpkt->size;
630 
631  const uint8_t *buf_end;
632  uint8_t segment_type;
633  int segment_length;
634  int i, ret;
635 
636  ff_dlog(avctx, "PGS sub packet:\n");
637 
638  for (i = 0; i < buf_size; i++) {
639  ff_dlog(avctx, "%02x ", buf[i]);
640  if (i % 16 == 15)
641  ff_dlog(avctx, "\n");
642  }
643 
644  if (i & 15)
645  ff_dlog(avctx, "\n");
646 
647  *got_sub_ptr = 0;
648 
649  /* Ensure that we have received at a least a segment code and segment length */
650  if (buf_size < 3)
651  return -1;
652 
653  buf_end = buf + buf_size;
654 
655  /* Step through buffer to identify segments */
656  while (buf < buf_end) {
657  segment_type = bytestream_get_byte(&buf);
658  segment_length = bytestream_get_be16(&buf);
659 
660  ff_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
661 
662  if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
663  break;
664 
665  ret = 0;
666  switch (segment_type) {
667  case PALETTE_SEGMENT:
668  ret = parse_palette_segment(avctx, buf, segment_length);
669  break;
670  case OBJECT_SEGMENT:
671  ret = parse_object_segment(avctx, buf, segment_length);
672  break;
674  ret = parse_presentation_segment(avctx, buf, segment_length, ((AVSubtitle*)(data))->pts);
675  break;
676  case WINDOW_SEGMENT:
677  /*
678  * Window Segment Structure (No new information provided):
679  * 2 bytes: Unknown,
680  * 2 bytes: X position of subtitle,
681  * 2 bytes: Y position of subtitle,
682  * 2 bytes: Width of subtitle,
683  * 2 bytes: Height of subtitle.
684  */
685  break;
686  case DISPLAY_SEGMENT:
687  if (*got_sub_ptr) {
688  av_log(avctx, AV_LOG_ERROR, "Duplicate display segment\n");
689  ret = AVERROR_INVALIDDATA;
690  break;
691  }
692  ret = display_end_segment(avctx, data, buf, segment_length);
693  if (ret >= 0)
694  *got_sub_ptr = ret;
695  break;
696  default:
697  av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
698  segment_type, segment_length);
699  ret = AVERROR_INVALIDDATA;
700  break;
701  }
702  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
704  *got_sub_ptr = 0;
705  return ret;
706  }
707 
708  buf += segment_length;
709  }
710 
711  return buf_size;
712 }
713 
714 #define OFFSET(x) offsetof(PGSSubContext, x)
715 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
716 static const AVOption options[] = {
717  {"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD},
718  { NULL },
719 };
720 
721 static const AVClass pgsdec_class = {
722  .class_name = "PGS subtitle decoder",
723  .item_name = av_default_item_name,
724  .option = options,
725  .version = LIBAVUTIL_VERSION_INT,
726 };
727 
729  .name = "pgssub",
730  .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
731  .type = AVMEDIA_TYPE_SUBTITLE,
733  .priv_data_size = sizeof(PGSSubContext),
734  .init = init_decoder,
735  .close = close_decoder,
736  .decode = decode,
737  .priv_class = &pgsdec_class,
738 };
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1660
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define flags(name, subs,...)
Definition: cbs_av1.c:572
static struct @322 state
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static float sub(float src0, float src1)
#define cm
Definition: dvbsubdec.c:37
static int rle(uint8_t *dst, const uint8_t *src, int compressed_size, int uncompressed_size)
Definition: exr.c:218
enum AVCodecID id
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
#define AV_SUBTITLE_FLAG_FORCED
Definition: avcodec.h:2689
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Definition: avcodec.c:557
@ SUBTITLE_BITMAP
A bitmap, pict will be set.
Definition: avcodec.h:2674
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h:529
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
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...
Definition: utils.c:50
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:478
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static const int16_t alpha[]
Definition: ilbcdata.h:55
misc image utilities
int i
Definition: input.c:407
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:84
Various defines for YUV<->RGB conversion.
#define YUV_TO_RGB1_CCIR_BT709(cb1, cr1)
Definition: colorspace.h:44
#define YUV_TO_RGB1_CCIR(cb1, cr1)
Definition: colorspace.h:34
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
Definition: colorspace.h:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
uint8_t w
Definition: llviddspenc.c:39
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:92
#define MAX_NEG_CROP
Definition: mathops.h:31
#define ff_crop_tab
const char data[16]
Definition: mxf.c:142
AVOptions.
static av_cold int close_decoder(AVCodecContext *avctx)
Definition: pgssubdec.c:144
static void flush_cache(AVCodecContext *avctx)
Definition: pgssubdec.c:101
#define MAX_EPOCH_PALETTES
Definition: pgssubdec.c:37
static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
Definition: pgssubdec.c:126
#define MAX_EPOCH_OBJECTS
Definition: pgssubdec.c:38
static const AVOption options[]
Definition: pgssubdec.c:716
SegmentType
Definition: pgssubdec.c:41
@ OBJECT_SEGMENT
Definition: pgssubdec.c:43
@ PALETTE_SEGMENT
Definition: pgssubdec.c:42
@ DISPLAY_SEGMENT
Definition: pgssubdec.c:46
@ PRESENTATION_SEGMENT
Definition: pgssubdec.c:44
@ WINDOW_SEGMENT
Definition: pgssubdec.c:45
static int parse_object_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the picture segment packet.
Definition: pgssubdec.c:231
static av_cold int init_decoder(AVCodecContext *avctx)
Definition: pgssubdec.c:137
static int decode(AVCodecContext *avctx, void *data, int *got_sub_ptr, AVPacket *avpkt)
Definition: pgssubdec.c:625
#define SD
Definition: pgssubdec.c:715
static int display_end_segment(AVCodecContext *avctx, void *data, const uint8_t *buf, int buf_size)
Parse the display segment packet.
Definition: pgssubdec.c:505
static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect, const uint8_t *buf, unsigned int buf_size)
Decode the RLE data.
Definition: pgssubdec.c:161
#define RGBA(r, g, b, a)
Definition: pgssubdec.c:36
static PGSSubObject * find_object(int id, PGSSubObjects *objects)
Definition: pgssubdec.c:115
#define MAX_OBJECT_REFS
Definition: pgssubdec.c:39
static const AVClass pgsdec_class
Definition: pgssubdec.c:721
#define OFFSET(x)
Definition: pgssubdec.c:714
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:395
static int parse_palette_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse the palette segment packet.
Definition: pgssubdec.c:333
AVCodec ff_pgssub_decoder
Definition: pgssubdec.c:728
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
void * priv_data
Definition: avcodec.h:563
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1649
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
PGSSubObjects objects
Definition: pgssubdec.c:97
PGSSubPresentation presentation
Definition: pgssubdec.c:95
int forced_subs_only
Definition: pgssubdec.c:98
PGSSubPalettes palettes
Definition: pgssubdec.c:96
uint8_t composition_flag
Definition: pgssubdec.c:52
unsigned int rle_buffer_size
Definition: pgssubdec.c:74
uint8_t * rle
Definition: pgssubdec.c:73
unsigned int rle_remaining_len
Definition: pgssubdec.c:75
unsigned int rle_data_len
Definition: pgssubdec.c:74
PGSSubObject object[MAX_EPOCH_OBJECTS]
Definition: pgssubdec.c:80
uint32_t clut[256]
Definition: pgssubdec.c:85
PGSSubPalette palette[MAX_EPOCH_PALETTES]
Definition: pgssubdec.c:90
PGSSubObjectRef objects[MAX_OBJECT_REFS]
Definition: pgssubdec.c:65
Definition: f_ebur128.c:91
int w
Definition: f_ebur128.c:91
int h
Definition: f_ebur128.c:91
uint8_t run
Definition: svq3.c:205
#define av_malloc_array(a, b)
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
static int64_t pts
const char * b
Definition: vf_curves.c:119
const char * g
Definition: vf_curves.c:118
const char * r
Definition: vf_curves.c:117
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:221
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:220