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.

499 lines
15KB

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