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.

723 lines
22KB

  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) (((unsigned)(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->data[0] = av_malloc_array(rect->w, rect->h);
  145. if (!rect->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->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 || !width || !height) {
  246. av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions (%dx%d) invalid.\n", width, height);
  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. object->rle_data_len = 0;
  254. object->rle_remaining_len = 0;
  255. return AVERROR(ENOMEM);
  256. }
  257. memcpy(object->rle, buf, buf_size);
  258. object->rle_data_len = buf_size;
  259. object->rle_remaining_len = rle_bitmap_len - buf_size;
  260. return 0;
  261. }
  262. /**
  263. * Parse the palette segment packet.
  264. *
  265. * The palette segment contains details of the palette,
  266. * a maximum of 256 colors can be defined.
  267. *
  268. * @param avctx contains the current codec context
  269. * @param buf pointer to the packet to process
  270. * @param buf_size size of packet to process
  271. */
  272. static int parse_palette_segment(AVCodecContext *avctx,
  273. const uint8_t *buf, int buf_size)
  274. {
  275. PGSSubContext *ctx = avctx->priv_data;
  276. PGSSubPalette *palette;
  277. const uint8_t *buf_end = buf + buf_size;
  278. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  279. int color_id;
  280. int y, cb, cr, alpha;
  281. int r, g, b, r_add, g_add, b_add;
  282. int id;
  283. id = bytestream_get_byte(&buf);
  284. palette = find_palette(id, &ctx->palettes);
  285. if (!palette) {
  286. if (ctx->palettes.count >= MAX_EPOCH_PALETTES) {
  287. av_log(avctx, AV_LOG_ERROR, "Too many palettes in epoch\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. palette = &ctx->palettes.palette[ctx->palettes.count++];
  291. palette->id = id;
  292. }
  293. /* Skip palette version */
  294. buf += 1;
  295. while (buf < buf_end) {
  296. color_id = bytestream_get_byte(&buf);
  297. y = bytestream_get_byte(&buf);
  298. cr = bytestream_get_byte(&buf);
  299. cb = bytestream_get_byte(&buf);
  300. alpha = bytestream_get_byte(&buf);
  301. /* Default to BT.709 colorspace. In case of <= 576 height use BT.601 */
  302. if (avctx->height <= 0 || avctx->height > 576) {
  303. YUV_TO_RGB1_CCIR_BT709(cb, cr);
  304. } else {
  305. YUV_TO_RGB1_CCIR(cb, cr);
  306. }
  307. YUV_TO_RGB2_CCIR(r, g, b, y);
  308. ff_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
  309. /* Store color in palette */
  310. palette->clut[color_id] = RGBA(r,g,b,alpha);
  311. }
  312. return 0;
  313. }
  314. /**
  315. * Parse the presentation segment packet.
  316. *
  317. * The presentation segment contains details on the video
  318. * width, video height, x & y subtitle position.
  319. *
  320. * @param avctx contains the current codec context
  321. * @param buf pointer to the packet to process
  322. * @param buf_size size of packet to process
  323. * @todo TODO: Implement cropping
  324. */
  325. static int parse_presentation_segment(AVCodecContext *avctx,
  326. const uint8_t *buf, int buf_size,
  327. int64_t pts)
  328. {
  329. PGSSubContext *ctx = avctx->priv_data;
  330. int i, state, ret;
  331. const uint8_t *buf_end = buf + buf_size;
  332. // Video descriptor
  333. int w = bytestream_get_be16(&buf);
  334. int h = bytestream_get_be16(&buf);
  335. ctx->presentation.pts = pts;
  336. ff_dlog(avctx, "Video Dimensions %dx%d\n",
  337. w, h);
  338. ret = ff_set_dimensions(avctx, w, h);
  339. if (ret < 0)
  340. return ret;
  341. /* Skip 1 bytes of unknown, frame rate */
  342. buf++;
  343. // Composition descriptor
  344. ctx->presentation.id_number = bytestream_get_be16(&buf);
  345. /*
  346. * state is a 2 bit field that defines pgs epoch boundaries
  347. * 00 - Normal, previously defined objects and palettes are still valid
  348. * 01 - Acquisition point, previous objects and palettes can be released
  349. * 10 - Epoch start, previous objects and palettes can be released
  350. * 11 - Epoch continue, previous objects and palettes can be released
  351. *
  352. * reserved 6 bits discarded
  353. */
  354. state = bytestream_get_byte(&buf) >> 6;
  355. if (state != 0) {
  356. flush_cache(avctx);
  357. }
  358. /*
  359. * skip palette_update_flag (0x80),
  360. */
  361. buf += 1;
  362. ctx->presentation.palette_id = bytestream_get_byte(&buf);
  363. ctx->presentation.object_count = bytestream_get_byte(&buf);
  364. if (ctx->presentation.object_count > MAX_OBJECT_REFS) {
  365. av_log(avctx, AV_LOG_ERROR,
  366. "Invalid number of presentation objects %d\n",
  367. ctx->presentation.object_count);
  368. ctx->presentation.object_count = 2;
  369. if (avctx->err_recognition & AV_EF_EXPLODE) {
  370. return AVERROR_INVALIDDATA;
  371. }
  372. }
  373. for (i = 0; i < ctx->presentation.object_count; i++)
  374. {
  375. if (buf_end - buf < 8) {
  376. av_log(avctx, AV_LOG_ERROR, "Insufficent space for object\n");
  377. ctx->presentation.object_count = i;
  378. return AVERROR_INVALIDDATA;
  379. }
  380. ctx->presentation.objects[i].id = bytestream_get_be16(&buf);
  381. ctx->presentation.objects[i].window_id = bytestream_get_byte(&buf);
  382. ctx->presentation.objects[i].composition_flag = bytestream_get_byte(&buf);
  383. ctx->presentation.objects[i].x = bytestream_get_be16(&buf);
  384. ctx->presentation.objects[i].y = bytestream_get_be16(&buf);
  385. // If cropping
  386. if (ctx->presentation.objects[i].composition_flag & 0x80) {
  387. ctx->presentation.objects[i].crop_x = bytestream_get_be16(&buf);
  388. ctx->presentation.objects[i].crop_y = bytestream_get_be16(&buf);
  389. ctx->presentation.objects[i].crop_w = bytestream_get_be16(&buf);
  390. ctx->presentation.objects[i].crop_h = bytestream_get_be16(&buf);
  391. }
  392. ff_dlog(avctx, "Subtitle Placement x=%d, y=%d\n",
  393. ctx->presentation.objects[i].x, ctx->presentation.objects[i].y);
  394. if (ctx->presentation.objects[i].x > avctx->width ||
  395. ctx->presentation.objects[i].y > avctx->height) {
  396. av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
  397. ctx->presentation.objects[i].x,
  398. ctx->presentation.objects[i].y,
  399. avctx->width, avctx->height);
  400. ctx->presentation.objects[i].x = 0;
  401. ctx->presentation.objects[i].y = 0;
  402. if (avctx->err_recognition & AV_EF_EXPLODE) {
  403. return AVERROR_INVALIDDATA;
  404. }
  405. }
  406. }
  407. return 0;
  408. }
  409. /**
  410. * Parse the display segment packet.
  411. *
  412. * The display segment controls the updating of the display.
  413. *
  414. * @param avctx contains the current codec context
  415. * @param data pointer to the data pertaining the subtitle to display
  416. * @param buf pointer to the packet to process
  417. * @param buf_size size of packet to process
  418. */
  419. static int display_end_segment(AVCodecContext *avctx, void *data,
  420. const uint8_t *buf, int buf_size)
  421. {
  422. AVSubtitle *sub = data;
  423. PGSSubContext *ctx = avctx->priv_data;
  424. int64_t pts;
  425. PGSSubPalette *palette;
  426. int i, ret;
  427. pts = ctx->presentation.pts != AV_NOPTS_VALUE ? ctx->presentation.pts : sub->pts;
  428. memset(sub, 0, sizeof(*sub));
  429. sub->pts = pts;
  430. ctx->presentation.pts = AV_NOPTS_VALUE;
  431. sub->start_display_time = 0;
  432. // There is no explicit end time for PGS subtitles. The end time
  433. // is defined by the start of the next sub which may contain no
  434. // objects (i.e. clears the previous sub)
  435. sub->end_display_time = UINT32_MAX;
  436. sub->format = 0;
  437. // Blank if last object_count was 0.
  438. if (!ctx->presentation.object_count)
  439. return 1;
  440. sub->rects = av_mallocz_array(ctx->presentation.object_count, sizeof(*sub->rects));
  441. if (!sub->rects) {
  442. return AVERROR(ENOMEM);
  443. }
  444. palette = find_palette(ctx->presentation.palette_id, &ctx->palettes);
  445. if (!palette) {
  446. // Missing palette. Should only happen with damaged streams.
  447. av_log(avctx, AV_LOG_ERROR, "Invalid palette id %d\n",
  448. ctx->presentation.palette_id);
  449. avsubtitle_free(sub);
  450. return AVERROR_INVALIDDATA;
  451. }
  452. for (i = 0; i < ctx->presentation.object_count; i++) {
  453. PGSSubObject *object;
  454. sub->rects[i] = av_mallocz(sizeof(*sub->rects[0]));
  455. if (!sub->rects[i]) {
  456. avsubtitle_free(sub);
  457. return AVERROR(ENOMEM);
  458. }
  459. sub->num_rects++;
  460. sub->rects[i]->type = SUBTITLE_BITMAP;
  461. /* Process bitmap */
  462. object = find_object(ctx->presentation.objects[i].id, &ctx->objects);
  463. if (!object) {
  464. // Missing object. Should only happen with damaged streams.
  465. av_log(avctx, AV_LOG_ERROR, "Invalid object id %d\n",
  466. ctx->presentation.objects[i].id);
  467. if (avctx->err_recognition & AV_EF_EXPLODE) {
  468. avsubtitle_free(sub);
  469. return AVERROR_INVALIDDATA;
  470. }
  471. // Leaves rect empty with 0 width and height.
  472. continue;
  473. }
  474. if (ctx->presentation.objects[i].composition_flag & 0x40)
  475. sub->rects[i]->flags |= AV_SUBTITLE_FLAG_FORCED;
  476. sub->rects[i]->x = ctx->presentation.objects[i].x;
  477. sub->rects[i]->y = ctx->presentation.objects[i].y;
  478. if (object->rle) {
  479. sub->rects[i]->w = object->w;
  480. sub->rects[i]->h = object->h;
  481. sub->rects[i]->linesize[0] = object->w;
  482. if (object->rle_remaining_len) {
  483. av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
  484. object->rle_data_len, object->rle_remaining_len);
  485. if (avctx->err_recognition & AV_EF_EXPLODE) {
  486. avsubtitle_free(sub);
  487. return AVERROR_INVALIDDATA;
  488. }
  489. }
  490. ret = decode_rle(avctx, sub->rects[i], object->rle, object->rle_data_len);
  491. if (ret < 0) {
  492. if ((avctx->err_recognition & AV_EF_EXPLODE) ||
  493. ret == AVERROR(ENOMEM)) {
  494. avsubtitle_free(sub);
  495. return ret;
  496. }
  497. sub->rects[i]->w = 0;
  498. sub->rects[i]->h = 0;
  499. continue;
  500. }
  501. }
  502. /* Allocate memory for colors */
  503. sub->rects[i]->nb_colors = 256;
  504. sub->rects[i]->data[1] = av_mallocz(AVPALETTE_SIZE);
  505. if (!sub->rects[i]->data[1]) {
  506. avsubtitle_free(sub);
  507. return AVERROR(ENOMEM);
  508. }
  509. if (!ctx->forced_subs_only || ctx->presentation.objects[i].composition_flag & 0x40)
  510. memcpy(sub->rects[i]->data[1], palette->clut, sub->rects[i]->nb_colors * sizeof(uint32_t));
  511. #if FF_API_AVPICTURE
  512. FF_DISABLE_DEPRECATION_WARNINGS
  513. {
  514. AVSubtitleRect *rect;
  515. int j;
  516. rect = sub->rects[i];
  517. for (j = 0; j < 4; j++) {
  518. rect->pict.data[j] = rect->data[j];
  519. rect->pict.linesize[j] = rect->linesize[j];
  520. }
  521. }
  522. FF_ENABLE_DEPRECATION_WARNINGS
  523. #endif
  524. }
  525. return 1;
  526. }
  527. static int decode(AVCodecContext *avctx, void *data, int *data_size,
  528. AVPacket *avpkt)
  529. {
  530. const uint8_t *buf = avpkt->data;
  531. int buf_size = avpkt->size;
  532. const uint8_t *buf_end;
  533. uint8_t segment_type;
  534. int segment_length;
  535. int i, ret;
  536. ff_dlog(avctx, "PGS sub packet:\n");
  537. for (i = 0; i < buf_size; i++) {
  538. ff_dlog(avctx, "%02x ", buf[i]);
  539. if (i % 16 == 15)
  540. ff_dlog(avctx, "\n");
  541. }
  542. if (i & 15)
  543. ff_dlog(avctx, "\n");
  544. *data_size = 0;
  545. /* Ensure that we have received at a least a segment code and segment length */
  546. if (buf_size < 3)
  547. return -1;
  548. buf_end = buf + buf_size;
  549. /* Step through buffer to identify segments */
  550. while (buf < buf_end) {
  551. segment_type = bytestream_get_byte(&buf);
  552. segment_length = bytestream_get_be16(&buf);
  553. ff_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
  554. if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
  555. break;
  556. ret = 0;
  557. switch (segment_type) {
  558. case PALETTE_SEGMENT:
  559. ret = parse_palette_segment(avctx, buf, segment_length);
  560. break;
  561. case OBJECT_SEGMENT:
  562. ret = parse_object_segment(avctx, buf, segment_length);
  563. break;
  564. case PRESENTATION_SEGMENT:
  565. ret = parse_presentation_segment(avctx, buf, segment_length, ((AVSubtitle*)(data))->pts);
  566. break;
  567. case WINDOW_SEGMENT:
  568. /*
  569. * Window Segment Structure (No new information provided):
  570. * 2 bytes: Unknown,
  571. * 2 bytes: X position of subtitle,
  572. * 2 bytes: Y position of subtitle,
  573. * 2 bytes: Width of subtitle,
  574. * 2 bytes: Height of subtitle.
  575. */
  576. break;
  577. case DISPLAY_SEGMENT:
  578. ret = display_end_segment(avctx, data, buf, segment_length);
  579. if (ret >= 0)
  580. *data_size = ret;
  581. break;
  582. default:
  583. av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
  584. segment_type, segment_length);
  585. ret = AVERROR_INVALIDDATA;
  586. break;
  587. }
  588. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  589. return ret;
  590. buf += segment_length;
  591. }
  592. return buf_size;
  593. }
  594. #define OFFSET(x) offsetof(PGSSubContext, x)
  595. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  596. static const AVOption options[] = {
  597. {"forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD},
  598. { NULL },
  599. };
  600. static const AVClass pgsdec_class = {
  601. .class_name = "PGS subtitle decoder",
  602. .item_name = av_default_item_name,
  603. .option = options,
  604. .version = LIBAVUTIL_VERSION_INT,
  605. };
  606. AVCodec ff_pgssub_decoder = {
  607. .name = "pgssub",
  608. .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
  609. .type = AVMEDIA_TYPE_SUBTITLE,
  610. .id = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
  611. .priv_data_size = sizeof(PGSSubContext),
  612. .init = init_decoder,
  613. .close = close_decoder,
  614. .decode = decode,
  615. .priv_class = &pgsdec_class,
  616. };