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.

469 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. int object_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_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. 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 (avctx->width < width || avctx->height < 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. */
  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. int w = bytestream_get_be16(&buf);
  219. int h = bytestream_get_be16(&buf);
  220. dprintf(avctx, "Video Dimensions %dx%d\n",
  221. w, h);
  222. if (av_image_check_size(w, h, 0, avctx) >= 0)
  223. avcodec_set_dimensions(avctx, w, h);
  224. /* Skip 1 bytes of unknown, frame rate? */
  225. buf++;
  226. ctx->presentation.id_number = bytestream_get_be16(&buf);
  227. /*
  228. * Skip 3 bytes of unknown:
  229. * state
  230. * palette_update_flag (0x80),
  231. * palette_id_to_use,
  232. */
  233. buf += 3;
  234. ctx->presentation.object_number = bytestream_get_byte(&buf);
  235. if (!ctx->presentation.object_number)
  236. return;
  237. /*
  238. * Skip 4 bytes of unknown:
  239. * object_id_ref (2 bytes),
  240. * window_id_ref,
  241. * composition_flag (0x80 - object cropped, 0x40 - object forced)
  242. */
  243. buf += 4;
  244. x = bytestream_get_be16(&buf);
  245. y = bytestream_get_be16(&buf);
  246. /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
  247. dprintf(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
  248. if (x > avctx->width || y > avctx->height) {
  249. av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
  250. x, y, avctx->width, avctx->height);
  251. x = 0; y = 0;
  252. }
  253. /* Fill in dimensions */
  254. ctx->presentation.x = x;
  255. ctx->presentation.y = y;
  256. }
  257. /**
  258. * Parse the display segment packet.
  259. *
  260. * The display segment controls the updating of the display.
  261. *
  262. * @param avctx contains the current codec context
  263. * @param data pointer to the data pertaining the subtitle to display
  264. * @param buf pointer to the packet to process
  265. * @param buf_size size of packet to process
  266. * @todo TODO: Fix start time, relies on correct PTS, currently too late
  267. *
  268. * @todo TODO: Fix end time, normally cleared by a second display
  269. * @todo segment, which is currently ignored as it clears
  270. * @todo the subtitle too early.
  271. */
  272. static int display_end_segment(AVCodecContext *avctx, void *data,
  273. const uint8_t *buf, int buf_size)
  274. {
  275. AVSubtitle *sub = data;
  276. PGSSubContext *ctx = avctx->priv_data;
  277. /*
  278. * The end display time is a timeout value and is only reached
  279. * if the next subtitle is later then timeout or subtitle has
  280. * not been cleared by a subsequent empty display command.
  281. */
  282. memset(sub, 0, sizeof(*sub));
  283. // Blank if last object_number was 0.
  284. // Note that this may be wrong for more complex subtitles.
  285. if (!ctx->presentation.object_number)
  286. return 1;
  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. };