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.

236 lines
7.6KB

  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 "internal.h"
  25. #include "put_bits.h"
  26. /**
  27. * Number of pixels to pad left and right.
  28. *
  29. * The official encoder pads the subtitles with two pixels on either side,
  30. * but until we find out why, we won't do it (we will pad to have width
  31. * divisible by 2 though).
  32. */
  33. #define PADDING 0
  34. #define PADDING_COLOR 0
  35. /**
  36. * Encode a single color run. At most 16 bits will be used.
  37. * @param len length of the run, values > 255 mean "until end of line", may not be < 0.
  38. * @param color color to encode, only the lowest two bits are used and all others must be 0.
  39. */
  40. static void put_xsub_rle(PutBitContext *pb, int len, int color)
  41. {
  42. if (len <= 255)
  43. put_bits(pb, 2 + ((ff_log2_tab[len] >> 1) << 2), len);
  44. else
  45. put_bits(pb, 14, 0);
  46. put_bits(pb, 2, color);
  47. }
  48. /**
  49. * Encode a 4-color bitmap with XSUB rle.
  50. *
  51. * The encoded bitmap may be wider than the source bitmap due to padding.
  52. */
  53. static int xsub_encode_rle(PutBitContext *pb, const uint8_t *bitmap,
  54. int linesize, int w, int h)
  55. {
  56. int x0, x1, y, len, color = PADDING_COLOR;
  57. for (y = 0; y < h; y++) {
  58. x0 = 0;
  59. while (x0 < w) {
  60. // Make sure we have enough room for at least one run and padding
  61. if (pb->size_in_bits - put_bits_count(pb) < 7*8)
  62. return AVERROR_BUFFER_TOO_SMALL;
  63. x1 = x0;
  64. color = bitmap[x1++] & 3;
  65. while (x1 < w && (bitmap[x1] & 3) == color)
  66. x1++;
  67. len = x1 - x0;
  68. if (PADDING && x0 == 0) {
  69. if (color == PADDING_COLOR) {
  70. len += PADDING;
  71. x0 -= PADDING;
  72. } else
  73. put_xsub_rle(pb, PADDING, PADDING_COLOR);
  74. }
  75. // Run can't be longer than 255, unless it is the rest of a row
  76. if (x1 == w && color == PADDING_COLOR) {
  77. len += PADDING + (w&1);
  78. } else
  79. len = FFMIN(len, 255);
  80. put_xsub_rle(pb, len, color);
  81. x0 += len;
  82. }
  83. if (color != PADDING_COLOR && (PADDING + (w&1)))
  84. put_xsub_rle(pb, PADDING + (w&1), PADDING_COLOR);
  85. align_put_bits(pb);
  86. bitmap += linesize;
  87. }
  88. return 0;
  89. }
  90. static int make_tc(uint64_t ms, int *tc)
  91. {
  92. static const int tc_divs[3] = { 1000, 60, 60 };
  93. int i;
  94. for (i=0; i<3; i++) {
  95. tc[i] = ms % tc_divs[i];
  96. ms /= tc_divs[i];
  97. }
  98. tc[3] = ms;
  99. return ms > 99;
  100. }
  101. static int xsub_encode(AVCodecContext *avctx, unsigned char *buf,
  102. int bufsize, const AVSubtitle *h)
  103. {
  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 AVERROR_BUFFER_TOO_SMALL;
  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. #if FF_API_AVPICTURE
  120. FF_DISABLE_DEPRECATION_WARNINGS
  121. if (!h->rects[0]->data[0]) {
  122. AVSubtitleRect *rect = h->rects[0];
  123. int j;
  124. for (j = 0; j < 4; j++) {
  125. rect->data[j] = rect->pict.data[j];
  126. rect->linesize[j] = rect->pict.linesize[j];
  127. }
  128. }
  129. FF_ENABLE_DEPRECATION_WARNINGS
  130. #endif
  131. // TODO: render text-based subtitles into bitmaps
  132. if (!h->rects[0]->data[0] || !h->rects[0]->data[1]) {
  133. av_log(avctx, AV_LOG_WARNING, "No subtitle bitmap available.\n");
  134. return AVERROR(EINVAL);
  135. }
  136. // TODO: color reduction, similar to dvdsub encoder
  137. if (h->rects[0]->nb_colors > 4)
  138. av_log(avctx, AV_LOG_WARNING, "No more than 4 subtitle colors supported (%d found.)\n", h->rects[0]->nb_colors);
  139. // TODO: Palette swapping if color zero is not transparent
  140. if (((uint32_t *)h->rects[0]->data[1])[0] & 0xff000000)
  141. av_log(avctx, AV_LOG_WARNING, "Color index 0 is not transparent. Transparency will be messed up.\n");
  142. if (make_tc(startTime, start_tc) || make_tc(endTime, end_tc)) {
  143. av_log(avctx, AV_LOG_WARNING, "Time code >= 100 hours.\n");
  144. return AVERROR(EINVAL);
  145. }
  146. snprintf(buf, 28,
  147. "[%02d:%02d:%02d.%03d-%02d:%02d:%02d.%03d]",
  148. start_tc[3], start_tc[2], start_tc[1], start_tc[0],
  149. end_tc[3], end_tc[2], end_tc[1], end_tc[0]);
  150. // Width and height must probably be multiples of 2.
  151. // 2 pixels required on either side of subtitle.
  152. // Possibly due to limitations of hardware renderers.
  153. // TODO: check if the bitmap is already padded
  154. width = FFALIGN(h->rects[0]->w, 2) + PADDING * 2;
  155. height = FFALIGN(h->rects[0]->h, 2);
  156. bytestream_put_le16(&hdr, width);
  157. bytestream_put_le16(&hdr, height);
  158. bytestream_put_le16(&hdr, h->rects[0]->x);
  159. bytestream_put_le16(&hdr, h->rects[0]->y);
  160. bytestream_put_le16(&hdr, h->rects[0]->x + width -1);
  161. bytestream_put_le16(&hdr, h->rects[0]->y + height -1);
  162. rlelenptr = hdr; // Will store length of first field here later.
  163. hdr+=2;
  164. // Palette
  165. for (i=0; i<4; i++)
  166. bytestream_put_be24(&hdr, ((uint32_t *)h->rects[0]->data[1])[i]);
  167. // Bitmap
  168. // RLE buffer. Reserve 2 bytes for possible padding after the last row.
  169. init_put_bits(&pb, hdr, bufsize - (hdr - buf) - 2);
  170. if (xsub_encode_rle(&pb, h->rects[0]->data[0],
  171. h->rects[0]->linesize[0] * 2,
  172. h->rects[0]->w, (h->rects[0]->h + 1) >> 1))
  173. return AVERROR_BUFFER_TOO_SMALL;
  174. bytestream_put_le16(&rlelenptr, put_bits_count(&pb) >> 3); // Length of first field
  175. if (xsub_encode_rle(&pb, h->rects[0]->data[0] + h->rects[0]->linesize[0],
  176. h->rects[0]->linesize[0] * 2,
  177. h->rects[0]->w, h->rects[0]->h >> 1))
  178. return AVERROR_BUFFER_TOO_SMALL;
  179. // Enforce total height to be a multiple of 2
  180. if (h->rects[0]->h & 1) {
  181. put_xsub_rle(&pb, h->rects[0]->w, PADDING_COLOR);
  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. avctx->bits_per_coded_sample = 4;
  191. return 0;
  192. }
  193. AVCodec ff_xsub_encoder = {
  194. .name = "xsub",
  195. .long_name = NULL_IF_CONFIG_SMALL("DivX subtitles (XSUB)"),
  196. .type = AVMEDIA_TYPE_SUBTITLE,
  197. .id = AV_CODEC_ID_XSUB,
  198. .init = xsub_encoder_init,
  199. .encode_sub = xsub_encode,
  200. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  201. };