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.

251 lines
7.1KB

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