pgssubdec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 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. 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 = 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 then 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. cb = bytestream_get_byte(&buf);
  204. cr = 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. if (!ctx->presentation.object_number)
  248. return;
  249. /*
  250. * Skip 4 bytes of unknown:
  251. * object_id_ref (2 bytes),
  252. * window_id_ref,
  253. * composition_flag (0x80 - object cropped, 0x40 - object forced)
  254. */
  255. buf += 4;
  256. x = bytestream_get_be16(&buf);
  257. y = bytestream_get_be16(&buf);
  258. /* TODO If cropping, cropping_x, cropping_y, cropping_width, cropping_height (all 2 bytes).*/
  259. av_dlog(avctx, "Subtitle Placement x=%d, y=%d\n", x, y);
  260. if (x > avctx->width || y > avctx->height) {
  261. av_log(avctx, AV_LOG_ERROR, "Subtitle out of video bounds. x = %d, y = %d, video width = %d, video height = %d.\n",
  262. x, y, avctx->width, avctx->height);
  263. x = 0; y = 0;
  264. }
  265. /* Fill in dimensions */
  266. ctx->presentation.x = x;
  267. ctx->presentation.y = y;
  268. }
  269. /**
  270. * Parse the display segment packet.
  271. *
  272. * The display segment controls the updating of the display.
  273. *
  274. * @param avctx contains the current codec context
  275. * @param data pointer to the data pertaining the subtitle to display
  276. * @param buf pointer to the packet to process
  277. * @param buf_size size of packet to process
  278. * @todo TODO: Fix start time, relies on correct PTS, currently too late
  279. *
  280. * @todo TODO: Fix end time, normally cleared by a second display
  281. * @todo segment, which is currently ignored as it clears
  282. * @todo the subtitle too early.
  283. */
  284. static int display_end_segment(AVCodecContext *avctx, void *data,
  285. const uint8_t *buf, int buf_size)
  286. {
  287. AVSubtitle *sub = data;
  288. PGSSubContext *ctx = avctx->priv_data;
  289. /*
  290. * The end display time is a timeout value and is only reached
  291. * if the next subtitle is later then timeout or subtitle has
  292. * not been cleared by a subsequent empty display command.
  293. */
  294. memset(sub, 0, sizeof(*sub));
  295. // Blank if last object_number was 0.
  296. // Note that this may be wrong for more complex subtitles.
  297. if (!ctx->presentation.object_number)
  298. return 1;
  299. sub->start_display_time = 0;
  300. sub->end_display_time = 20000;
  301. sub->format = 0;
  302. sub->rects = av_mallocz(sizeof(*sub->rects));
  303. sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
  304. sub->num_rects = 1;
  305. sub->rects[0]->x = ctx->presentation.x;
  306. sub->rects[0]->y = ctx->presentation.y;
  307. sub->rects[0]->w = ctx->picture.w;
  308. sub->rects[0]->h = ctx->picture.h;
  309. sub->rects[0]->type = SUBTITLE_BITMAP;
  310. /* Process bitmap */
  311. sub->rects[0]->pict.linesize[0] = ctx->picture.w;
  312. if (ctx->picture.rle) {
  313. if (ctx->picture.rle_remaining_len)
  314. av_log(avctx, AV_LOG_ERROR, "RLE data length %u is %u bytes shorter than expected\n",
  315. ctx->picture.rle_data_len, ctx->picture.rle_remaining_len);
  316. if(decode_rle(avctx, sub, ctx->picture.rle, ctx->picture.rle_data_len) < 0)
  317. return 0;
  318. }
  319. /* Allocate memory for colors */
  320. sub->rects[0]->nb_colors = 256;
  321. sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  322. memcpy(sub->rects[0]->pict.data[1], ctx->clut, sub->rects[0]->nb_colors * sizeof(uint32_t));
  323. return 1;
  324. }
  325. static int decode(AVCodecContext *avctx, void *data, int *data_size,
  326. AVPacket *avpkt)
  327. {
  328. const uint8_t *buf = avpkt->data;
  329. int buf_size = avpkt->size;
  330. const uint8_t *buf_end;
  331. uint8_t segment_type;
  332. int segment_length;
  333. #ifdef DEBUG_PACKET_CONTENTS
  334. int i;
  335. av_log(avctx, AV_LOG_INFO, "PGS sub packet:\n");
  336. for (i = 0; i < buf_size; i++) {
  337. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  338. if (i % 16 == 15)
  339. av_log(avctx, AV_LOG_INFO, "\n");
  340. }
  341. if (i & 15)
  342. av_log(avctx, AV_LOG_INFO, "\n");
  343. #endif
  344. *data_size = 0;
  345. /* Ensure that we have received at a least a segment code and segment length */
  346. if (buf_size < 3)
  347. return -1;
  348. buf_end = buf + buf_size;
  349. /* Step through buffer to identify segments */
  350. while (buf < buf_end) {
  351. segment_type = bytestream_get_byte(&buf);
  352. segment_length = bytestream_get_be16(&buf);
  353. av_dlog(avctx, "Segment Length %d, Segment Type %x\n", segment_length, segment_type);
  354. if (segment_type != DISPLAY_SEGMENT && segment_length > buf_end - buf)
  355. break;
  356. switch (segment_type) {
  357. case PALETTE_SEGMENT:
  358. parse_palette_segment(avctx, buf, segment_length);
  359. break;
  360. case PICTURE_SEGMENT:
  361. parse_picture_segment(avctx, buf, segment_length);
  362. break;
  363. case PRESENTATION_SEGMENT:
  364. parse_presentation_segment(avctx, buf, segment_length);
  365. break;
  366. case WINDOW_SEGMENT:
  367. /*
  368. * Window Segment Structure (No new information provided):
  369. * 2 bytes: Unkown,
  370. * 2 bytes: X position of subtitle,
  371. * 2 bytes: Y position of subtitle,
  372. * 2 bytes: Width of subtitle,
  373. * 2 bytes: Height of subtitle.
  374. */
  375. break;
  376. case DISPLAY_SEGMENT:
  377. *data_size = display_end_segment(avctx, data, buf, segment_length);
  378. break;
  379. default:
  380. av_log(avctx, AV_LOG_ERROR, "Unknown subtitle segment type 0x%x, length %d\n",
  381. segment_type, segment_length);
  382. break;
  383. }
  384. buf += segment_length;
  385. }
  386. return buf_size;
  387. }
  388. AVCodec ff_pgssub_decoder = {
  389. "pgssub",
  390. AVMEDIA_TYPE_SUBTITLE,
  391. CODEC_ID_HDMV_PGS_SUBTITLE,
  392. sizeof(PGSSubContext),
  393. init_decoder,
  394. NULL,
  395. close_decoder,
  396. decode,
  397. .long_name = NULL_IF_CONFIG_SMALL("HDMV Presentation Graphic Stream subtitles"),
  398. };