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.

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