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.

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