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.

408 lines
11KB

  1. /*
  2. * DVB subtitle encoding for ffmpeg
  3. * Copyright (c) 2005 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. typedef struct DVBSubtitleContext {
  21. int hide_state;
  22. int object_version;
  23. } DVBSubtitleContext;
  24. #define PUTBITS2(val)\
  25. {\
  26. bitbuf |= (val) << bitcnt;\
  27. bitcnt -= 2;\
  28. if (bitcnt < 0) {\
  29. bitcnt = 6;\
  30. *q++ = bitbuf;\
  31. bitbuf = 0;\
  32. }\
  33. }
  34. static void dvb_encode_rle2(uint8_t **pq,
  35. const uint8_t *bitmap, int linesize,
  36. int w, int h)
  37. {
  38. uint8_t *q;
  39. unsigned int bitbuf;
  40. int bitcnt;
  41. int x, y, len, x1, v, color;
  42. q = *pq;
  43. for(y = 0; y < h; y++) {
  44. *q++ = 0x10;
  45. bitbuf = 0;
  46. bitcnt = 6;
  47. x = 0;
  48. while (x < w) {
  49. x1 = x;
  50. color = bitmap[x1++];
  51. while (x1 < w && bitmap[x1] == color)
  52. x1++;
  53. len = x1 - x;
  54. if (color == 0 && len == 2) {
  55. PUTBITS2(0);
  56. PUTBITS2(0);
  57. PUTBITS2(1);
  58. } else if (len >= 3 && len <= 10) {
  59. v = len - 3;
  60. PUTBITS2(0);
  61. PUTBITS2((v >> 2) | 2);
  62. PUTBITS2(v & 3);
  63. PUTBITS2(color);
  64. } else if (len >= 12 && len <= 27) {
  65. v = len - 12;
  66. PUTBITS2(0);
  67. PUTBITS2(0);
  68. PUTBITS2(2);
  69. PUTBITS2(v >> 2);
  70. PUTBITS2(v & 3);
  71. PUTBITS2(color);
  72. } else if (len >= 29) {
  73. /* length = 29 ... 284 */
  74. if (len > 284)
  75. len = 284;
  76. v = len - 29;
  77. PUTBITS2(0);
  78. PUTBITS2(0);
  79. PUTBITS2(3);
  80. PUTBITS2((v >> 6));
  81. PUTBITS2((v >> 4) & 3);
  82. PUTBITS2((v >> 2) & 3);
  83. PUTBITS2(v & 3);
  84. PUTBITS2(color);
  85. } else {
  86. PUTBITS2(color);
  87. if (color == 0) {
  88. PUTBITS2(1);
  89. }
  90. len = 1;
  91. }
  92. x += len;
  93. }
  94. /* end of line */
  95. PUTBITS2(0);
  96. PUTBITS2(0);
  97. PUTBITS2(0);
  98. if (bitcnt != 6) {
  99. *q++ = bitbuf;
  100. }
  101. *q++ = 0xf0;
  102. bitmap += linesize;
  103. }
  104. *pq = q;
  105. }
  106. #define PUTBITS4(val)\
  107. {\
  108. bitbuf |= (val) << bitcnt;\
  109. bitcnt -= 4;\
  110. if (bitcnt < 0) {\
  111. bitcnt = 4;\
  112. *q++ = bitbuf;\
  113. bitbuf = 0;\
  114. }\
  115. }
  116. /* some DVB decoders only implement 4 bits/pixel */
  117. static void dvb_encode_rle4(uint8_t **pq,
  118. const uint8_t *bitmap, int linesize,
  119. int w, int h)
  120. {
  121. uint8_t *q;
  122. unsigned int bitbuf;
  123. int bitcnt;
  124. int x, y, len, x1, v, color;
  125. q = *pq;
  126. for(y = 0; y < h; y++) {
  127. *q++ = 0x11;
  128. bitbuf = 0;
  129. bitcnt = 4;
  130. x = 0;
  131. while (x < w) {
  132. x1 = x;
  133. color = bitmap[x1++];
  134. while (x1 < w && bitmap[x1] == color)
  135. x1++;
  136. len = x1 - x;
  137. if (color == 0 && len == 2) {
  138. PUTBITS4(0);
  139. PUTBITS4(0xd);
  140. } else if (color == 0 && (len >= 3 && len <= 9)) {
  141. PUTBITS4(0);
  142. PUTBITS4(len - 2);
  143. } else if (len >= 4 && len <= 7) {
  144. PUTBITS4(0);
  145. PUTBITS4(8 + len - 4);
  146. PUTBITS4(color);
  147. } else if (len >= 9 && len <= 24) {
  148. PUTBITS4(0);
  149. PUTBITS4(0xe);
  150. PUTBITS4(len - 9);
  151. PUTBITS4(color);
  152. } else if (len >= 25) {
  153. if (len > 280)
  154. len = 280;
  155. v = len - 25;
  156. PUTBITS4(0);
  157. PUTBITS4(0xf);
  158. PUTBITS4(v >> 4);
  159. PUTBITS4(v & 0xf);
  160. PUTBITS4(color);
  161. } else {
  162. PUTBITS4(color);
  163. if (color == 0) {
  164. PUTBITS4(0xc);
  165. }
  166. len = 1;
  167. }
  168. x += len;
  169. }
  170. /* end of line */
  171. PUTBITS4(0);
  172. PUTBITS4(0);
  173. if (bitcnt != 4) {
  174. *q++ = bitbuf;
  175. }
  176. *q++ = 0xf0;
  177. bitmap += linesize;
  178. }
  179. *pq = q;
  180. }
  181. #define SCALEBITS 10
  182. #define ONE_HALF (1 << (SCALEBITS - 1))
  183. #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
  184. #define RGB_TO_Y_CCIR(r, g, b) \
  185. ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
  186. FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  187. #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
  188. (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
  189. FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  190. #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
  191. (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
  192. FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  193. static inline void putbe16(uint8_t **pq, uint16_t v)
  194. {
  195. uint8_t *q;
  196. q = *pq;
  197. *q++ = v >> 8;
  198. *q++ = v;
  199. *pq = q;
  200. }
  201. static int encode_dvb_subtitles(DVBSubtitleContext *s,
  202. uint8_t *outbuf, AVSubtitle *h)
  203. {
  204. uint8_t *q, *pseg_len;
  205. int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
  206. q = outbuf;
  207. page_id = 1;
  208. region_id = 0;
  209. clut_id = 0;
  210. object_id = 0;
  211. if (h->nb_colors <= 4) {
  212. /* 2 bpp, some decoders do not support it correctly */
  213. bpp_index = 0;
  214. } else if (h->nb_colors <= 16) {
  215. /* 4 bpp, standard encoding */
  216. bpp_index = 1;
  217. } else {
  218. return -1;
  219. }
  220. *q++ = 0x00; /* subtitle_stream_id */
  221. /* page composition segment */
  222. *q++ = 0x0f; /* sync_byte */
  223. *q++ = 0x10; /* segment_type */
  224. putbe16(&q, page_id);
  225. pseg_len = q;
  226. q += 2; /* segment length */
  227. *q++ = 30; /* page_timeout (seconds) */
  228. if (s->hide_state)
  229. page_state = 0; /* normal case */
  230. else
  231. page_state = 2; /* mode change */
  232. /* page_version = 0 + page_state */
  233. *q++ = s->object_version | (page_state << 2) | 3;
  234. *q++ = region_id;
  235. *q++ = 0xff; /* reserved */
  236. putbe16(&q, 0); /* left pos */
  237. putbe16(&q, 0); /* top pos */
  238. putbe16(&pseg_len, q - pseg_len - 2);
  239. if (!s->hide_state) {
  240. /* CLUT segment */
  241. *q++ = 0x0f; /* sync byte */
  242. *q++ = 0x12; /* CLUT definition segment */
  243. putbe16(&q, page_id);
  244. pseg_len = q;
  245. q += 2; /* segment length */
  246. *q++ = clut_id;
  247. *q++ = (0 << 4) | 0xf; /* version = 0 */
  248. for(i = 0; i < h->nb_colors; i++) {
  249. *q++ = i; /* clut_entry_id */
  250. *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
  251. {
  252. int a, r, g, b;
  253. a = (h->rgba_palette[i] >> 24) & 0xff;
  254. r = (h->rgba_palette[i] >> 16) & 0xff;
  255. g = (h->rgba_palette[i] >> 8) & 0xff;
  256. b = (h->rgba_palette[i] >> 0) & 0xff;
  257. *q++ = RGB_TO_Y_CCIR(r, g, b);
  258. *q++ = RGB_TO_V_CCIR(r, g, b, 0);
  259. *q++ = RGB_TO_U_CCIR(r, g, b, 0);
  260. *q++ = 255 - a;
  261. }
  262. }
  263. putbe16(&pseg_len, q - pseg_len - 2);
  264. }
  265. /* region composition segment */
  266. *q++ = 0x0f; /* sync_byte */
  267. *q++ = 0x11; /* segment_type */
  268. putbe16(&q, page_id);
  269. pseg_len = q;
  270. q += 2; /* segment length */
  271. *q++ = region_id;
  272. *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
  273. putbe16(&q, 720); /* region width */
  274. putbe16(&q, 576); /* region height */
  275. *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
  276. *q++ = clut_id;
  277. *q++ = 0; /* 8 bit fill colors */
  278. *q++ = 0x03; /* 4 bit and 2 bit fill colors */
  279. if (!s->hide_state) {
  280. putbe16(&q, object_id);
  281. *q++ = (0 << 6) | (0 << 4) | ((h->x >> 8) & 0xf);
  282. *q++ = h->x;
  283. *q++ = 0xf0 | ((h->y >> 8) & 0xf);
  284. *q++ = h->y;
  285. }
  286. putbe16(&pseg_len, q - pseg_len - 2);
  287. if (!s->hide_state) {
  288. /* Object Data segment */
  289. *q++ = 0x0f; /* sync byte */
  290. *q++ = 0x13;
  291. putbe16(&q, page_id);
  292. pseg_len = q;
  293. q += 2; /* segment length */
  294. putbe16(&q, object_id);
  295. *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
  296. onject_coding_method,
  297. non_modifying_color_flag */
  298. {
  299. uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
  300. void (*dvb_encode_rle)(uint8_t **pq,
  301. const uint8_t *bitmap, int linesize,
  302. int w, int h);
  303. ptop_field_len = q;
  304. q += 2;
  305. pbottom_field_len = q;
  306. q += 2;
  307. if (bpp_index == 0)
  308. dvb_encode_rle = dvb_encode_rle2;
  309. else
  310. dvb_encode_rle = dvb_encode_rle4;
  311. top_ptr = q;
  312. dvb_encode_rle(&q, h->bitmap, h->w * 2, h->w, h->h >> 1);
  313. bottom_ptr = q;
  314. dvb_encode_rle(&q, h->bitmap + h->w, h->w * 2, h->w, h->h >> 1);
  315. putbe16(&ptop_field_len, bottom_ptr - top_ptr);
  316. putbe16(&pbottom_field_len, q - bottom_ptr);
  317. }
  318. putbe16(&pseg_len, q - pseg_len - 2);
  319. }
  320. /* end of display set segment */
  321. *q++ = 0x0f; /* sync_byte */
  322. *q++ = 0x80; /* segment_type */
  323. putbe16(&q, page_id);
  324. pseg_len = q;
  325. q += 2; /* segment length */
  326. putbe16(&pseg_len, q - pseg_len - 2);
  327. *q++ = 0xff; /* end of PES data */
  328. s->object_version = (s->object_version + 1) & 0xf;
  329. s->hide_state = !s->hide_state;
  330. return q - outbuf;
  331. }
  332. static int dvbsub_init_decoder(AVCodecContext *avctx)
  333. {
  334. return 0;
  335. }
  336. static int dvbsub_close_decoder(AVCodecContext *avctx)
  337. {
  338. return 0;
  339. }
  340. static int dvbsub_encode(AVCodecContext *avctx,
  341. unsigned char *buf, int buf_size, void *data)
  342. {
  343. DVBSubtitleContext *s = avctx->priv_data;
  344. AVSubtitle *sub = data;
  345. int ret;
  346. ret = encode_dvb_subtitles(s, buf, sub);
  347. return ret;
  348. }
  349. AVCodec dvbsub_encoder = {
  350. "dvbsub",
  351. CODEC_TYPE_SUBTITLE,
  352. CODEC_ID_DVB_SUBTITLE,
  353. sizeof(DVBSubtitleContext),
  354. dvbsub_init_decoder,
  355. dvbsub_encode,
  356. dvbsub_close_decoder,
  357. };