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.

239 lines
7.3KB

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