dvbsub.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. uint32_t x= ((uint32_t*)h->rects[clut_id]->pict.data[1])[i];
  240. a = (x >> 24) & 0xff;
  241. r = (x >> 16) & 0xff;
  242. g = (x >> 8) & 0xff;
  243. b = (x >> 0) & 0xff;
  244. *q++ = RGB_TO_Y_CCIR(r, g, b);
  245. *q++ = RGB_TO_V_CCIR(r, g, b, 0);
  246. *q++ = RGB_TO_U_CCIR(r, g, b, 0);
  247. *q++ = 255 - a;
  248. }
  249. }
  250. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  251. }
  252. }
  253. for (region_id = 0; region_id < h->num_rects; region_id++) {
  254. /* region composition segment */
  255. if (h->rects[region_id]->nb_colors <= 4) {
  256. /* 2 bpp, some decoders do not support it correctly */
  257. bpp_index = 0;
  258. } else if (h->rects[region_id]->nb_colors <= 16) {
  259. /* 4 bpp, standard encoding */
  260. bpp_index = 1;
  261. } else {
  262. return -1;
  263. }
  264. *q++ = 0x0f; /* sync_byte */
  265. *q++ = 0x11; /* segment_type */
  266. bytestream_put_be16(&q, page_id);
  267. pseg_len = q;
  268. q += 2; /* segment length */
  269. *q++ = region_id;
  270. *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
  271. bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
  272. bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
  273. *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
  274. *q++ = region_id; /* clut_id == region_id */
  275. *q++ = 0; /* 8 bit fill colors */
  276. *q++ = 0x03; /* 4 bit and 2 bit fill colors */
  277. if (!s->hide_state) {
  278. bytestream_put_be16(&q, region_id); /* object_id == region_id */
  279. *q++ = (0 << 6) | (0 << 4);
  280. *q++ = 0;
  281. *q++ = 0xf0;
  282. *q++ = 0;
  283. }
  284. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  285. }
  286. if (!s->hide_state) {
  287. for (object_id = 0; object_id < h->num_rects; object_id++) {
  288. /* Object Data segment */
  289. if (h->rects[object_id]->nb_colors <= 4) {
  290. /* 2 bpp, some decoders do not support it correctly */
  291. bpp_index = 0;
  292. } else if (h->rects[object_id]->nb_colors <= 16) {
  293. /* 4 bpp, standard encoding */
  294. bpp_index = 1;
  295. } else {
  296. return -1;
  297. }
  298. *q++ = 0x0f; /* sync byte */
  299. *q++ = 0x13;
  300. bytestream_put_be16(&q, page_id);
  301. pseg_len = q;
  302. q += 2; /* segment length */
  303. bytestream_put_be16(&q, object_id);
  304. *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
  305. onject_coding_method,
  306. non_modifying_color_flag */
  307. {
  308. uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
  309. void (*dvb_encode_rle)(uint8_t **pq,
  310. const uint8_t *bitmap, int linesize,
  311. int w, int h);
  312. ptop_field_len = q;
  313. q += 2;
  314. pbottom_field_len = q;
  315. q += 2;
  316. if (bpp_index == 0)
  317. dvb_encode_rle = dvb_encode_rle2;
  318. else
  319. dvb_encode_rle = dvb_encode_rle4;
  320. top_ptr = q;
  321. dvb_encode_rle(&q, h->rects[object_id]->pict.data[0], h->rects[object_id]->w * 2,
  322. h->rects[object_id]->w, h->rects[object_id]->h >> 1);
  323. bottom_ptr = q;
  324. dvb_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
  325. h->rects[object_id]->w * 2, h->rects[object_id]->w,
  326. h->rects[object_id]->h >> 1);
  327. bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
  328. bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
  329. }
  330. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  331. }
  332. }
  333. /* end of display set segment */
  334. *q++ = 0x0f; /* sync_byte */
  335. *q++ = 0x80; /* segment_type */
  336. bytestream_put_be16(&q, page_id);
  337. pseg_len = q;
  338. q += 2; /* segment length */
  339. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  340. *q++ = 0xff; /* end of PES data */
  341. s->object_version = (s->object_version + 1) & 0xf;
  342. s->hide_state = !s->hide_state;
  343. return q - outbuf;
  344. }
  345. static int dvbsub_encode(AVCodecContext *avctx,
  346. unsigned char *buf, int buf_size, void *data)
  347. {
  348. DVBSubtitleContext *s = avctx->priv_data;
  349. AVSubtitle *sub = data;
  350. int ret;
  351. ret = encode_dvb_subtitles(s, buf, sub);
  352. return ret;
  353. }
  354. AVCodec dvbsub_encoder = {
  355. "dvbsub",
  356. CODEC_TYPE_SUBTITLE,
  357. CODEC_ID_DVB_SUBTITLE,
  358. sizeof(DVBSubtitleContext),
  359. NULL,
  360. dvbsub_encode,
  361. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  362. };