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.

221 lines
7.2KB

  1. /*
  2. * DivX (XSUB) subtitle encoder
  3. * Copyright (c) 2005 DivX, Inc.
  4. * Copyright (c) 2009 Bjorn Axelsson
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "put_bits.h"
  25. /**
  26. * Number of pixels to pad left and right.
  27. *
  28. * The official encoder pads the subtitles with two pixels on either side,
  29. * but until we find out why, we won't do it (we will pad to have width
  30. * divisible by 2 though).
  31. */
  32. #define PADDING 0
  33. #define PADDING_COLOR 0
  34. /**
  35. * Encode a single color run. At most 16 bits will be used.
  36. * @param len length of the run, values > 255 mean "until end of line", may not be < 0.
  37. * @param color color to encode, only the lowest two bits are used and all others must be 0.
  38. */
  39. static void put_xsub_rle(PutBitContext *pb, int len, int color)
  40. {
  41. if (len <= 255)
  42. put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len);
  43. else
  44. put_bits(pb, 14, 0);
  45. put_bits(pb, 2, color);
  46. }
  47. /**
  48. * Encode a 4-color bitmap with XSUB rle.
  49. *
  50. * The encoded bitmap may be wider than the source bitmap due to padding.
  51. */
  52. static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap,
  53. int linesize, int w, int h)
  54. {
  55. int x0, x1, y, len, color = PADDING_COLOR;
  56. for (y = 0; y < h; y++) {
  57. x0 = 0;
  58. while (x0 < w) {
  59. // Make sure we have enough room for at least one run and padding
  60. if (pb->size_in_bits - put_bits_count(pb) < 7*8)
  61. return -1;
  62. x1 = x0;
  63. color = bitmap[x1++] & 3;
  64. while (x1 < w && (bitmap[x1] & 3) == color)
  65. x1++;
  66. len = x1 - x0;
  67. if (PADDING && x0 == 0) {
  68. if (color == PADDING_COLOR) {
  69. len += PADDING;
  70. x0 -= PADDING;
  71. } else
  72. put_xsub_rle(pb, PADDING, PADDING_COLOR);
  73. }
  74. // Run can't be longer than 255, unless it is the rest of a row
  75. if (x1 == w && color == PADDING_COLOR) {
  76. len += PADDING + (w&1);
  77. } else
  78. len = FFMIN(len, 255);
  79. put_xsub_rle(pb, len, color);
  80. x0 += len;
  81. }
  82. if (color != PADDING_COLOR && (PADDING + (w&1)))
  83. put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR);
  84. avpriv_align_put_bits(pb);
  85. bitmap += linesize;
  86. }
  87. return 0;
  88. }
  89. static int make_tc(uint64_t ms, int *tc)
  90. {
  91. static const int tc_divs[3] = { 1000, 60, 60 };
  92. int i;
  93. for (i=0; i<3; i++) {
  94. tc[i] = ms % tc_divs[i];
  95. ms /= tc_divs[i];
  96. }
  97. tc[3] = ms;
  98. return ms > 99;
  99. }
  100. static int xsub_encode(AVCodecContext *avctx, unsigned char *buf,
  101. int bufsize, void *data)
  102. {
  103. AVSubtitle *h = data;
  104. uint64_t startTime = h->pts / 1000; // FIXME: need better solution...
  105. uint64_t endTime = startTime + h->end_display_time - h->start_display_time;
  106. int start_tc[4], end_tc[4];
  107. uint8_t *hdr = buf + 27; // Point behind the timestamp
  108. uint8_t *rlelenptr;
  109. uint16_t width, height;
  110. int i;
  111. PutBitContext pb;
  112. if (bufsize < 27 + 7*2 + 4*3) {
  113. av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n");
  114. return -1;
  115. }
  116. // TODO: support multiple rects
  117. if (h->num_rects != 1)
  118. av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects);
  119. // TODO: render text-based subtitles into bitmaps
  120. if (!h->rects[0]->pict.data[0] || !h->rects[0]->pict.data[1]) {
  121. av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n");
  122. return -1;
  123. }
  124. // TODO: color reduction, similar to dvdsub encoder
  125. if (h->rects[0]->nb_colors > 4)
  126. av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors);
  127. // TODO: Palette swapping if color zero is not transparent
  128. if (((uint32_t *)h->rects[0]->pict.data[1])[0] & 0xff)
  129. av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n");
  130. if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) {
  131. av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n");
  132. return -1;
  133. }
  134. snprintf(buf, 28,
  135. "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]",
  136. start_tc[3], start_tc[2], start_tc[1], start_tc[0],
  137. end_tc[3], end_tc[2], end_tc[1], end_tc[0]);
  138. // Width and height must probably be multiples of 2.
  139. // 2 pixels required on either side of subtitle.
  140. // Possibly due to limitations of hardware renderers.
  141. // TODO: check if the bitmap is already padded
  142. width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2;
  143. height = FFALIGN(h->rects[0]->h, 2);
  144. bytestream_put_le16(&hdr, width);
  145. bytestream_put_le16(&hdr, height);
  146. bytestream_put_le16(&hdr, h->rects[0]->x);
  147. bytestream_put_le16(&hdr, h->rects[0]->y);
  148. bytestream_put_le16(&hdr, h->rects[0]->x + width);
  149. bytestream_put_le16(&hdr, h->rects[0]->y + height);
  150. rlelenptr = hdr; // Will store length of first field here later.
  151. hdr+=2;
  152. // Palette
  153. for (i=0; i<4; i++)
  154. bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->pict.data[1])[i]);
  155. // Bitmap
  156. // RLE buffer. Reserve 2 bytes for possible padding after the last row.
  157. init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
  158. if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0],
  159. h->rects[0]->pict.linesize[0]*2,
  160. h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
  161. return -1;
  162. bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field
  163. if (xsub_encode_rle(&pb, h->rects[0]->pict.data[0] + h->rects[0]->pict.linesize[0],
  164. h->rects[0]->pict.linesize[0]*2,
  165. h->rects[0]->w, h->rects[0]->h >> 1))
  166. return -1;
  167. // Enforce total height to be be multiple of 2
  168. if (h->rects[0]->h & 1) {
  169. put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
  170. avpriv_align_put_bits(&pb);
  171. }
  172. flush_put_bits(&pb);
  173. return hdr - buf + put_bits_count(&pb)/8;
  174. }
  175. static av_cold int xsub_encoder_init(AVCodecContext *avctx)
  176. {
  177. if (!avctx->codec_tag)
  178. avctx->codec_tag = MKTAG('D','X','S','B');
  179. return 0;
  180. }
  181. AVCodec ff_xsub_encoder = {
  182. .name = "xsub",
  183. .type = AVMEDIA_TYPE_SUBTITLE,
  184. .id = AV_CODEC_ID_XSUB,
  185. .init = xsub_encoder_init,
  186. .encode = xsub_encode,
  187. .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
  188. };