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.

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