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.

467 lines
14KB

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