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.

461 lines
14KB

  1. /*
  2. * DVB subtitle encoding
  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. #include "libavutil/colorspace.h"
  24. typedef struct DVBSubtitleContext {
  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. static void dvb_encode_rle8(uint8_t **pq,
  185. const uint8_t *bitmap, int linesize,
  186. int w, int h)
  187. {
  188. uint8_t *q;
  189. int x, y, len, x1, color;
  190. q = *pq;
  191. for (y = 0; y < h; y++) {
  192. *q++ = 0x12;
  193. x = 0;
  194. while (x < w) {
  195. x1 = x;
  196. color = bitmap[x1++];
  197. while (x1 < w && bitmap[x1] == color)
  198. x1++;
  199. len = x1 - x;
  200. if (len == 1 && color) {
  201. // 00000001 to 11111111 1 pixel in colour x
  202. *q++ = color;
  203. } else {
  204. if (color == 0x00) {
  205. // 00000000 0LLLLLLL L pixels (1-127) in colour 0 (L > 0)
  206. len = FFMIN(len, 127);
  207. *q++ = 0x00;
  208. *q++ = len;
  209. } else if (len > 2) {
  210. // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
  211. len = FFMIN(len, 127);
  212. *q++ = 0x00;
  213. *q++ = 0x80+len;
  214. *q++ = color;
  215. }
  216. else if (len == 2) {
  217. *q++ = color;
  218. *q++ = color;
  219. } else {
  220. *q++ = color;
  221. len = 1;
  222. }
  223. }
  224. x += len;
  225. }
  226. /* end of line */
  227. // 00000000 00000000 end of 8-bit/pixel_code_string
  228. *q++ = 0x00;
  229. *q++ = 0x00;
  230. bitmap += linesize;
  231. }
  232. *pq = q;
  233. }
  234. static int encode_dvb_subtitles(DVBSubtitleContext *s,
  235. uint8_t *outbuf, const AVSubtitle *h)
  236. {
  237. uint8_t *q, *pseg_len;
  238. int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
  239. q = outbuf;
  240. page_id = 1;
  241. if (h->num_rects && !h->rects)
  242. return -1;
  243. /* page composition segment */
  244. *q++ = 0x0f; /* sync_byte */
  245. *q++ = 0x10; /* segment_type */
  246. bytestream_put_be16(&q, page_id);
  247. pseg_len = q;
  248. q += 2; /* segment length */
  249. *q++ = 30; /* page_timeout (seconds) */
  250. page_state = 2; /* mode change */
  251. /* page_version = 0 + page_state */
  252. *q++ = (s->object_version << 4) | (page_state << 2) | 3;
  253. for (region_id = 0; region_id < h->num_rects; region_id++) {
  254. *q++ = region_id;
  255. *q++ = 0xff; /* reserved */
  256. bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
  257. bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
  258. }
  259. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  260. if (h->num_rects) {
  261. for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
  262. /* CLUT segment */
  263. if (h->rects[clut_id]->nb_colors <= 4) {
  264. /* 2 bpp, some decoders do not support it correctly */
  265. bpp_index = 0;
  266. } else if (h->rects[clut_id]->nb_colors <= 16) {
  267. /* 4 bpp, standard encoding */
  268. bpp_index = 1;
  269. } else if (h->rects[clut_id]->nb_colors <= 256) {
  270. /* 8 bpp, standard encoding */
  271. bpp_index = 2;
  272. } else {
  273. return -1;
  274. }
  275. /* CLUT segment */
  276. *q++ = 0x0f; /* sync byte */
  277. *q++ = 0x12; /* CLUT definition segment */
  278. bytestream_put_be16(&q, page_id);
  279. pseg_len = q;
  280. q += 2; /* segment length */
  281. *q++ = clut_id;
  282. *q++ = (0 << 4) | 0xf; /* version = 0 */
  283. for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
  284. *q++ = i; /* clut_entry_id */
  285. *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
  286. {
  287. int a, r, g, b;
  288. uint32_t x= ((uint32_t*)h->rects[clut_id]->pict.data[1])[i];
  289. a = (x >> 24) & 0xff;
  290. r = (x >> 16) & 0xff;
  291. g = (x >> 8) & 0xff;
  292. b = (x >> 0) & 0xff;
  293. *q++ = RGB_TO_Y_CCIR(r, g, b);
  294. *q++ = RGB_TO_V_CCIR(r, g, b, 0);
  295. *q++ = RGB_TO_U_CCIR(r, g, b, 0);
  296. *q++ = 255 - a;
  297. }
  298. }
  299. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  300. }
  301. }
  302. for (region_id = 0; region_id < h->num_rects; region_id++) {
  303. /* region composition segment */
  304. if (h->rects[region_id]->nb_colors <= 4) {
  305. /* 2 bpp, some decoders do not support it correctly */
  306. bpp_index = 0;
  307. } else if (h->rects[region_id]->nb_colors <= 16) {
  308. /* 4 bpp, standard encoding */
  309. bpp_index = 1;
  310. } else {
  311. return -1;
  312. }
  313. *q++ = 0x0f; /* sync_byte */
  314. *q++ = 0x11; /* segment_type */
  315. bytestream_put_be16(&q, page_id);
  316. pseg_len = q;
  317. q += 2; /* segment length */
  318. *q++ = region_id;
  319. *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
  320. bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
  321. bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
  322. *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
  323. *q++ = region_id; /* clut_id == region_id */
  324. *q++ = 0; /* 8 bit fill colors */
  325. *q++ = 0x03; /* 4 bit and 2 bit fill colors */
  326. bytestream_put_be16(&q, region_id); /* object_id == region_id */
  327. *q++ = (0 << 6) | (0 << 4);
  328. *q++ = 0;
  329. *q++ = 0xf0;
  330. *q++ = 0;
  331. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  332. }
  333. if (h->num_rects) {
  334. for (object_id = 0; object_id < h->num_rects; object_id++) {
  335. void (*dvb_encode_rle)(uint8_t **pq,
  336. const uint8_t *bitmap, int linesize,
  337. int w, int h);
  338. /* bpp_index maths */
  339. if (h->rects[object_id]->nb_colors <= 4) {
  340. /* 2 bpp, some decoders do not support it correctly */
  341. dvb_encode_rle = dvb_encode_rle2;
  342. } else if (h->rects[object_id]->nb_colors <= 16) {
  343. /* 4 bpp, standard encoding */
  344. dvb_encode_rle = dvb_encode_rle4;
  345. } else if (h->rects[object_id]->nb_colors <= 256) {
  346. /* 8 bpp, standard encoding */
  347. dvb_encode_rle = dvb_encode_rle8;
  348. } else {
  349. return -1;
  350. }
  351. /* Object Data segment */
  352. *q++ = 0x0f; /* sync byte */
  353. *q++ = 0x13;
  354. bytestream_put_be16(&q, page_id);
  355. pseg_len = q;
  356. q += 2; /* segment length */
  357. bytestream_put_be16(&q, object_id);
  358. *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
  359. onject_coding_method,
  360. non_modifying_color_flag */
  361. {
  362. uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
  363. ptop_field_len = q;
  364. q += 2;
  365. pbottom_field_len = q;
  366. q += 2;
  367. top_ptr = q;
  368. dvb_encode_rle(&q, h->rects[object_id]->pict.data[0], h->rects[object_id]->w * 2,
  369. h->rects[object_id]->w, h->rects[object_id]->h >> 1);
  370. bottom_ptr = q;
  371. dvb_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
  372. h->rects[object_id]->w * 2, h->rects[object_id]->w,
  373. h->rects[object_id]->h >> 1);
  374. bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
  375. bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
  376. }
  377. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  378. }
  379. }
  380. /* end of display set segment */
  381. *q++ = 0x0f; /* sync_byte */
  382. *q++ = 0x80; /* segment_type */
  383. bytestream_put_be16(&q, page_id);
  384. pseg_len = q;
  385. q += 2; /* segment length */
  386. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  387. s->object_version = (s->object_version + 1) & 0xf;
  388. return q - outbuf;
  389. }
  390. static int dvbsub_encode(AVCodecContext *avctx,
  391. unsigned char *buf, int buf_size,
  392. const AVSubtitle *sub)
  393. {
  394. DVBSubtitleContext *s = avctx->priv_data;
  395. int ret;
  396. ret = encode_dvb_subtitles(s, buf, sub);
  397. return ret;
  398. }
  399. AVCodec ff_dvbsub_encoder = {
  400. .name = "dvbsub",
  401. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  402. .type = AVMEDIA_TYPE_SUBTITLE,
  403. .id = AV_CODEC_ID_DVB_SUBTITLE,
  404. .priv_data_size = sizeof(DVBSubtitleContext),
  405. .encode_sub = dvbsub_encode,
  406. };