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.

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