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.

492 lines
14KB

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