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.

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