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.

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