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.

444 lines
13KB

  1. /*
  2. * DVB subtitle encoding for ffmpeg
  3. * Copyright (c) 2005 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. typedef struct DVBSubtitleContext {
  21. int hide_state;
  22. int object_version;
  23. } DVBSubtitleContext;
  24. #define PUTBITS2(val)\
  25. {\
  26. bitbuf |= (val) << bitcnt;\
  27. bitcnt -= 2;\
  28. if (bitcnt < 0) {\
  29. bitcnt = 6;\
  30. *q++ = bitbuf;\
  31. bitbuf = 0;\
  32. }\
  33. }
  34. static void dvb_encode_rle2(uint8_t **pq,
  35. const uint8_t *bitmap, int linesize,
  36. int w, int h)
  37. {
  38. uint8_t *q;
  39. unsigned int bitbuf;
  40. int bitcnt;
  41. int x, y, len, x1, v, color;
  42. q = *pq;
  43. for(y = 0; y < h; y++) {
  44. *q++ = 0x10;
  45. bitbuf = 0;
  46. bitcnt = 6;
  47. x = 0;
  48. while (x < w) {
  49. x1 = x;
  50. color = bitmap[x1++];
  51. while (x1 < w && bitmap[x1] == color)
  52. x1++;
  53. len = x1 - x;
  54. if (color == 0 && len == 2) {
  55. PUTBITS2(0);
  56. PUTBITS2(0);
  57. PUTBITS2(1);
  58. } else if (len >= 3 && len <= 10) {
  59. v = len - 3;
  60. PUTBITS2(0);
  61. PUTBITS2((v >> 2) | 2);
  62. PUTBITS2(v & 3);
  63. PUTBITS2(color);
  64. } else if (len >= 12 && len <= 27) {
  65. v = len - 12;
  66. PUTBITS2(0);
  67. PUTBITS2(0);
  68. PUTBITS2(2);
  69. PUTBITS2(v >> 2);
  70. PUTBITS2(v & 3);
  71. PUTBITS2(color);
  72. } else if (len >= 29) {
  73. /* length = 29 ... 284 */
  74. if (len > 284)
  75. len = 284;
  76. v = len - 29;
  77. PUTBITS2(0);
  78. PUTBITS2(0);
  79. PUTBITS2(3);
  80. PUTBITS2((v >> 6));
  81. PUTBITS2((v >> 4) & 3);
  82. PUTBITS2((v >> 2) & 3);
  83. PUTBITS2(v & 3);
  84. PUTBITS2(color);
  85. } else {
  86. PUTBITS2(color);
  87. if (color == 0) {
  88. PUTBITS2(1);
  89. }
  90. len = 1;
  91. }
  92. x += len;
  93. }
  94. /* end of line */
  95. PUTBITS2(0);
  96. PUTBITS2(0);
  97. PUTBITS2(0);
  98. if (bitcnt != 6) {
  99. *q++ = bitbuf;
  100. }
  101. *q++ = 0xf0;
  102. bitmap += linesize;
  103. }
  104. *pq = q;
  105. }
  106. #define PUTBITS4(val)\
  107. {\
  108. bitbuf |= (val) << bitcnt;\
  109. bitcnt -= 4;\
  110. if (bitcnt < 0) {\
  111. bitcnt = 4;\
  112. *q++ = bitbuf;\
  113. bitbuf = 0;\
  114. }\
  115. }
  116. /* some DVB decoders only implement 4 bits/pixel */
  117. static void dvb_encode_rle4(uint8_t **pq,
  118. const uint8_t *bitmap, int linesize,
  119. int w, int h)
  120. {
  121. uint8_t *q;
  122. unsigned int bitbuf;
  123. int bitcnt;
  124. int x, y, len, x1, v, color;
  125. q = *pq;
  126. for(y = 0; y < h; y++) {
  127. *q++ = 0x11;
  128. bitbuf = 0;
  129. bitcnt = 4;
  130. x = 0;
  131. while (x < w) {
  132. x1 = x;
  133. color = bitmap[x1++];
  134. while (x1 < w && bitmap[x1] == color)
  135. x1++;
  136. len = x1 - x;
  137. if (color == 0 && len == 2) {
  138. PUTBITS4(0);
  139. PUTBITS4(0xd);
  140. } else if (color == 0 && (len >= 3 && len <= 9)) {
  141. PUTBITS4(0);
  142. PUTBITS4(len - 2);
  143. } else if (len >= 4 && len <= 7) {
  144. PUTBITS4(0);
  145. PUTBITS4(8 + len - 4);
  146. PUTBITS4(color);
  147. } else if (len >= 9 && len <= 24) {
  148. PUTBITS4(0);
  149. PUTBITS4(0xe);
  150. PUTBITS4(len - 9);
  151. PUTBITS4(color);
  152. } else if (len >= 25) {
  153. if (len > 280)
  154. len = 280;
  155. v = len - 25;
  156. PUTBITS4(0);
  157. PUTBITS4(0xf);
  158. PUTBITS4(v >> 4);
  159. PUTBITS4(v & 0xf);
  160. PUTBITS4(color);
  161. } else {
  162. PUTBITS4(color);
  163. if (color == 0) {
  164. PUTBITS4(0xc);
  165. }
  166. len = 1;
  167. }
  168. x += len;
  169. }
  170. /* end of line */
  171. PUTBITS4(0);
  172. PUTBITS4(0);
  173. if (bitcnt != 4) {
  174. *q++ = bitbuf;
  175. }
  176. *q++ = 0xf0;
  177. bitmap += linesize;
  178. }
  179. *pq = q;
  180. }
  181. #define SCALEBITS 10
  182. #define ONE_HALF (1 << (SCALEBITS - 1))
  183. #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
  184. #define RGB_TO_Y_CCIR(r, g, b) \
  185. ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
  186. FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  187. #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
  188. (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
  189. FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  190. #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
  191. (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
  192. FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  193. static inline void putbe16(uint8_t **pq, uint16_t v)
  194. {
  195. uint8_t *q;
  196. q = *pq;
  197. *q++ = v >> 8;
  198. *q++ = v;
  199. *pq = q;
  200. }
  201. static int encode_dvb_subtitles(DVBSubtitleContext *s,
  202. uint8_t *outbuf, AVSubtitle *h)
  203. {
  204. uint8_t *q, *pseg_len;
  205. int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
  206. q = outbuf;
  207. page_id = 1;
  208. if (h->num_rects == 0 || h->rects == NULL)
  209. return -1;
  210. *q++ = 0x00; /* subtitle_stream_id */
  211. /* page composition segment */
  212. *q++ = 0x0f; /* sync_byte */
  213. *q++ = 0x10; /* segment_type */
  214. putbe16(&q, page_id);
  215. pseg_len = q;
  216. q += 2; /* segment length */
  217. *q++ = 30; /* page_timeout (seconds) */
  218. if (s->hide_state)
  219. page_state = 0; /* normal case */
  220. else
  221. page_state = 2; /* mode change */
  222. /* page_version = 0 + page_state */
  223. *q++ = s->object_version | (page_state << 2) | 3;
  224. for (region_id = 0; region_id < h->num_rects; region_id++) {
  225. *q++ = region_id;
  226. *q++ = 0xff; /* reserved */
  227. putbe16(&q, h->rects[region_id].x); /* left pos */
  228. putbe16(&q, h->rects[region_id].y); /* top pos */
  229. }
  230. putbe16(&pseg_len, q - pseg_len - 2);
  231. if (!s->hide_state) {
  232. for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
  233. /* CLUT segment */
  234. if (h->rects[clut_id].nb_colors <= 4) {
  235. /* 2 bpp, some decoders do not support it correctly */
  236. bpp_index = 0;
  237. } else if (h->rects[clut_id].nb_colors <= 16) {
  238. /* 4 bpp, standard encoding */
  239. bpp_index = 1;
  240. } else {
  241. return -1;
  242. }
  243. *q++ = 0x0f; /* sync byte */
  244. *q++ = 0x12; /* CLUT definition segment */
  245. putbe16(&q, page_id);
  246. pseg_len = q;
  247. q += 2; /* segment length */
  248. *q++ = clut_id;
  249. *q++ = (0 << 4) | 0xf; /* version = 0 */
  250. for(i = 0; i < h->rects[clut_id].nb_colors; i++) {
  251. *q++ = i; /* clut_entry_id */
  252. *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
  253. {
  254. int a, r, g, b;
  255. a = (h->rects[clut_id].rgba_palette[i] >> 24) & 0xff;
  256. r = (h->rects[clut_id].rgba_palette[i] >> 16) & 0xff;
  257. g = (h->rects[clut_id].rgba_palette[i] >> 8) & 0xff;
  258. b = (h->rects[clut_id].rgba_palette[i] >> 0) & 0xff;
  259. *q++ = RGB_TO_Y_CCIR(r, g, b);
  260. *q++ = RGB_TO_V_CCIR(r, g, b, 0);
  261. *q++ = RGB_TO_U_CCIR(r, g, b, 0);
  262. *q++ = 255 - a;
  263. }
  264. }
  265. putbe16(&pseg_len, q - pseg_len - 2);
  266. }
  267. }
  268. for (region_id = 0; region_id < h->num_rects; region_id++) {
  269. /* region composition segment */
  270. if (h->rects[region_id].nb_colors <= 4) {
  271. /* 2 bpp, some decoders do not support it correctly */
  272. bpp_index = 0;
  273. } else if (h->rects[region_id].nb_colors <= 16) {
  274. /* 4 bpp, standard encoding */
  275. bpp_index = 1;
  276. } else {
  277. return -1;
  278. }
  279. *q++ = 0x0f; /* sync_byte */
  280. *q++ = 0x11; /* segment_type */
  281. putbe16(&q, page_id);
  282. pseg_len = q;
  283. q += 2; /* segment length */
  284. *q++ = region_id;
  285. *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
  286. putbe16(&q, h->rects[region_id].w); /* region width */
  287. putbe16(&q, h->rects[region_id].h); /* region height */
  288. *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
  289. *q++ = region_id; /* clut_id == region_id */
  290. *q++ = 0; /* 8 bit fill colors */
  291. *q++ = 0x03; /* 4 bit and 2 bit fill colors */
  292. if (!s->hide_state) {
  293. putbe16(&q, region_id); /* object_id == region_id */
  294. *q++ = (0 << 6) | (0 << 4);
  295. *q++ = 0;
  296. *q++ = 0xf0;
  297. *q++ = 0;
  298. }
  299. putbe16(&pseg_len, q - pseg_len - 2);
  300. }
  301. if (!s->hide_state) {
  302. for (object_id = 0; object_id < h->num_rects; object_id++) {
  303. /* Object Data segment */
  304. if (h->rects[region_id].nb_colors <= 4) {
  305. /* 2 bpp, some decoders do not support it correctly */
  306. bpp_index = 0;
  307. } else if (h->rects[region_id].nb_colors <= 16) {
  308. /* 4 bpp, standard encoding */
  309. bpp_index = 1;
  310. } else {
  311. return -1;
  312. }
  313. *q++ = 0x0f; /* sync byte */
  314. *q++ = 0x13;
  315. putbe16(&q, page_id);
  316. pseg_len = q;
  317. q += 2; /* segment length */
  318. putbe16(&q, object_id);
  319. *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
  320. onject_coding_method,
  321. non_modifying_color_flag */
  322. {
  323. uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
  324. void (*dvb_encode_rle)(uint8_t **pq,
  325. const uint8_t *bitmap, int linesize,
  326. int w, int h);
  327. ptop_field_len = q;
  328. q += 2;
  329. pbottom_field_len = q;
  330. q += 2;
  331. if (bpp_index == 0)
  332. dvb_encode_rle = dvb_encode_rle2;
  333. else
  334. dvb_encode_rle = dvb_encode_rle4;
  335. top_ptr = q;
  336. dvb_encode_rle(&q, h->rects[object_id].bitmap, h->rects[object_id].w * 2,
  337. h->rects[object_id].w, h->rects[object_id].h >> 1);
  338. bottom_ptr = q;
  339. dvb_encode_rle(&q, h->rects[object_id].bitmap + h->rects[object_id].w,
  340. h->rects[object_id].w * 2, h->rects[object_id].w,
  341. h->rects[object_id].h >> 1);
  342. putbe16(&ptop_field_len, bottom_ptr - top_ptr);
  343. putbe16(&pbottom_field_len, q - bottom_ptr);
  344. }
  345. putbe16(&pseg_len, q - pseg_len - 2);
  346. }
  347. }
  348. /* end of display set segment */
  349. *q++ = 0x0f; /* sync_byte */
  350. *q++ = 0x80; /* segment_type */
  351. putbe16(&q, page_id);
  352. pseg_len = q;
  353. q += 2; /* segment length */
  354. putbe16(&pseg_len, q - pseg_len - 2);
  355. *q++ = 0xff; /* end of PES data */
  356. s->object_version = (s->object_version + 1) & 0xf;
  357. s->hide_state = !s->hide_state;
  358. return q - outbuf;
  359. }
  360. static int dvbsub_init_decoder(AVCodecContext *avctx)
  361. {
  362. return 0;
  363. }
  364. static int dvbsub_close_decoder(AVCodecContext *avctx)
  365. {
  366. return 0;
  367. }
  368. static int dvbsub_encode(AVCodecContext *avctx,
  369. unsigned char *buf, int buf_size, void *data)
  370. {
  371. DVBSubtitleContext *s = avctx->priv_data;
  372. AVSubtitle *sub = data;
  373. int ret;
  374. ret = encode_dvb_subtitles(s, buf, sub);
  375. return ret;
  376. }
  377. AVCodec dvbsub_encoder = {
  378. "dvbsub",
  379. CODEC_TYPE_SUBTITLE,
  380. CODEC_ID_DVB_SUBTITLE,
  381. sizeof(DVBSubtitleContext),
  382. dvbsub_init_decoder,
  383. dvbsub_encode,
  384. dvbsub_close_decoder,
  385. };