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.

413 lines
12KB

  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. #include "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 int encode_dvb_subtitles(DVBSubtitleContext *s,
  186. uint8_t *outbuf, AVSubtitle *h)
  187. {
  188. uint8_t *q, *pseg_len;
  189. int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
  190. q = outbuf;
  191. page_id = 1;
  192. if (h->num_rects == 0 || h->rects == NULL)
  193. return -1;
  194. *q++ = 0x00; /* subtitle_stream_id */
  195. /* page composition segment */
  196. *q++ = 0x0f; /* sync_byte */
  197. *q++ = 0x10; /* segment_type */
  198. bytestream_put_be16(&q, page_id);
  199. pseg_len = q;
  200. q += 2; /* segment length */
  201. *q++ = 30; /* page_timeout (seconds) */
  202. if (s->hide_state)
  203. page_state = 0; /* normal case */
  204. else
  205. page_state = 2; /* mode change */
  206. /* page_version = 0 + page_state */
  207. *q++ = s->object_version | (page_state << 2) | 3;
  208. for (region_id = 0; region_id < h->num_rects; region_id++) {
  209. *q++ = region_id;
  210. *q++ = 0xff; /* reserved */
  211. bytestream_put_be16(&q, h->rects[region_id].x); /* left pos */
  212. bytestream_put_be16(&q, h->rects[region_id].y); /* top pos */
  213. }
  214. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  215. if (!s->hide_state) {
  216. for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
  217. /* CLUT segment */
  218. if (h->rects[clut_id].nb_colors <= 4) {
  219. /* 2 bpp, some decoders do not support it correctly */
  220. bpp_index = 0;
  221. } else if (h->rects[clut_id].nb_colors <= 16) {
  222. /* 4 bpp, standard encoding */
  223. bpp_index = 1;
  224. } else {
  225. return -1;
  226. }
  227. *q++ = 0x0f; /* sync byte */
  228. *q++ = 0x12; /* CLUT definition segment */
  229. bytestream_put_be16(&q, page_id);
  230. pseg_len = q;
  231. q += 2; /* segment length */
  232. *q++ = clut_id;
  233. *q++ = (0 << 4) | 0xf; /* version = 0 */
  234. for(i = 0; i < h->rects[clut_id].nb_colors; i++) {
  235. *q++ = i; /* clut_entry_id */
  236. *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
  237. {
  238. int a, r, g, b;
  239. a = (h->rects[clut_id].rgba_palette[i] >> 24) & 0xff;
  240. r = (h->rects[clut_id].rgba_palette[i] >> 16) & 0xff;
  241. g = (h->rects[clut_id].rgba_palette[i] >> 8) & 0xff;
  242. b = (h->rects[clut_id].rgba_palette[i] >> 0) & 0xff;
  243. *q++ = RGB_TO_Y_CCIR(r, g, b);
  244. *q++ = RGB_TO_V_CCIR(r, g, b, 0);
  245. *q++ = RGB_TO_U_CCIR(r, g, b, 0);
  246. *q++ = 255 - a;
  247. }
  248. }
  249. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  250. }
  251. }
  252. for (region_id = 0; region_id < h->num_rects; region_id++) {
  253. /* region composition segment */
  254. if (h->rects[region_id].nb_colors <= 4) {
  255. /* 2 bpp, some decoders do not support it correctly */
  256. bpp_index = 0;
  257. } else if (h->rects[region_id].nb_colors <= 16) {
  258. /* 4 bpp, standard encoding */
  259. bpp_index = 1;
  260. } else {
  261. return -1;
  262. }
  263. *q++ = 0x0f; /* sync_byte */
  264. *q++ = 0x11; /* segment_type */
  265. bytestream_put_be16(&q, page_id);
  266. pseg_len = q;
  267. q += 2; /* segment length */
  268. *q++ = region_id;
  269. *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
  270. bytestream_put_be16(&q, h->rects[region_id].w); /* region width */
  271. bytestream_put_be16(&q, h->rects[region_id].h); /* region height */
  272. *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
  273. *q++ = region_id; /* clut_id == region_id */
  274. *q++ = 0; /* 8 bit fill colors */
  275. *q++ = 0x03; /* 4 bit and 2 bit fill colors */
  276. if (!s->hide_state) {
  277. bytestream_put_be16(&q, region_id); /* object_id == region_id */
  278. *q++ = (0 << 6) | (0 << 4);
  279. *q++ = 0;
  280. *q++ = 0xf0;
  281. *q++ = 0;
  282. }
  283. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  284. }
  285. if (!s->hide_state) {
  286. for (object_id = 0; object_id < h->num_rects; object_id++) {
  287. /* Object Data segment */
  288. if (h->rects[object_id].nb_colors <= 4) {
  289. /* 2 bpp, some decoders do not support it correctly */
  290. bpp_index = 0;
  291. } else if (h->rects[object_id].nb_colors <= 16) {
  292. /* 4 bpp, standard encoding */
  293. bpp_index = 1;
  294. } else {
  295. return -1;
  296. }
  297. *q++ = 0x0f; /* sync byte */
  298. *q++ = 0x13;
  299. bytestream_put_be16(&q, page_id);
  300. pseg_len = q;
  301. q += 2; /* segment length */
  302. bytestream_put_be16(&q, object_id);
  303. *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
  304. onject_coding_method,
  305. non_modifying_color_flag */
  306. {
  307. uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
  308. void (*dvb_encode_rle)(uint8_t **pq,
  309. const uint8_t *bitmap, int linesize,
  310. int w, int h);
  311. ptop_field_len = q;
  312. q += 2;
  313. pbottom_field_len = q;
  314. q += 2;
  315. if (bpp_index == 0)
  316. dvb_encode_rle = dvb_encode_rle2;
  317. else
  318. dvb_encode_rle = dvb_encode_rle4;
  319. top_ptr = q;
  320. dvb_encode_rle(&q, h->rects[object_id].bitmap, h->rects[object_id].w * 2,
  321. h->rects[object_id].w, h->rects[object_id].h >> 1);
  322. bottom_ptr = q;
  323. dvb_encode_rle(&q, h->rects[object_id].bitmap + h->rects[object_id].w,
  324. h->rects[object_id].w * 2, h->rects[object_id].w,
  325. h->rects[object_id].h >> 1);
  326. bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
  327. bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
  328. }
  329. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  330. }
  331. }
  332. /* end of display set segment */
  333. *q++ = 0x0f; /* sync_byte */
  334. *q++ = 0x80; /* segment_type */
  335. bytestream_put_be16(&q, page_id);
  336. pseg_len = q;
  337. q += 2; /* segment length */
  338. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  339. *q++ = 0xff; /* end of PES data */
  340. s->object_version = (s->object_version + 1) & 0xf;
  341. s->hide_state = !s->hide_state;
  342. return q - outbuf;
  343. }
  344. static int dvbsub_encode(AVCodecContext *avctx,
  345. unsigned char *buf, int buf_size, void *data)
  346. {
  347. DVBSubtitleContext *s = avctx->priv_data;
  348. AVSubtitle *sub = data;
  349. int ret;
  350. ret = encode_dvb_subtitles(s, buf, sub);
  351. return ret;
  352. }
  353. AVCodec dvbsub_encoder = {
  354. "dvbsub",
  355. CODEC_TYPE_SUBTITLE,
  356. CODEC_ID_DVB_SUBTITLE,
  357. sizeof(DVBSubtitleContext),
  358. NULL,
  359. dvbsub_encode,
  360. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  361. };