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.

446 lines
13KB

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