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.

516 lines
16KB

  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 "dsputil.h"
  27. #include "bytestream.h"
  28. #include "libavutil/colorspace.h"
  29. #include "libavutil/imgutils.h"
  30. #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
  31. enum SegmentType {
  32. PALETTE_SEGMENT = 0x14,
  33. PICTURE_SEGMENT = 0x15,
  34. PRESENTATION_SEGMENT = 0x16,
  35. WINDOW_SEGMENT = 0x17,
  36. DISPLAY_SEGMENT = 0x80,
  37. };
  38. typedef struct PGSSubPictureReference {
  39. int x;
  40. int y;
  41. int picture_id;
  42. } PGSSubPictureReference;
  43. typedef struct PGSSubPresentation {
  44. int id_number;
  45. int object_count;
  46. PGSSubPictureReference *objects;
  47. } PGSSubPresentation;
  48. typedef struct PGSSubPicture {
  49. int w;
  50. int h;
  51. uint8_t *rle;
  52. unsigned int rle_buffer_size, rle_data_len;
  53. unsigned int rle_remaining_len;
  54. } PGSSubPicture;
  55. typedef struct PGSSubContext {
  56. PGSSubPresentation presentation;
  57. uint32_t clut[256];
  58. PGSSubPicture pictures[UINT16_MAX];
  59. } PGSSubContext;
  60. static av_cold int init_decoder(AVCodecContext *avctx)
  61. {
  62. avctx->pix_fmt = PIX_FMT_PAL8;
  63. return 0;
  64. }
  65. static av_cold int close_decoder(AVCodecContext *avctx)
  66. {
  67. uint16_t picture;
  68. PGSSubContext *ctx = avctx->priv_data;
  69. av_freep(&ctx->presentation.objects);
  70. ctx->presentation.object_count = 0;
  71. for (picture = 0; picture < UINT16_MAX; ++picture) {
  72. av_freep(&ctx->pictures[picture].rle);
  73. ctx->pictures[picture].rle_buffer_size = 0;
  74. }
  75. return 0;
  76. }
  77. /**
  78. * Decode the RLE data.
  79. *
  80. * The subtitle is stored as an Run Length Encoded image.
  81. *
  82. * @param avctx contains the current codec context
  83. * @param sub pointer to the processed subtitle data
  84. * @param buf pointer to the RLE data to process
  85. * @param buf_size size of the RLE data to process
  86. */
  87. static int decode_rle(AVCodecContext *avctx, AVSubtitle *sub, int rect,
  88. const uint8_t *buf, unsigned int buf_size)
  89. {
  90. const uint8_t *rle_bitmap_end;
  91. int pixel_count, line_count;
  92. rle_bitmap_end = buf + buf_size;
  93. sub->rects[rect]->pict.data[0] = av_malloc(sub->rects[rect]->w * sub->rects[rect]->h);
  94. if (!sub->rects[rect]->pict.data[0])
  95. return -1;
  96. pixel_count = 0;
  97. line_count = 0;
  98. while (buf < rle_bitmap_end && line_count < sub->rects[rect]->h) {
  99. uint8_t flags, color;
  100. int run;
  101. color = bytestream_get_byte(&buf);
  102. run = 1;
  103. if (color == 0x00) {
  104. flags = bytestream_get_byte(&buf);
  105. run = flags & 0x3f;
  106. if (flags & 0x40)
  107. run = (run << 8) + bytestream_get_byte(&buf);
  108. color = flags & 0x80 ? bytestream_get_byte(&buf) : 0;
  109. }
  110. if (run > 0 && pixel_count + run <= sub->rects[rect]->w * sub->rects[rect]->h) {
  111. memset(sub->rects[rect]->pict.data[0] + pixel_count, color, run);
  112. pixel_count += run;
  113. } else if (!run) {
  114. /*
  115. * New Line. Check if correct pixels decoded, if not display warning
  116. * and adjust bitmap pointer to correct new line position.
  117. */
  118. if (pixel_count % sub->rects[rect]->w > 0)
  119. av_log(avctx, AV_LOG_ERROR, "Decoded %d pixels, when line should be %d pixels\n",
  120. pixel_count % sub->rects[rect]->w, sub->rects[rect]->w);
  121. line_count++;
  122. }
  123. }
  124. if (pixel_count < sub->rects[rect]->w * sub->rects[rect]->h) {
  125. av_log(avctx, AV_LOG_ERROR, "Insufficient RLE data for subtitle\n");
  126. return -1;
  127. }
  128. av_dlog(avctx, "Pixel Count = %d, Area = %d\n", pixel_count, sub->rects[rect]->w * sub->rects[rect]->h);
  129. return 0;
  130. }
  131. /**
  132. * Parse the picture segment packet.
  133. *
  134. * The picture segment contains details on the sequence id,
  135. * width, height and Run Length Encoded (RLE) bitmap data.
  136. *
  137. * @param avctx contains the current codec context
  138. * @param buf pointer to the packet to process
  139. * @param buf_size size of packet to process
  140. * @todo TODO: Enable support for RLE data over multiple packets
  141. */
  142. static int parse_picture_segment(AVCodecContext *avctx,
  143. const uint8_t *buf, int buf_size)
  144. {
  145. PGSSubContext *ctx = avctx->priv_data;
  146. uint8_t sequence_desc;
  147. unsigned int rle_bitmap_len, width, height;
  148. uint16_t picture_id;
  149. if (buf_size <= 4)
  150. return -1;
  151. buf_size -= 4;
  152. picture_id = bytestream_get_be16(&buf);
  153. /* skip 1 unknown byte: Version Number */
  154. buf++;
  155. /* Read the Sequence Description to determine if start of RLE data or appended to previous RLE */
  156. sequence_desc = bytestream_get_byte(&buf);
  157. if (!(sequence_desc & 0x80)) {
  158. /* Additional RLE data */
  159. if (buf_size > ctx->pictures[picture_id].rle_remaining_len)
  160. return -1;
  161. memcpy(ctx->pictures[picture_id].rle + ctx->pictures[picture_id].rle_data_len, buf, buf_size);
  162. ctx->pictures[picture_id].rle_data_len += buf_size;
  163. ctx->pictures[picture_id].rle_remaining_len -= buf_size;
  164. return 0;
  165. }
  166. if (buf_size <= 7)
  167. return -1;
  168. buf_size -= 7;
  169. /* Decode rle bitmap length, stored size includes width/height data */
  170. rle_bitmap_len = bytestream_get_be24(&buf) - 2*2;
  171. /* Get bitmap dimensions from data */
  172. width = bytestream_get_be16(&buf);
  173. height = bytestream_get_be16(&buf);
  174. /* Make sure the bitmap is not too large */
  175. if (avctx->width < width || avctx->height < height) {
  176. av_log(avctx, AV_LOG_ERROR, "Bitmap dimensions larger than video.\n");
  177. return -1;
  178. }
  179. ctx->pictures[picture_id].w = width;
  180. ctx->pictures[picture_id].h = height;
  181. av_fast_malloc(&ctx->pictures[picture_id].rle, &ctx->pictures[picture_id].rle_buffer_size, rle_bitmap_len);
  182. if (!ctx->pictures[picture_id].rle)
  183. return -1;
  184. memcpy(ctx->pictures[picture_id].rle, buf, buf_size);
  185. ctx->pictures[picture_id].rle_data_len = buf_size;
  186. ctx->pictures[picture_id].rle_remaining_len = rle_bitmap_len - buf_size;
  187. return 0;
  188. }
  189. /**
  190. * Parse the palette segment packet.
  191. *
  192. * The palette segment contains details of the palette,
  193. * a maximum of 256 colors can be defined.
  194. *
  195. * @param avctx contains the current codec context
  196. * @param buf pointer to the packet to process
  197. * @param buf_size size of packet to process
  198. */
  199. static void parse_palette_segment(AVCodecContext *avctx,
  200. const uint8_t *buf, int buf_size)
  201. {
  202. PGSSubContext *ctx = avctx->priv_data;
  203. const uint8_t *buf_end = buf + buf_size;
  204. const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  205. int color_id;
  206. int y, cb, cr, alpha;
  207. int r, g, b, r_add, g_add, b_add;
  208. /* Skip two null bytes */
  209. buf += 2;
  210. while (buf < buf_end) {
  211. color_id = bytestream_get_byte(&buf);
  212. y = bytestream_get_byte(&buf);
  213. cr = bytestream_get_byte(&buf);
  214. cb = bytestream_get_byte(&buf);
  215. alpha = bytestream_get_byte(&buf);
  216. YUV_TO_RGB1(cb, cr);
  217. YUV_TO_RGB2(r, g, b, y);
  218. av_dlog(avctx, "Color %d := (%d,%d,%d,%d)\n", color_id, r, g, b, alpha);
  219. /* Store color in palette */
  220. ctx->clut[color_id] = RGBA(r,g,b,alpha);
  221. }
  222. }
  223. /**
  224. * Parse the presentation segment packet.
  225. *
  226. * The presentation segment contains details on the video
  227. * width, video height, x & y subtitle position.
  228. *
  229. * @param avctx contains the current codec context
  230. * @param buf pointer to the packet to process
  231. * @param buf_size size of packet to process
  232. * @todo TODO: Implement cropping
  233. * @todo TODO: Implement forcing of subtitles
  234. */
  235. static void parse_presentation_segment(AVCodecContext *avctx,
  236. const uint8_t *buf, int buf_size)
  237. {
  238. PGSSubContext *ctx = avctx->priv_data;
  239. int w = bytestream_get_be16(&buf);
  240. int h = bytestream_get_be16(&buf);
  241. uint16_t object_index;
  242. av_dlog(avctx, "Video Dimensions %dx%d\n",
  243. w, h);
  244. if (av_image_check_size(w, h, 0, avctx) >= 0)
  245. avcodec_set_dimensions(avctx, w, h);
  246. /* Skip 1 bytes of unknown, frame rate? */
  247. buf++;
  248. ctx->presentation.id_number = bytestream_get_be16(&buf);
  249. /*
  250. * Skip 3 bytes of unknown:
  251. * state
  252. * palette_update_flag (0x80),
  253. * palette_id_to_use,
  254. */
  255. buf += 3;
  256. ctx->presentation.object_count = bytestream_get_byte(&buf);
  257. if (!ctx->presentation.object_count)
  258. return;
  259. /* Verify that enough bytes are remaining for all of the objects. */
  260. buf_size -= 11;
  261. if (buf_size < ctx->presentation.object_count * 8) {
  262. ctx->presentation.object_count = 0;
  263. return;
  264. }
  265. av_freep(&ctx->presentation.objects);
  266. ctx->presentation.objects = av_malloc(sizeof(PGSSubPictureReference) * ctx->presentation.object_count);
  267. if (!ctx->presentation.objects) {
  268. ctx->presentation.object_count = 0;
  269. return;
  270. }
  271. for (object_index = 0; object_index < ctx->presentation.object_count; ++object_index) {
  272. PGSSubPictureReference *reference = &ctx->presentation.objects[object_index];
  273. reference->picture_id = bytestream_get_be16(&buf);
  274. /*
  275. * Skip 2 bytes of unknown:
  276. * window_id_ref,
  277. * composition_flag (0x80 - object cropped, 0x40 - object forced)
  278. */
  279. buf += 2;
  280. reference->x = bytestream_get_be16(&buf);
  281. reference->y = bytestream_get_be16(&buf);
  282. /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
  283. av_dlog(avctx, "Subtitle Placement ID=%d, x=%d, y=%d\n", reference->picture_id, reference->x, reference->y);
  284. if (reference->x > avctx->width || reference->y > avctx->height) {
  285. av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
  286. reference->x, reference->y, avctx->width, avctx->height);
  287. reference->x = 0;
  288. reference->y = 0;
  289. }
  290. }
  291. }
  292. /**
  293. * Parse the display segment packet.
  294. *
  295. * The display segment controls the updating of the display.
  296. *
  297. * @param avctx contains the current codec context
  298. * @param data pointer to the data pertaining the subtitle to display
  299. * @param buf pointer to the packet to process
  300. * @param buf_size size of packet to process
  301. * @todo TODO: Fix start time, relies on correct PTS, currently too late
  302. *
  303. * @todo TODO: Fix end time, normally cleared by a second display
  304. * @todo segment, which is currently ignored as it clears
  305. * @todo the subtitle too early.
  306. */
  307. static int display_end_segment(AVCodecContext *avctx, void *data,
  308. const uint8_t *buf, int buf_size)
  309. {
  310. AVSubtitle *sub = data;
  311. PGSSubContext *ctx = avctx->priv_data;
  312. uint16_t rect;
  313. /*
  314. * The end display time is a timeout value and is only reached
  315. * if the next subtitle is later than timeout or subtitle has
  316. * not been cleared by a subsequent empty display command.
  317. */
  318. memset(sub, 0, sizeof(*sub));
  319. // Blank if last object_count was 0.
  320. if (!ctx->presentation.object_count)
  321. return 1;
  322. sub->start_display_time = 0;
  323. sub->end_display_time = 20000;
  324. sub->format = 0;
  325. sub->num_rects = ctx->presentation.object_count;
  326. sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
  327. for (rect = 0; rect < sub->num_rects; ++rect) {
  328. uint16_t picture_id = ctx->presentation.objects[rect].picture_id;
  329. sub->rects[rect] = av_mallocz(sizeof(*sub->rects[rect]));
  330. sub->rects[rect]->x = ctx->presentation.objects[rect].x;
  331. sub->rects[rect]->y = ctx->presentation.objects[rect].y;
  332. sub->rects[rect]->w = ctx->pictures[picture_id].w;
  333. sub->rects[rect]->h = ctx->pictures[picture_id].h;
  334. sub->rects[rect]->type = SUBTITLE_BITMAP;
  335. /* Process bitmap */
  336. sub->rects[rect]->pict.linesize[0] = ctx->pictures[picture_id].w;
  337. if (ctx->pictures[picture_id].rle) {
  338. if (ctx->pictures[picture_id].rle_remaining_len)
  339. av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
  340. ctx->pictures[picture_id].rle_data_len, ctx->pictures[picture_id].rle_remaining_len);
  341. if (decode_rle(avctx, sub, rect, ctx->pictures[picture_id].rle, ctx->pictures[picture_id].rle_data_len) < 0)
  342. return 0;
  343. }
  344. /* Allocate memory for colors */
  345. sub->rects[rect]->nb_colors = 256;
  346. sub->rects[rect]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  347. memcpy(sub->rects[rect]->pict.data[1], ctx->clut, sub->rects[rect]->nb_colors * sizeof(uint32_t));
  348. }
  349. return 1;
  350. }
  351. static int decode(AVCodecContext *avctx, void *data, int *data_size,
  352. AVPacket *avpkt)
  353. {
  354. const uint8_t *buf = avpkt->data;
  355. int buf_size = avpkt->size;
  356. const uint8_t *buf_end;
  357. uint8_t segment_type;
  358. int segment_length;
  359. int i;
  360. av_dlog(avctx, "PGS sub packet:\n");
  361. for (i = 0; i < buf_size; i++) {
  362. av_dlog(avctx, "%02x ", buf[i]);
  363. if (i % 16 == 15)
  364. av_dlog(avctx, "\n");
  365. }
  366. if (i & 15)
  367. av_dlog(avctx, "\n");
  368. *data_size = 0;
  369. /* Ensure that we have received at a least a segment code and segment length */
  370. if (buf_size < 3)
  371. return -1;
  372. buf_end = buf + buf_size;
  373. /* Step through buffer to identify segments */
  374. while (buf < buf_end) {
  375. segment_type = bytestream_get_byte(&buf);
  376. segment_length = bytestream_get_be16(&buf);
  377. av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
  378. if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
  379. break;
  380. switch (segment_type) {
  381. case PALETTE_SEGMENT:
  382. parse_palette_segment(avctx, buf, segment_length);
  383. break;
  384. case PICTURE_SEGMENT:
  385. parse_picture_segment(avctx, buf, segment_length);
  386. break;
  387. case PRESENTATION_SEGMENT:
  388. parse_presentation_segment(avctx, buf, segment_length);
  389. break;
  390. case WINDOW_SEGMENT:
  391. /*
  392. * Window Segment Structure (No new information provided):
  393. * 2 bytes: Unkown,
  394. * 2 bytes: X position of subtitle,
  395. * 2 bytes: Y position of subtitle,
  396. * 2 bytes: Width of subtitle,
  397. * 2 bytes: Height of subtitle.
  398. */
  399. break;
  400. case DISPLAY_SEGMENT:
  401. *data_size = display_end_segment(avctx, data, buf, segment_length);
  402. break;
  403. default:
  404. av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
  405. segment_type, segment_length);
  406. break;
  407. }
  408. buf += segment_length;
  409. }
  410. return buf_size;
  411. }
  412. AVCodec ff_pgssub_decoder = {
  413. .name = "pgssub",
  414. .type = AVMEDIA_TYPE_SUBTITLE,
  415. .id = CODEC_ID_HDMV_PGS_SUBTITLE,
  416. .priv_data_size = sizeof(PGSSubContext),
  417. .init = init_decoder,
  418. .close = close_decoder,
  419. .decode = decode,
  420. .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
  421. };