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.

233 lines
7.4KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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, const AVSubtitle *h)
  102. {
  103. uint64_t startTime = h->pts / 1000; // FIXME: need better solution...
  104. uint64_t endTime = startTime + h->end_display_time - h->start_display_time;
  105. int start_tc[4], end_tc[4];
  106. uint8_t *hdr = buf + 27; // Point behind the timestamp
  107. uint8_t *rlelenptr;
  108. uint16_t width, height;
  109. int i;
  110. PutBitContext pb;
  111. if (bufsize < 27 + 7*2 + 4*3) {
  112. av_log(avctx, AV_LOG_ERROR, "Buffer too small for XSUB header.\n");
  113. return -1;
  114. }
  115. // TODO: support multiple rects
  116. if (h->num_rects > 1)
  117. av_log(avctx, AV_LOG_WARNING, "Only single rects supported (%d in subtitle.)\n", h->num_rects);
  118. #if FF_API_AVPICTURE
  119. FF_DISABLE_DEPRECATION_WARNINGS
  120. if (!h->rects[0]->data[0]) {
  121. AVSubtitleRect *rect = h->rects[0];
  122. int j;
  123. for (j = 0; j < 4; j++) {
  124. rect->data[j] = rect->pict.data[j];
  125. rect->linesize[j] = rect->pict.linesize[j];
  126. }
  127. }
  128. FF_ENABLE_DEPRECATION_WARNINGS
  129. #endif
  130. // TODO: render text-based subtitles into bitmaps
  131. if (!h->rects[0]->data[0] || !h->rects[0]->data[1]) {
  132. av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n");
  133. return -1;
  134. }
  135. // TODO: color reduction, similar to dvdsub encoder
  136. if (h->rects[0]->nb_colors > 4)
  137. av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors);
  138. // TODO: Palette swapping if color zero is not transparent
  139. if (((uint32_t *)h->rects[0]->data[1])[0] & 0xff)
  140. av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n");
  141. if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) {
  142. av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n");
  143. return -1;
  144. }
  145. snprintf(buf, 28,
  146. "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]",
  147. start_tc[3], start_tc[2], start_tc[1], start_tc[0],
  148. end_tc[3], end_tc[2], end_tc[1], end_tc[0]);
  149. // Width and height must probably be multiples of 2.
  150. // 2 pixels required on either side of subtitle.
  151. // Possibly due to limitations of hardware renderers.
  152. // TODO: check if the bitmap is already padded
  153. width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2;
  154. height = FFALIGN(h->rects[0]->h, 2);
  155. bytestream_put_le16(&hdr, width);
  156. bytestream_put_le16(&hdr, height);
  157. bytestream_put_le16(&hdr, h->rects[0]->x);
  158. bytestream_put_le16(&hdr, h->rects[0]->y);
  159. bytestream_put_le16(&hdr, h->rects[0]->x + width);
  160. bytestream_put_le16(&hdr, h->rects[0]->y + height);
  161. rlelenptr = hdr; // Will store length of first field here later.
  162. hdr+=2;
  163. // Palette
  164. for (i=0; i<4; i++)
  165. bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->data[1])[i]);
  166. // Bitmap
  167. // RLE buffer. Reserve 2 bytes for possible padding after the last row.
  168. init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
  169. if (xsub_encode_rle(&pb, h->rects[0]->data[0],
  170. h->rects[0]->linesize[0] * 2,
  171. h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
  172. return -1;
  173. bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field
  174. if (xsub_encode_rle(&pb, h->rects[0]->data[0] + h->rects[0]->linesize[0],
  175. h->rects[0]->linesize[0] * 2,
  176. h->rects[0]->w, h->rects[0]->h >> 1))
  177. return -1;
  178. // Enforce total height to be be multiple of 2
  179. if (h->rects[0]->h & 1) {
  180. put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
  181. avpriv_align_put_bits(&pb);
  182. }
  183. flush_put_bits(&pb);
  184. return hdr - buf + put_bits_count(&pb)/8;
  185. }
  186. static av_cold int xsub_encoder_init(AVCodecContext *avctx)
  187. {
  188. if (!avctx->codec_tag)
  189. avctx->codec_tag = MKTAG('D','X','S','B');
  190. return 0;
  191. }
  192. AVCodec ff_xsub_encoder = {
  193. .name = "xsub",
  194. .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
  195. .type = AVMEDIA_TYPE_SUBTITLE,
  196. .id = AV_CODEC_ID_XSUB,
  197. .init = xsub_encoder_init,
  198. .encode_sub = xsub_encode,
  199. };