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.

438 lines
13KB

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