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.

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