You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

700 lines
21KB

  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. * @file
  23. * PGS subtitle decoder
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. #include "mathops.h"
  29. #include "libavutil/colorspace.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/opt.h"
  32. #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
  33. #define MAX_EPOCH_PALETTES 8 // Max 8 allowed per PGS epoch
  34. #define MAX_EPOCH_OBJECTS 64 // Max 64 allowed per PGS epoch
  35. #define MAX_OBJECT_REFS 2 // Max objects per display set
  36. enum SegmentType {
  37. PALETTE_SEGMENT = 0x14,
  38. OBJECT_SEGMENT = 0x15,
  39. PRESENTATION_SEGMENT = 0x16,
  40. WINDOW_SEGMENT = 0x17,
  41. DISPLAY_SEGMENT = 0x80,
  42. };
  43. typedef struct PGSSubObjectRef {
  44. int id;
  45. int window_id;
  46. uint8_t composition_flag;
  47. int x;
  48. int y;
  49. int crop_x;
  50. int crop_y;
  51. int crop_w;
  52. int crop_h;
  53. } PGSSubObjectRef;
  54. typedef struct PGSSubPresentation {
  55. int id_number;
  56. int palette_id;
  57. int object_count;
  58. PGSSubObjectRef objects[MAX_OBJECT_REFS];
  59. int64_t pts;
  60. } PGSSubPresentation;
  61. typedef struct PGSSubObject {
  62. int id;
  63. int w;
  64. int h;
  65. uint8_t *rle;
  66. unsigned int rle_buffer_size, rle_data_len;
  67. unsigned int rle_remaining_len;
  68. } PGSSubObject;
  69. typedef struct PGSSubObjects {
  70. int count;
  71. PGSSubObject object[MAX_EPOCH_OBJECTS];
  72. } PGSSubObjects;
  73. typedef struct PGSSubPalette {
  74. int id;
  75. uint32_t clut[256];
  76. } PGSSubPalette;
  77. typedef struct PGSSubPalettes {
  78. int count;
  79. PGSSubPalette palette[MAX_EPOCH_PALETTES];
  80. } PGSSubPalettes;
  81. typedef struct PGSSubContext {
  82. AVClass *class;
  83. PGSSubPresentation presentation;
  84. PGSSubPalettes palettes;
  85. PGSSubObjects objects;
  86. int forced_subs_only;
  87. } PGSSubContext;
  88. static void flush_cache(AVCodecContext *avctx)
  89. {
  90. PGSSubContext *ctx = avctx->priv_data;
  91. int i;
  92. for (i = 0; i < ctx->objects.count; i++) {
  93. av_freep(&ctx->objects.object[i].rle);
  94. ctx->objects.object[i].rle_buffer_size = 0;
  95. ctx->objects.object[i].rle_remaining_len = 0;
  96. }
  97. ctx->objects.count = 0;
  98. ctx->palettes.count = 0;
  99. }
  100. static PGSSubObject * find_object(int id, PGSSubObjects *objects)
  101. {
  102. int i;
  103. for (i = 0; i < objects->count; i++) {
  104. if (objects->object[i].id == id)
  105. return &objects->object[i];
  106. }
  107. return NULL;
  108. }
  109. static PGSSubPalette * find_palette(int id, PGSSubPalettes *palettes)
  110. {
  111. int i;
  112. for (i = 0; i < palettes->count; i++) {
  113. if (palettes->palette[i].id == id)
  114. return &palettes->palette[i];
  115. }
  116. return NULL;
  117. }
  118. static av_cold int init_decoder(AVCodecContext *avctx)
  119. {
  120. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  121. return 0;
  122. }
  123. static av_cold int close_decoder(AVCodecContext *avctx)
  124. {
  125. flush_cache(avctx);
  126. return 0;
  127. }
  128. /**
  129. * Decode the RLE data.
  130. *
  131. * The subtitle is stored as a Run Length Encoded image.
  132. *
  133. * @param avctx contains the current codec context
  134. * @param sub pointer to the processed subtitle data
  135. * @param buf pointer to the RLE data to process
  136. * @param buf_size size of the RLE data to process
  137. */
  138. static int decode_rle(AVCodecContext *avctx, AVSubtitleRect *rect,
  139. const uint8_t *buf, unsigned int buf_size)
  140. {
  141. const uint8_t *rle_bitmap_end;
  142. int pixel_count, line_count;
  143. rle_bitmap_end = buf + buf_size;
  144. rect->pict.data[0] = av_malloc_array(rect->w, rect->h);
  145. if (!rect->pict.data[0])
  146. return AVERROR(ENOMEM);
  147. pixel_count = 0;
  148. line_count = 0;
  149. while (buf < rle_bitmap_end && line_count < rect->h) {
  150. uint8_t flags, color;
  151. int run;
  152. color = bytestream_get_byte(&buf);
  153. run = 1;
  154. if (color == 0x00) {
  155. flags = bytestream_get_byte(&buf);
  156. run = flags & 0x3f;
  157. if (flags & 0x40)
  158. run = (run << 8) + bytestream_get_byte(&buf);
  159. color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
  160. }
  161. if (run > 0 && pixel_count + run <= rect->w * rect->h) {
  162. memset(rect->pict.data[0] + pixel_count, color, run);
  163. pixel_count += run;
  164. } else if (!run) {
  165. /*
  166. * New Line. Check if correct pixels decoded, if not display warning
  167. * and adjust bitmap pointer to correct new line position.
  168. */
  169. if (pixel_count % rect->w > 0) {
  170. av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
  171. pixel_count % rect->w, rect->w);
  172. if (avctx->err_recognition & AV_EF_EXPLODE) {
  173. return AVERROR_INVALIDDATA;
  174. }
  175. }
  176. line_count++;
  177. }
  178. }
  179. if (pixel_count < rect->w * rect->h) {
  180. av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. ff_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, rect->w * rect->h);
  184. return 0;
  185. }
  186. /**
  187. * Parse the picture segment packet.
  188. *
  189. * The picture segment contains details on the sequence id,
  190. * width, height and Run Length Encoded (RLE) bitmap data.
  191. *
  192. * @param avctx contains the current codec context
  193. * @param buf pointer to the packet to process
  194. * @param buf_size size of packet to process
  195. */
  196. static int parse_object_segment(AVCodecContext *avctx,
  197. const uint8_t *buf, int buf_size)
  198. {
  199. PGSSubContext *ctx = avctx->priv_data;
  200. PGSSubObject *object;
  201. uint8_t sequence_desc;
  202. unsigned int rle_bitmap_len, width, height;
  203. int id;
  204. if (buf_size <= 4)
  205. return AVERROR_INVALIDDATA;
  206. buf_size -= 4;
  207. id = bytestream_get_be16(&buf);
  208. object = find_object(id, &ctx->objects);
  209. if (!object) {
  210. if (ctx->objects.count >= MAX_EPOCH_OBJECTS) {
  211. av_log(avctx, AV_LOG_ERROR, "Too many objects in epoch\n");
  212. return AVERROR_INVALIDDATA;
  213. }
  214. object = &ctx->objects.object[ctx->objects.count++];
  215. object->id = id;
  216. }
  217. /* skip object version number */
  218. buf += 1;
  219. /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
  220. sequence_desc = bytestream_get_byte(&buf);
  221. if (!(sequence_desc & 0x80)) {
  222. /* Additional RLE data */
  223. if (buf_size > object->rle_remaining_len)
  224. return AVERROR_INVALIDDATA;
  225. memcpy(object->rle + object->rle_data_len, buf, buf_size);
  226. object->rle_data_len += buf_size;
  227. object->rle_remaining_len -= buf_size;
  228. return 0;
  229. }
  230. if (buf_size <= 7)
  231. return AVERROR_INVALIDDATA;
  232. buf_size -= 7;
  233. /* Decode rle bitmap length, stored size includes width/height data */
  234. rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
  235. if (buf_size > rle_bitmap_len) {
  236. av_log(avctx, AV_LOG_ERROR,
  237. "Buffer dimension %d larger than the expected RLE data %d\n",
  238. buf_size, rle_bitmap_len);
  239. return AVERROR_INVALIDDATA;
  240. }
  241. /* Get bitmap dimensions from data */
  242. width = bytestream_get_be16(&buf);
  243. height = bytestream_get_be16(&buf);
  244. /* Make sure the bitmap is not too large */
  245. if (avctx->width < width || avctx->height < height) {
  246. av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
  247. return AVERROR_INVALIDDATA;
  248. }
  249. object->w = width;
  250. object->h = height;
  251. av_fast_padded_malloc(&object->rle, &object->rle_buffer_size, rle_bitmap_len);
  252. if (!object->rle)
  253. return AVERROR(ENOMEM);
  254. memcpy(object->rle, buf, buf_size);
  255. object->rle_data_len = buf_size;
  256. object->rle_remaining_len = rle_bitmap_len - buf_size;
  257. return 0;
  258. }
  259. /**
  260. * Parse the palette segment packet.
  261. *
  262. * The palette segment contains details of the palette,
  263. * a maximum of 256 colors can be defined.
  264. *
  265. * @param avctx contains the current codec context
  266. * @param buf pointer to the packet to process
  267. * @param buf_size size of packet to process
  268. */
  269. static int parse_palette_segment(AVCodecContext *avctx,
  270. const uint8_t *buf, int buf_size)
  271. {
  272. PGSSubContext *ctx = avctx->priv_data;
  273. PGSSubPalette *palette;
  274. const uint8_t *buf_end = buf + buf_size;
  275. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  276. int color_id;
  277. int y, cb, cr, alpha;
  278. int r, g, b, r_add, g_add, b_add;
  279. int id;
  280. id = bytestream_get_byte(&buf);
  281. palette = find_palette(id, &ctx->palettes);
  282. if (!palette) {
  283. if (ctx->palettes.count >= MAX_EPOCH_PALETTES) {
  284. av_log(avctx, AV_LOG_ERROR, "Too many palettes in epoch\n");
  285. return AVERROR_INVALIDDATA;
  286. }
  287. palette = &ctx->palettes.palette[ctx->palettes.count++];
  288. palette->id = id;
  289. }
  290. /* Skip palette version */
  291. buf += 1;
  292. while (buf < buf_end) {
  293. color_id = bytestream_get_byte(&buf);
  294. y = bytestream_get_byte(&buf);
  295. cr = bytestream_get_byte(&buf);
  296. cb = bytestream_get_byte(&buf);
  297. alpha = bytestream_get_byte(&buf);
  298. YUV_TO_RGB1(cb, cr);
  299. YUV_TO_RGB2(r, g, b, y);
  300. ff_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
  301. /* Store color in palette */
  302. palette->clut[color_id] = RGBA(r,g,b,alpha);
  303. }
  304. return 0;
  305. }
  306. /**
  307. * Parse the presentation segment packet.
  308. *
  309. * The presentation segment contains details on the video
  310. * width, video height, x & y subtitle position.
  311. *
  312. * @param avctx contains the current codec context
  313. * @param buf pointer to the packet to process
  314. * @param buf_size size of packet to process
  315. * @todo TODO: Implement cropping
  316. */
  317. static int parse_presentation_segment(AVCodecContext *avctx,
  318. const uint8_t *buf, int buf_size,
  319. int64_t pts)
  320. {
  321. PGSSubContext *ctx = avctx->priv_data;
  322. int i, state, ret;
  323. const uint8_t *buf_end = buf + buf_size;
  324. // Video descriptor
  325. int w = bytestream_get_be16(&buf);
  326. int h = bytestream_get_be16(&buf);
  327. ctx->presentation.pts = pts;
  328. ff_dlog(avctx, "Video Dimensions %dx%d\n",
  329. w, h);
  330. ret = ff_set_dimensions(avctx, w, h);
  331. if (ret < 0)
  332. return ret;
  333. /* Skip 1 bytes of unknown, frame rate */
  334. buf++;
  335. // Composition descriptor
  336. ctx->presentation.id_number = bytestream_get_be16(&buf);
  337. /*
  338. * state is a 2 bit field that defines pgs epoch boundaries
  339. * 00 - Normal, previously defined objects and palettes are still valid
  340. * 01 - Acquisition point, previous objects and palettes can be released
  341. * 10 - Epoch start, previous objects and palettes can be released
  342. * 11 - Epoch continue, previous objects and palettes can be released
  343. *
  344. * reserved 6 bits discarded
  345. */
  346. state = bytestream_get_byte(&buf) >> 6;
  347. if (state != 0) {
  348. flush_cache(avctx);
  349. }
  350. /*
  351. * skip palette_update_flag (0x80),
  352. */
  353. buf += 1;
  354. ctx->presentation.palette_id = bytestream_get_byte(&buf);
  355. ctx->presentation.object_count = bytestream_get_byte(&buf);
  356. if (ctx->presentation.object_count > MAX_OBJECT_REFS) {
  357. av_log(avctx, AV_LOG_ERROR,
  358. "Invalid number of presentation objects %d\n",
  359. ctx->presentation.object_count);
  360. ctx->presentation.object_count = 2;
  361. if (avctx->err_recognition & AV_EF_EXPLODE) {
  362. return AVERROR_INVALIDDATA;
  363. }
  364. }
  365. for (i = 0; i < ctx->presentation.object_count; i++)
  366. {
  367. if (buf_end - buf < 8) {
  368. av_log(avctx, AV_LOG_ERROR, "Insufficent space for object\n");
  369. ctx->presentation.object_count = i;
  370. return AVERROR_INVALIDDATA;
  371. }
  372. ctx->presentation.objects[i].id = bytestream_get_be16(&buf);
  373. ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf);
  374. ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf);
  375. ctx->presentation.objects[i].x = bytestream_get_be16(&buf);
  376. ctx->presentation.objects[i].y = bytestream_get_be16(&buf);
  377. // If cropping
  378. if (ctx->presentation.objects[i].composition_flag & 0x80) {
  379. ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf);
  380. ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf);
  381. ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf);
  382. ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf);
  383. }
  384. ff_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",
  385. ctx->presentation.objects[i].x, ctx->presentation.objects[i].y);
  386. if (ctx->presentation.objects[i].x > avctx->width ||
  387. ctx->presentation.objects[i].y > avctx->height) {
  388. av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
  389. ctx->presentation.objects[i].x,
  390. ctx->presentation.objects[i].y,
  391. avctx->width, avctx->height);
  392. ctx->presentation.objects[i].x = 0;
  393. ctx->presentation.objects[i].y = 0;
  394. if (avctx->err_recognition & AV_EF_EXPLODE) {
  395. return AVERROR_INVALIDDATA;
  396. }
  397. }
  398. }
  399. return 0;
  400. }
  401. /**
  402. * Parse the display segment packet.
  403. *
  404. * The display segment controls the updating of the display.
  405. *
  406. * @param avctx contains the current codec context
  407. * @param data pointer to the data pertaining the subtitle to display
  408. * @param buf pointer to the packet to process
  409. * @param buf_size size of packet to process
  410. */
  411. static int display_end_segment(AVCodecContext *avctx, void *data,
  412. const uint8_t *buf, int buf_size)
  413. {
  414. AVSubtitle *sub = data;
  415. PGSSubContext *ctx = avctx->priv_data;
  416. int64_t pts;
  417. PGSSubPalette *palette;
  418. int i, ret;
  419. pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts;
  420. memset(sub, 0, sizeof(*sub));
  421. sub->pts = pts;
  422. ctx->presentation.pts = AV_NOPTS_VALUE;
  423. sub->start_display_time = 0;
  424. // There is no explicit end time for PGS subtitles. The end time
  425. // is defined by the start of the next sub which may contain no
  426. // objects (i.e. clears the previous sub)
  427. sub->end_display_time = UINT32_MAX;
  428. sub->format = 0;
  429. // Blank if last object_count was 0.
  430. if (!ctx->presentation.object_count)
  431. return 1;
  432. sub->rects = av_mallocz_array(ctx->presentation.object_count, sizeof(*sub->rects));
  433. if (!sub->rects) {
  434. return AVERROR(ENOMEM);
  435. }
  436. palette = find_palette(ctx->presentation.palette_id, &ctx->palettes);
  437. if (!palette) {
  438. // Missing palette. Should only happen with damaged streams.
  439. av_log(avctx, AV_LOG_ERROR, "Invalid palette id %d\n",
  440. ctx->presentation.palette_id);
  441. avsubtitle_free(sub);
  442. return AVERROR_INVALIDDATA;
  443. }
  444. for (i = 0; i < ctx->presentation.object_count; i++) {
  445. PGSSubObject *object;
  446. sub->rects[i] = av_mallocz(sizeof(*sub->rects[0]));
  447. if (!sub->rects[i]) {
  448. avsubtitle_free(sub);
  449. return AVERROR(ENOMEM);
  450. }
  451. sub->num_rects++;
  452. sub->rects[i]->type = SUBTITLE_BITMAP;
  453. /* Process bitmap */
  454. object = find_object(ctx->presentation.objects[i].id, &ctx->objects);
  455. if (!object) {
  456. // Missing object. Should only happen with damaged streams.
  457. av_log(avctx, AV_LOG_ERROR, "Invalid object id %d\n",
  458. ctx->presentation.objects[i].id);
  459. if (avctx->err_recognition & AV_EF_EXPLODE) {
  460. avsubtitle_free(sub);
  461. return AVERROR_INVALIDDATA;
  462. }
  463. // Leaves rect empty with 0 width and height.
  464. continue;
  465. }
  466. if (ctx->presentation.objects[i].composition_flag & 0x40)
  467. sub->rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED;
  468. sub->rects[i]->x = ctx->presentation.objects[i].x;
  469. sub->rects[i]->y = ctx->presentation.objects[i].y;
  470. sub->rects[i]->w = object->w;
  471. sub->rects[i]->h = object->h;
  472. sub->rects[i]->pict.linesize[0] = object->w;
  473. if (object->rle) {
  474. if (object->rle_remaining_len) {
  475. av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
  476. object->rle_data_len, object->rle_remaining_len);
  477. if (avctx->err_recognition & AV_EF_EXPLODE) {
  478. avsubtitle_free(sub);
  479. return AVERROR_INVALIDDATA;
  480. }
  481. }
  482. ret = decode_rle(avctx, sub->rects[i], object->rle, object->rle_data_len);
  483. if (ret < 0) {
  484. if ((avctx->err_recognition & AV_EF_EXPLODE) ||
  485. ret == AVERROR(ENOMEM)) {
  486. avsubtitle_free(sub);
  487. return ret;
  488. }
  489. sub->rects[i]->w = 0;
  490. sub->rects[i]->h = 0;
  491. continue;
  492. }
  493. }
  494. /* Allocate memory for colors */
  495. sub->rects[i]->nb_colors = 256;
  496. sub->rects[i]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  497. if (!sub->rects[i]->pict.data[1]) {
  498. avsubtitle_free(sub);
  499. return AVERROR(ENOMEM);
  500. }
  501. if (!ctx->forced_subs_only || ctx->presentation.objects[i].composition_flag & 0x40)
  502. memcpy(sub->rects[i]->pict.data[1], palette->clut, sub->rects[i]->nb_colors * sizeof(uint32_t));
  503. }
  504. return 1;
  505. }
  506. static int decode(AVCodecContext *avctx, void *data, int *data_size,
  507. AVPacket *avpkt)
  508. {
  509. const uint8_t *buf = avpkt->data;
  510. int buf_size = avpkt->size;
  511. const uint8_t *buf_end;
  512. uint8_t segment_type;
  513. int segment_length;
  514. int i, ret;
  515. ff_dlog(avctx, "PGS sub packet:\n");
  516. for (i = 0; i < buf_size; i++) {
  517. ff_dlog(avctx, "%02x ", buf[i]);
  518. if (i % 16 == 15)
  519. ff_dlog(avctx, "\n");
  520. }
  521. if (i & 15)
  522. ff_dlog(avctx, "\n");
  523. *data_size = 0;
  524. /* Ensure that we have received at a least a segment code and segment length */
  525. if (buf_size < 3)
  526. return -1;
  527. buf_end = buf + buf_size;
  528. /* Step through buffer to identify segments */
  529. while (buf < buf_end) {
  530. segment_type = bytestream_get_byte(&buf);
  531. segment_length = bytestream_get_be16(&buf);
  532. ff_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
  533. if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
  534. break;
  535. ret = 0;
  536. switch (segment_type) {
  537. case PALETTE_SEGMENT:
  538. ret = parse_palette_segment(avctx, buf, segment_length);
  539. break;
  540. case OBJECT_SEGMENT:
  541. ret = parse_object_segment(avctx, buf, segment_length);
  542. break;
  543. case PRESENTATION_SEGMENT:
  544. ret = parse_presentation_segment(avctx, buf, segment_length, ((AVSubtitle*)(data))->pts);
  545. break;
  546. case WINDOW_SEGMENT:
  547. /*
  548. * Window Segment Structure (No new information provided):
  549. * 2 bytes: Unknown,
  550. * 2 bytes: X position of subtitle,
  551. * 2 bytes: Y position of subtitle,
  552. * 2 bytes: Width of subtitle,
  553. * 2 bytes: Height of subtitle.
  554. */
  555. break;
  556. case DISPLAY_SEGMENT:
  557. ret = display_end_segment(avctx, data, buf, segment_length);
  558. if (ret >= 0)
  559. *data_size = ret;
  560. break;
  561. default:
  562. av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
  563. segment_type, segment_length);
  564. ret = AVERROR_INVALIDDATA;
  565. break;
  566. }
  567. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  568. return ret;
  569. buf += segment_length;
  570. }
  571. return buf_size;
  572. }
  573. #define OFFSET(x) offsetof(PGSSubContext, x)
  574. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  575. static const AVOption options[] = {
  576. {"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
  577. { NULL },
  578. };
  579. static const AVClass pgsdec_class = {
  580. .class_name = "PGS subtitle decoder",
  581. .item_name = av_default_item_name,
  582. .option = options,
  583. .version = LIBAVUTIL_VERSION_INT,
  584. };
  585. AVCodec ff_pgssub_decoder = {
  586. .name = "pgssub",
  587. .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
  588. .type = AVMEDIA_TYPE_SUBTITLE,
  589. .id = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
  590. .priv_data_size = sizeof(PGSSubContext),
  591. .init = init_decoder,
  592. .close = close_decoder,
  593. .decode = decode,
  594. .priv_class = &pgsdec_class,
  595. };