dvdsubenc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #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 == NULL)
  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. for (i=0; i<h->rects[object_id]->w*h->rects[object_id]->h; ++i) {
  103. color = h->rects[object_id]->pict.data[0][i];
  104. // only count non-transparent pixels
  105. alpha = ((uint32_t*)h->rects[object_id]->pict.data[1])[color] >> 24;
  106. hist[color] += alpha;
  107. }
  108. for (color=3;; --color) {
  109. hmax = 0;
  110. imax = 0;
  111. for (i=0; i<256; ++i)
  112. if (hist[i] > hmax) {
  113. imax = i;
  114. hmax = hist[i];
  115. }
  116. if (hmax == 0)
  117. break;
  118. if (color == 0)
  119. color = 3;
  120. av_log(NULL, AV_LOG_DEBUG, "dvd_subtitle hist[%d]=%ld -> col %d\n",
  121. imax, hist[imax], color);
  122. cmap[imax] = color;
  123. hist[imax] = 0;
  124. }
  125. // encode data block
  126. q = outbuf + 4;
  127. for (object_id = 0; object_id < rects; object_id++) {
  128. offset1[object_id] = q - outbuf;
  129. // worst case memory requirement: 1 nibble per pixel..
  130. if ((q - outbuf) + h->rects[object_id]->w*h->rects[object_id]->h/2
  131. + 17*rects + 21 > outbuf_size) {
  132. av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
  133. return -1;
  134. }
  135. dvd_encode_rle(&q, h->rects[object_id]->pict.data[0],
  136. h->rects[object_id]->w*2,
  137. h->rects[object_id]->w, h->rects[object_id]->h >> 1,
  138. cmap);
  139. offset2[object_id] = q - outbuf;
  140. dvd_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
  141. h->rects[object_id]->w*2,
  142. h->rects[object_id]->w, h->rects[object_id]->h >> 1,
  143. cmap);
  144. }
  145. // set data packet size
  146. qq = outbuf + 2;
  147. bytestream_put_be16(&qq, q - outbuf);
  148. // send start display command
  149. bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
  150. bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12*rects + 2);
  151. *q++ = 0x03; // palette - 4 nibbles
  152. *q++ = 0x03; *q++ = 0x7f;
  153. *q++ = 0x04; // alpha - 4 nibbles
  154. *q++ = 0xf0; *q++ = 0x00;
  155. //*q++ = 0x0f; *q++ = 0xff;
  156. // XXX not sure if more than one rect can really be encoded..
  157. // 12 bytes per rect
  158. for (object_id = 0; object_id < rects; object_id++) {
  159. int x2 = h->rects[object_id]->x + h->rects[object_id]->w - 1;
  160. int y2 = h->rects[object_id]->y + h->rects[object_id]->h - 1;
  161. *q++ = 0x05;
  162. // x1 x2 -> 6 nibbles
  163. *q++ = h->rects[object_id]->x >> 4;
  164. *q++ = (h->rects[object_id]->x << 4) | ((x2 >> 8) & 0xf);
  165. *q++ = x2;
  166. // y1 y2 -> 6 nibbles
  167. *q++ = h->rects[object_id]->y >> 4;
  168. *q++ = (h->rects[object_id]->y << 4) | ((y2 >> 8) & 0xf);
  169. *q++ = y2;
  170. *q++ = 0x06;
  171. // offset1, offset2
  172. bytestream_put_be16(&q, offset1[object_id]);
  173. bytestream_put_be16(&q, offset2[object_id]);
  174. }
  175. *q++ = 0x01; // start command
  176. *q++ = 0xff; // terminating command
  177. // send stop display command last
  178. bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
  179. bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
  180. *q++ = 0x02; // set end
  181. *q++ = 0xff; // terminating command
  182. qq = outbuf;
  183. bytestream_put_be16(&qq, q - outbuf);
  184. av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%td\n", q - outbuf);
  185. return q - outbuf;
  186. }
  187. static int dvdsub_encode(AVCodecContext *avctx,
  188. unsigned char *buf, int buf_size, void *data)
  189. {
  190. //DVDSubtitleContext *s = avctx->priv_data;
  191. AVSubtitle *sub = data;
  192. int ret;
  193. ret = encode_dvd_subtitles(buf, buf_size, sub);
  194. return ret;
  195. }
  196. AVCodec dvdsub_encoder = {
  197. "dvdsub",
  198. CODEC_TYPE_SUBTITLE,
  199. CODEC_ID_DVD_SUBTITLE,
  200. 0,
  201. NULL,
  202. dvdsub_encode,
  203. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  204. };