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.

224 lines
6.9KB

  1. /*
  2. * DVD subtitle encoding
  3. * Copyright (c) 2005 Wolfram Gloger
  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. #include "avcodec.h"
  22. #include "bytestream.h"
  23. #include "libavutil/avassert.h"
  24. // ncnt is the nibble counter
  25. #define PUTNIBBLE(val)\
  26. do {\
  27. if (ncnt++ & 1)\
  28. *q++ = bitbuf | ((val) & 0x0f);\
  29. else\
  30. bitbuf = (val) << 4;\
  31. } while(0)
  32. static void dvd_encode_rle(uint8_t **pq,
  33. const uint8_t *bitmap, int linesize,
  34. int w, int h,
  35. const int cmap[256])
  36. {
  37. uint8_t *q;
  38. unsigned int bitbuf = 0;
  39. int ncnt;
  40. int x, y, len, color;
  41. q = *pq;
  42. for (y = 0; y < h; ++y) {
  43. ncnt = 0;
  44. for(x = 0; x < w; x += len) {
  45. color = bitmap[x];
  46. for (len=1; x+len < w; ++len)
  47. if (bitmap[x+len] != color)
  48. break;
  49. color = cmap[color];
  50. av_assert0(color < 4);
  51. if (len < 0x04) {
  52. PUTNIBBLE((len << 2)|color);
  53. } else if (len < 0x10) {
  54. PUTNIBBLE(len >> 2);
  55. PUTNIBBLE((len << 2)|color);
  56. } else if (len < 0x40) {
  57. PUTNIBBLE(0);
  58. PUTNIBBLE(len >> 2);
  59. PUTNIBBLE((len << 2)|color);
  60. } else if (x+len == w) {
  61. PUTNIBBLE(0);
  62. PUTNIBBLE(0);
  63. PUTNIBBLE(0);
  64. PUTNIBBLE(color);
  65. } else {
  66. if (len > 0xff)
  67. len = 0xff;
  68. PUTNIBBLE(0);
  69. PUTNIBBLE(len >> 6);
  70. PUTNIBBLE(len >> 2);
  71. PUTNIBBLE((len << 2)|color);
  72. }
  73. }
  74. /* end of line */
  75. if (ncnt & 1)
  76. PUTNIBBLE(0);
  77. bitmap += linesize;
  78. }
  79. *pq = q;
  80. }
  81. static int encode_dvd_subtitles(uint8_t *outbuf, int outbuf_size,
  82. const AVSubtitle *h)
  83. {
  84. uint8_t *q, *qq;
  85. int object_id;
  86. int offset1[20], offset2[20];
  87. int i, imax, color, alpha, rects = h->num_rects;
  88. unsigned long hmax;
  89. unsigned long hist[256];
  90. int cmap[256];
  91. if (rects == 0 || h->rects == NULL)
  92. return -1;
  93. if (rects > 20)
  94. rects = 20;
  95. // analyze bitmaps, compress to 4 colors
  96. for (i=0; i<256; ++i) {
  97. hist[i] = 0;
  98. cmap[i] = 0;
  99. }
  100. for (object_id = 0; object_id < rects; object_id++)
  101. for (i=0; i<h->rects[object_id]->w*h->rects[object_id]->h; ++i) {
  102. color = h->rects[object_id]->pict.data[0][i];
  103. // only count non-transparent pixels
  104. alpha = ((uint32_t*)h->rects[object_id]->pict.data[1])[color] >> 24;
  105. hist[color] += alpha;
  106. }
  107. for (color=3;; --color) {
  108. hmax = 0;
  109. imax = 0;
  110. for (i=0; i<256; ++i)
  111. if (hist[i] > hmax) {
  112. imax = i;
  113. hmax = hist[i];
  114. }
  115. if (hmax == 0)
  116. break;
  117. if (color == 0)
  118. color = 3;
  119. av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
  120. imax, hist[imax], color);
  121. cmap[imax] = color;
  122. hist[imax] = 0;
  123. }
  124. // encode data block
  125. q = outbuf + 4;
  126. for (object_id = 0; object_id < rects; object_id++) {
  127. offset1[object_id] = q - outbuf;
  128. // worst case memory requirement: 1 nibble per pixel..
  129. if ((q - outbuf) + h->rects[object_id]->w*h->rects[object_id]->h/2
  130. + 17*rects + 21 > outbuf_size) {
  131. av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
  132. return -1;
  133. }
  134. dvd_encode_rle(&q, h->rects[object_id]->pict.data[0],
  135. h->rects[object_id]->w*2,
  136. h->rects[object_id]->w, h->rects[object_id]->h >> 1,
  137. cmap);
  138. offset2[object_id] = q - outbuf;
  139. dvd_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
  140. h->rects[object_id]->w*2,
  141. h->rects[object_id]->w, h->rects[object_id]->h >> 1,
  142. cmap);
  143. }
  144. // set data packet size
  145. qq = outbuf + 2;
  146. bytestream_put_be16(&qq, q - outbuf);
  147. // send start display command
  148. bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
  149. bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
  150. *q++ = 0x03; // palette - 4 nibbles
  151. *q++ = 0x03; *q++ = 0x7f;
  152. *q++ = 0x04; // alpha - 4 nibbles
  153. *q++ = 0xf0; *q++ = 0x00;
  154. //*q++ = 0x0f; *q++ = 0xff;
  155. // XXX not sure if more than one rect can really be encoded..
  156. // 12 bytes per rect
  157. for (object_id = 0; object_id < rects; object_id++) {
  158. int x2 = h->rects[object_id]->x + h->rects[object_id]->w - 1;
  159. int y2 = h->rects[object_id]->y + h->rects[object_id]->h - 1;
  160. *q++ = 0x05;
  161. // x1 x2 -> 6 nibbles
  162. *q++ = h->rects[object_id]->x >> 4;
  163. *q++ = (h->rects[object_id]->x << 4) | ((x2 >> 8) & 0xf);
  164. *q++ = x2;
  165. // y1 y2 -> 6 nibbles
  166. *q++ = h->rects[object_id]->y >> 4;
  167. *q++ = (h->rects[object_id]->y << 4) | ((y2 >> 8) & 0xf);
  168. *q++ = y2;
  169. *q++ = 0x06;
  170. // offset1, offset2
  171. bytestream_put_be16(&q, offset1[object_id]);
  172. bytestream_put_be16(&q, offset2[object_id]);
  173. }
  174. *q++ = 0x01; // start command
  175. *q++ = 0xff; // terminating command
  176. // send stop display command last
  177. bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
  178. bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
  179. *q++ = 0x02; // set end
  180. *q++ = 0xff; // terminating command
  181. qq = outbuf;
  182. bytestream_put_be16(&qq, q - outbuf);
  183. av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
  184. return q - outbuf;
  185. }
  186. static int dvdsub_encode(AVCodecContext *avctx,
  187. unsigned char *buf, int buf_size, void *data)
  188. {
  189. //DVDSubtitleContext *s = avctx->priv_data;
  190. AVSubtitle *sub = data;
  191. int ret;
  192. ret = encode_dvd_subtitles(buf, buf_size, sub);
  193. return ret;
  194. }
  195. AVCodec ff_dvdsub_encoder = {
  196. .name = "dvdsub",
  197. .type = AVMEDIA_TYPE_SUBTITLE,
  198. .id = AV_CODEC_ID_DVD_SUBTITLE,
  199. .encode = dvdsub_encode,
  200. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  201. };