/libavcodec/dvbsub.c

http://github.com/FFmpeg/FFmpeg · C · 529 lines · 421 code · 56 blank · 52 comment · 110 complexity · 4f76ff7fdc6c1efeb9e6a6f6ae0bab31 MD5 · raw file

  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 int dvb_encode_rle2(uint8_t **pq, int buf_size,
  38. const uint8_t *bitmap, int linesize,
  39. int w, int h)
  40. {
  41. uint8_t *q, *line_begin;
  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. // Worst case line is 3 bits per value + 4 bytes overhead
  48. if (buf_size * 8 < w * 3 + 32)
  49. return AVERROR_BUFFER_TOO_SMALL;
  50. line_begin = q;
  51. *q++ = 0x10;
  52. bitbuf = 0;
  53. bitcnt = 6;
  54. x = 0;
  55. while (x < w) {
  56. x1 = x;
  57. color = bitmap[x1++];
  58. while (x1 < w && bitmap[x1] == color)
  59. x1++;
  60. len = x1 - x;
  61. if (color == 0 && len == 2) {
  62. PUTBITS2(0);
  63. PUTBITS2(0);
  64. PUTBITS2(1);
  65. } else if (len >= 3 && len <= 10) {
  66. v = len - 3;
  67. PUTBITS2(0);
  68. PUTBITS2((v >> 2) | 2);
  69. PUTBITS2(v & 3);
  70. PUTBITS2(color);
  71. } else if (len >= 12 && len <= 27) {
  72. v = len - 12;
  73. PUTBITS2(0);
  74. PUTBITS2(0);
  75. PUTBITS2(2);
  76. PUTBITS2(v >> 2);
  77. PUTBITS2(v & 3);
  78. PUTBITS2(color);
  79. } else if (len >= 29) {
  80. /* length = 29 ... 284 */
  81. if (len > 284)
  82. len = 284;
  83. v = len - 29;
  84. PUTBITS2(0);
  85. PUTBITS2(0);
  86. PUTBITS2(3);
  87. PUTBITS2((v >> 6));
  88. PUTBITS2((v >> 4) & 3);
  89. PUTBITS2((v >> 2) & 3);
  90. PUTBITS2(v & 3);
  91. PUTBITS2(color);
  92. } else {
  93. PUTBITS2(color);
  94. if (color == 0) {
  95. PUTBITS2(1);
  96. }
  97. len = 1;
  98. }
  99. x += len;
  100. }
  101. /* end of line */
  102. PUTBITS2(0);
  103. PUTBITS2(0);
  104. PUTBITS2(0);
  105. if (bitcnt != 6) {
  106. *q++ = bitbuf;
  107. }
  108. *q++ = 0xf0;
  109. bitmap += linesize;
  110. buf_size -= q - line_begin;
  111. }
  112. len = q - *pq;
  113. *pq = q;
  114. return len;
  115. }
  116. #define PUTBITS4(val)\
  117. {\
  118. bitbuf |= (val) << bitcnt;\
  119. bitcnt -= 4;\
  120. if (bitcnt < 0) {\
  121. bitcnt = 4;\
  122. *q++ = bitbuf;\
  123. bitbuf = 0;\
  124. }\
  125. }
  126. /* some DVB decoders only implement 4 bits/pixel */
  127. static int dvb_encode_rle4(uint8_t **pq, int buf_size,
  128. const uint8_t *bitmap, int linesize,
  129. int w, int h)
  130. {
  131. uint8_t *q, *line_begin;
  132. unsigned int bitbuf;
  133. int bitcnt;
  134. int x, y, len, x1, v, color;
  135. q = *pq;
  136. for(y = 0; y < h; y++) {
  137. // Worst case line is 6 bits per value, + 4 bytes overhead
  138. if (buf_size * 8 < w * 6 + 32)
  139. return AVERROR_BUFFER_TOO_SMALL;
  140. line_begin = q;
  141. *q++ = 0x11;
  142. bitbuf = 0;
  143. bitcnt = 4;
  144. x = 0;
  145. while (x < w) {
  146. x1 = x;
  147. color = bitmap[x1++];
  148. while (x1 < w && bitmap[x1] == color)
  149. x1++;
  150. len = x1 - x;
  151. if (color == 0 && len == 2) {
  152. PUTBITS4(0);
  153. PUTBITS4(0xd);
  154. } else if (color == 0 && (len >= 3 && len <= 9)) {
  155. PUTBITS4(0);
  156. PUTBITS4(len - 2);
  157. } else if (len >= 4 && len <= 7) {
  158. PUTBITS4(0);
  159. PUTBITS4(8 + len - 4);
  160. PUTBITS4(color);
  161. } else if (len >= 9 && len <= 24) {
  162. PUTBITS4(0);
  163. PUTBITS4(0xe);
  164. PUTBITS4(len - 9);
  165. PUTBITS4(color);
  166. } else if (len >= 25) {
  167. if (len > 280)
  168. len = 280;
  169. v = len - 25;
  170. PUTBITS4(0);
  171. PUTBITS4(0xf);
  172. PUTBITS4(v >> 4);
  173. PUTBITS4(v & 0xf);
  174. PUTBITS4(color);
  175. } else {
  176. PUTBITS4(color);
  177. if (color == 0) {
  178. PUTBITS4(0xc);
  179. }
  180. len = 1;
  181. }
  182. x += len;
  183. }
  184. /* end of line */
  185. PUTBITS4(0);
  186. PUTBITS4(0);
  187. if (bitcnt != 4) {
  188. *q++ = bitbuf;
  189. }
  190. *q++ = 0xf0;
  191. bitmap += linesize;
  192. buf_size -= q - line_begin;
  193. }
  194. len = q - *pq;
  195. *pq = q;
  196. return len;
  197. }
  198. static int dvb_encode_rle8(uint8_t **pq, int buf_size,
  199. const uint8_t *bitmap, int linesize,
  200. int w, int h)
  201. {
  202. uint8_t *q, *line_begin;
  203. int x, y, len, x1, color;
  204. q = *pq;
  205. for (y = 0; y < h; y++) {
  206. // Worst case line is 12 bits per value, + 3 bytes overhead
  207. if (buf_size * 8 < w * 12 + 24)
  208. return AVERROR_BUFFER_TOO_SMALL;
  209. line_begin = q;
  210. *q++ = 0x12;
  211. x = 0;
  212. while (x < w) {
  213. x1 = x;
  214. color = bitmap[x1++];
  215. while (x1 < w && bitmap[x1] == color)
  216. x1++;
  217. len = x1 - x;
  218. if (len == 1 && color) {
  219. // 00000001 to 11111111 1 pixel in colour x
  220. *q++ = color;
  221. } else {
  222. if (color == 0x00) {
  223. // 00000000 0LLLLLLL L pixels (1-127) in colour 0 (L > 0)
  224. len = FFMIN(len, 127);
  225. *q++ = 0x00;
  226. *q++ = len;
  227. } else if (len > 2) {
  228. // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
  229. len = FFMIN(len, 127);
  230. *q++ = 0x00;
  231. *q++ = 0x80+len;
  232. *q++ = color;
  233. }
  234. else if (len == 2) {
  235. *q++ = color;
  236. *q++ = color;
  237. } else {
  238. *q++ = color;
  239. len = 1;
  240. }
  241. }
  242. x += len;
  243. }
  244. /* end of line */
  245. // 00000000 end of 8-bit/pixel_code_string
  246. *q++ = 0x00;
  247. *q++ = 0xf0;
  248. bitmap += linesize;
  249. buf_size -= q - line_begin;
  250. }
  251. len = q - *pq;
  252. *pq = q;
  253. return len;
  254. }
  255. static int encode_dvb_subtitles(AVCodecContext *avctx,
  256. uint8_t *outbuf, int buf_size,
  257. const AVSubtitle *h)
  258. {
  259. DVBSubtitleContext *s = avctx->priv_data;
  260. uint8_t *q, *pseg_len;
  261. int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
  262. q = outbuf;
  263. page_id = 1;
  264. if (h->num_rects && !h->rects)
  265. return AVERROR(EINVAL);
  266. if (avctx->width > 0 && avctx->height > 0) {
  267. if (buf_size < 11)
  268. return AVERROR_BUFFER_TOO_SMALL;
  269. /* display definition segment */
  270. *q++ = 0x0f; /* sync_byte */
  271. *q++ = 0x14; /* segment_type */
  272. bytestream_put_be16(&q, page_id);
  273. pseg_len = q;
  274. q += 2; /* segment length */
  275. *q++ = 0x00; /* dds version number & display window flag */
  276. bytestream_put_be16(&q, avctx->width - 1); /* display width */
  277. bytestream_put_be16(&q, avctx->height - 1); /* display height */
  278. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  279. buf_size -= 11;
  280. }
  281. /* page composition segment */
  282. if (buf_size < 8 + h->num_rects * 6)
  283. return AVERROR_BUFFER_TOO_SMALL;
  284. *q++ = 0x0f; /* sync_byte */
  285. *q++ = 0x10; /* segment_type */
  286. bytestream_put_be16(&q, page_id);
  287. pseg_len = q;
  288. q += 2; /* segment length */
  289. *q++ = 30; /* page_timeout (seconds) */
  290. page_state = 2; /* mode change */
  291. /* page_version = 0 + page_state */
  292. *q++ = (s->object_version << 4) | (page_state << 2) | 3;
  293. for (region_id = 0; region_id < h->num_rects; region_id++) {
  294. *q++ = region_id;
  295. *q++ = 0xff; /* reserved */
  296. bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
  297. bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
  298. }
  299. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  300. buf_size -= 8 + h->num_rects * 6;
  301. if (h->num_rects) {
  302. for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
  303. if (buf_size < 6 + h->rects[clut_id]->nb_colors * 6)
  304. return AVERROR_BUFFER_TOO_SMALL;
  305. /* CLUT segment */
  306. if (h->rects[clut_id]->nb_colors <= 4) {
  307. /* 2 bpp, some decoders do not support it correctly */
  308. bpp_index = 0;
  309. } else if (h->rects[clut_id]->nb_colors <= 16) {
  310. /* 4 bpp, standard encoding */
  311. bpp_index = 1;
  312. } else if (h->rects[clut_id]->nb_colors <= 256) {
  313. /* 8 bpp, standard encoding */
  314. bpp_index = 2;
  315. } else {
  316. return AVERROR(EINVAL);
  317. }
  318. /* CLUT segment */
  319. *q++ = 0x0f; /* sync byte */
  320. *q++ = 0x12; /* CLUT definition segment */
  321. bytestream_put_be16(&q, page_id);
  322. pseg_len = q;
  323. q += 2; /* segment length */
  324. *q++ = clut_id;
  325. *q++ = (0 << 4) | 0xf; /* version = 0 */
  326. for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
  327. *q++ = i; /* clut_entry_id */
  328. *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
  329. {
  330. int a, r, g, b;
  331. uint32_t x= ((uint32_t*)h->rects[clut_id]->data[1])[i];
  332. a = (x >> 24) & 0xff;
  333. r = (x >> 16) & 0xff;
  334. g = (x >> 8) & 0xff;
  335. b = (x >> 0) & 0xff;
  336. *q++ = RGB_TO_Y_CCIR(r, g, b);
  337. *q++ = RGB_TO_V_CCIR(r, g, b, 0);
  338. *q++ = RGB_TO_U_CCIR(r, g, b, 0);
  339. *q++ = 255 - a;
  340. }
  341. }
  342. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  343. buf_size -= 6 + h->rects[clut_id]->nb_colors * 6;
  344. }
  345. }
  346. if (buf_size < h->num_rects * 22)
  347. return AVERROR_BUFFER_TOO_SMALL;
  348. for (region_id = 0; region_id < h->num_rects; region_id++) {
  349. /* region composition segment */
  350. if (h->rects[region_id]->nb_colors <= 4) {
  351. /* 2 bpp, some decoders do not support it correctly */
  352. bpp_index = 0;
  353. } else if (h->rects[region_id]->nb_colors <= 16) {
  354. /* 4 bpp, standard encoding */
  355. bpp_index = 1;
  356. } else if (h->rects[region_id]->nb_colors <= 256) {
  357. /* 8 bpp, standard encoding */
  358. bpp_index = 2;
  359. } else {
  360. return AVERROR(EINVAL);
  361. }
  362. *q++ = 0x0f; /* sync_byte */
  363. *q++ = 0x11; /* segment_type */
  364. bytestream_put_be16(&q, page_id);
  365. pseg_len = q;
  366. q += 2; /* segment length */
  367. *q++ = region_id;
  368. *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
  369. bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
  370. bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
  371. *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
  372. *q++ = region_id; /* clut_id == region_id */
  373. *q++ = 0; /* 8 bit fill colors */
  374. *q++ = 0x03; /* 4 bit and 2 bit fill colors */
  375. bytestream_put_be16(&q, region_id); /* object_id == region_id */
  376. *q++ = (0 << 6) | (0 << 4);
  377. *q++ = 0;
  378. *q++ = 0xf0;
  379. *q++ = 0;
  380. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  381. }
  382. buf_size -= h->num_rects * 22;
  383. if (h->num_rects) {
  384. for (object_id = 0; object_id < h->num_rects; object_id++) {
  385. int (*dvb_encode_rle)(uint8_t **pq, int buf_size,
  386. const uint8_t *bitmap, int linesize,
  387. int w, int h);
  388. if (buf_size < 13)
  389. return AVERROR_BUFFER_TOO_SMALL;
  390. /* bpp_index maths */
  391. if (h->rects[object_id]->nb_colors <= 4) {
  392. /* 2 bpp, some decoders do not support it correctly */
  393. dvb_encode_rle = dvb_encode_rle2;
  394. } else if (h->rects[object_id]->nb_colors <= 16) {
  395. /* 4 bpp, standard encoding */
  396. dvb_encode_rle = dvb_encode_rle4;
  397. } else if (h->rects[object_id]->nb_colors <= 256) {
  398. /* 8 bpp, standard encoding */
  399. dvb_encode_rle = dvb_encode_rle8;
  400. } else {
  401. return AVERROR(EINVAL);
  402. }
  403. /* Object Data segment */
  404. *q++ = 0x0f; /* sync byte */
  405. *q++ = 0x13;
  406. bytestream_put_be16(&q, page_id);
  407. pseg_len = q;
  408. q += 2; /* segment length */
  409. bytestream_put_be16(&q, object_id);
  410. *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
  411. onject_coding_method,
  412. non_modifying_color_flag */
  413. {
  414. uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
  415. int ret;
  416. ptop_field_len = q;
  417. q += 2;
  418. pbottom_field_len = q;
  419. q += 2;
  420. buf_size -= 13;
  421. top_ptr = q;
  422. ret = dvb_encode_rle(&q, buf_size,
  423. h->rects[object_id]->data[0],
  424. h->rects[object_id]->w * 2,
  425. h->rects[object_id]->w,
  426. h->rects[object_id]->h >> 1);
  427. if (ret < 0)
  428. return ret;
  429. buf_size -= ret;
  430. bottom_ptr = q;
  431. ret = dvb_encode_rle(&q, buf_size,
  432. h->rects[object_id]->data[0] + h->rects[object_id]->w,
  433. h->rects[object_id]->w * 2,
  434. h->rects[object_id]->w,
  435. h->rects[object_id]->h >> 1);
  436. if (ret < 0)
  437. return ret;
  438. buf_size -= ret;
  439. bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
  440. bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
  441. }
  442. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  443. }
  444. }
  445. /* end of display set segment */
  446. if (buf_size < 6)
  447. return AVERROR_BUFFER_TOO_SMALL;
  448. *q++ = 0x0f; /* sync_byte */
  449. *q++ = 0x80; /* segment_type */
  450. bytestream_put_be16(&q, page_id);
  451. pseg_len = q;
  452. q += 2; /* segment length */
  453. bytestream_put_be16(&pseg_len, q - pseg_len - 2);
  454. buf_size -= 6;
  455. s->object_version = (s->object_version + 1) & 0xf;
  456. return q - outbuf;
  457. }
  458. static int dvbsub_encode(AVCodecContext *avctx,
  459. unsigned char *buf, int buf_size,
  460. const AVSubtitle *sub)
  461. {
  462. int ret;
  463. ret = encode_dvb_subtitles(avctx, buf, buf_size, sub);
  464. return ret;
  465. }
  466. AVCodec ff_dvbsub_encoder = {
  467. .name = "dvbsub",
  468. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  469. .type = AVMEDIA_TYPE_SUBTITLE,
  470. .id = AV_CODEC_ID_DVB_SUBTITLE,
  471. .priv_data_size = sizeof(DVBSubtitleContext),
  472. .encode_sub = dvbsub_encode,
  473. };