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.

248 lines
7.0KB

  1. /*
  2. * DVD subtitle encoding for ffmpeg
  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. #undef NDEBUG
  23. #include <assert.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. assert(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 inline void putbe16(uint8_t **pq, uint16_t v)
  82. {
  83. uint8_t *q = *pq;
  84. *q++ = v >> 8;
  85. *q++ = v;
  86. *pq = q;
  87. }
  88. static int encode_dvd_subtitles(uint8_t *outbuf, int outbuf_size,
  89. const AVSubtitle *h)
  90. {
  91. uint8_t *q, *qq;
  92. int object_id;
  93. int offset1[20], offset2[20];
  94. int i, imax, color, alpha, rects = h->num_rects;
  95. unsigned long hmax;
  96. unsigned long hist[256];
  97. int cmap[256];
  98. if (rects == 0 || h->rects == NULL)
  99. return -1;
  100. if (rects > 20)
  101. rects = 20;
  102. // analyze bitmaps, compress to 4 colors
  103. for (i=0; i<256; ++i) {
  104. hist[i] = 0;
  105. cmap[i] = 0;
  106. }
  107. for (object_id = 0; object_id < rects; object_id++)
  108. for (i=0; i<h->rects[object_id].w*h->rects[object_id].h; ++i) {
  109. color = h->rects[object_id].bitmap[i];
  110. // only count non-transparent pixels
  111. alpha = h->rects[object_id].rgba_palette[color] >> 24;
  112. hist[color] += alpha;
  113. }
  114. for (color=3;; --color) {
  115. hmax = 0;
  116. imax = 0;
  117. for (i=0; i<256; ++i)
  118. if (hist[i] > hmax) {
  119. imax = i;
  120. hmax = hist[i];
  121. }
  122. if (hmax == 0)
  123. break;
  124. if (color == 0)
  125. color = 3;
  126. av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
  127. imax, hist[imax], color);
  128. cmap[imax] = color;
  129. hist[imax] = 0;
  130. }
  131. // encode data block
  132. q = outbuf + 4;
  133. for (object_id = 0; object_id < rects; object_id++) {
  134. offset1[object_id] = q - outbuf;
  135. // worst case memory requirement: 1 nibble per pixel..
  136. if ((q - outbuf) + h->rects[object_id].w*h->rects[object_id].h/2
  137. + 17*rects + 21 > outbuf_size) {
  138. av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
  139. return -1;
  140. }
  141. dvd_encode_rle(&q, h->rects[object_id].bitmap,
  142. h->rects[object_id].w*2,
  143. h->rects[object_id].w, h->rects[object_id].h >> 1,
  144. cmap);
  145. offset2[object_id] = q - outbuf;
  146. dvd_encode_rle(&q, h->rects[object_id].bitmap + h->rects[object_id].w,
  147. h->rects[object_id].w*2,
  148. h->rects[object_id].w, h->rects[object_id].h >> 1,
  149. cmap);
  150. }
  151. // set data packet size
  152. qq = outbuf + 2;
  153. putbe16(&qq, q - outbuf);
  154. // send start display command
  155. putbe16(&q, (h->start_display_time*90) >> 10);
  156. putbe16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
  157. *q++ = 0x03; // palette - 4 nibbles
  158. *q++ = 0x03; *q++ = 0x7f;
  159. *q++ = 0x04; // alpha - 4 nibbles
  160. *q++ = 0xf0; *q++ = 0x00;
  161. //*q++ = 0x0f; *q++ = 0xff;
  162. // XXX not sure if more than one rect can really be encoded..
  163. // 12 bytes per rect
  164. for (object_id = 0; object_id < rects; object_id++) {
  165. int x2 = h->rects[object_id].x + h->rects[object_id].w - 1;
  166. int y2 = h->rects[object_id].y + h->rects[object_id].h - 1;
  167. *q++ = 0x05;
  168. // x1 x2 -> 6 nibbles
  169. *q++ = h->rects[object_id].x >> 4;
  170. *q++ = (h->rects[object_id].x << 4) | ((x2 >> 8) & 0xf);
  171. *q++ = x2;
  172. // y1 y2 -> 6 nibbles
  173. *q++ = h->rects[object_id].y >> 4;
  174. *q++ = (h->rects[object_id].y << 4) | ((y2 >> 8) & 0xf);
  175. *q++ = y2;
  176. *q++ = 0x06;
  177. // offset1, offset2
  178. putbe16(&q, offset1[object_id]);
  179. putbe16(&q, offset2[object_id]);
  180. }
  181. *q++ = 0x01; // start command
  182. *q++ = 0xff; // terminating command
  183. // send stop display command last
  184. putbe16(&q, (h->end_display_time*90) >> 10);
  185. putbe16(&q, (q - outbuf) - 2 /*+ 4*/);
  186. *q++ = 0x02; // set end
  187. *q++ = 0xff; // terminating command
  188. qq = outbuf;
  189. putbe16(&qq, q - outbuf);
  190. av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
  191. return q - outbuf;
  192. }
  193. static int dvdsub_init_encoder(AVCodecContext *avctx)
  194. {
  195. return 0;
  196. }
  197. static int dvdsub_close_encoder(AVCodecContext *avctx)
  198. {
  199. return 0;
  200. }
  201. static int dvdsub_encode(AVCodecContext *avctx,
  202. unsigned char *buf, int buf_size, void *data)
  203. {
  204. //DVDSubtitleContext *s = avctx->priv_data;
  205. AVSubtitle *sub = data;
  206. int ret;
  207. ret = encode_dvd_subtitles(buf, buf_size, sub);
  208. return ret;
  209. }
  210. AVCodec dvdsub_encoder = {
  211. "dvdsub",
  212. CODEC_TYPE_SUBTITLE,
  213. CODEC_ID_DVD_SUBTITLE,
  214. 0,
  215. dvdsub_init_encoder,
  216. dvdsub_encode,
  217. dvdsub_close_encoder,
  218. };
  219. /* Local Variables: */
  220. /* c-basic-offset:4 */
  221. /* End: */