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.

470 lines
14KB

  1. /*
  2. * DVD subtitle decoding 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. //#define DEBUG
  21. typedef struct DVDSubContext {
  22. } DVDSubContext;
  23. static int dvdsub_init_decoder(AVCodecContext *avctx)
  24. {
  25. return 0;
  26. }
  27. uint16_t getbe16(const uint8_t *p)
  28. {
  29. return (p[0] << 8) | p[1];
  30. }
  31. int get_nibble(const uint8_t *buf, int nibble_offset)
  32. {
  33. return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
  34. }
  35. static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
  36. const uint8_t *buf, int nibble_offset, int buf_size)
  37. {
  38. unsigned int v;
  39. int x, y, len, color, nibble_end;
  40. uint8_t *d;
  41. nibble_end = buf_size * 2;
  42. x = 0;
  43. y = 0;
  44. d = bitmap;
  45. for(;;) {
  46. if (nibble_offset >= nibble_end)
  47. return -1;
  48. v = get_nibble(buf, nibble_offset++);
  49. if (v < 0x4) {
  50. v = (v << 4) | get_nibble(buf, nibble_offset++);
  51. if (v < 0x10) {
  52. v = (v << 4) | get_nibble(buf, nibble_offset++);
  53. if (v < 0x040) {
  54. v = (v << 4) | get_nibble(buf, nibble_offset++);
  55. if (v < 4) {
  56. v |= (w - x) << 2;
  57. }
  58. }
  59. }
  60. }
  61. len = v >> 2;
  62. if (len > (w - x))
  63. len = (w - x);
  64. color = v & 0x03;
  65. memset(d + x, color, len);
  66. x += len;
  67. if (x >= w) {
  68. y++;
  69. if (y >= h)
  70. break;
  71. d += linesize;
  72. x = 0;
  73. /* byte align */
  74. nibble_offset += (nibble_offset & 1);
  75. }
  76. }
  77. return 0;
  78. }
  79. static void guess_palette(uint32_t *rgba_palette,
  80. uint8_t *palette,
  81. uint8_t *alpha,
  82. uint32_t subtitle_color)
  83. {
  84. uint8_t color_used[16];
  85. int nb_opaque_colors, i, level, j, r, g, b;
  86. for(i = 0; i < 4; i++)
  87. rgba_palette[i] = 0;
  88. memset(color_used, 0, 16);
  89. nb_opaque_colors = 0;
  90. for(i = 0; i < 4; i++) {
  91. if (alpha[i] != 0 && !color_used[palette[i]]) {
  92. color_used[palette[i]] = 1;
  93. nb_opaque_colors++;
  94. }
  95. }
  96. if (nb_opaque_colors == 0)
  97. return;
  98. j = 0;
  99. memset(color_used, 0, 16);
  100. for(i = 0; i < 4; i++) {
  101. if (alpha[i] != 0) {
  102. if (!color_used[palette[i]]) {
  103. level = (0xff * (j + 1)) / nb_opaque_colors;
  104. r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
  105. g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
  106. b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
  107. rgba_palette[i] = b | (g << 8) | (r << 16) | (0xff << 24);
  108. color_used[palette[i]] = (i + 1);
  109. j++;
  110. } else {
  111. rgba_palette[i] = rgba_palette[color_used[palette[i]] - 1];
  112. }
  113. }
  114. }
  115. }
  116. static int decode_dvd_subtitles(AVSubtitle *sub_header,
  117. const uint8_t *buf, int buf_size)
  118. {
  119. int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
  120. uint8_t palette[4], alpha[4];
  121. int date;
  122. int i;
  123. if (buf_size < 4)
  124. return -1;
  125. sub_header->rects = NULL;
  126. sub_header->num_rects = 0;
  127. sub_header->start_display_time = 0;
  128. sub_header->end_display_time = 0;
  129. cmd_pos = getbe16(buf + 2);
  130. while ((cmd_pos + 4) < buf_size) {
  131. date = getbe16(buf + cmd_pos);
  132. next_cmd_pos = getbe16(buf + cmd_pos + 2);
  133. #ifdef DEBUG
  134. av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
  135. cmd_pos, next_cmd_pos, date);
  136. #endif
  137. pos = cmd_pos + 4;
  138. offset1 = -1;
  139. offset2 = -1;
  140. x1 = y1 = x2 = y2 = 0;
  141. while (pos < buf_size) {
  142. cmd = buf[pos++];
  143. #ifdef DEBUG
  144. av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
  145. #endif
  146. switch(cmd) {
  147. case 0x00:
  148. /* force display */
  149. break;
  150. case 0x01:
  151. /* set start date */
  152. sub_header->start_display_time = date * 10;
  153. break;
  154. case 0x02:
  155. /* set end date */
  156. sub_header->end_display_time = date * 10;
  157. break;
  158. case 0x03:
  159. /* set palette */
  160. if ((buf_size - pos) < 2)
  161. goto fail;
  162. palette[0] = buf[pos] >> 4;
  163. palette[1] = buf[pos] & 0x0f;
  164. palette[2] = buf[pos + 1] >> 4;
  165. palette[3] = buf[pos + 1] & 0x0f;
  166. pos += 2;
  167. break;
  168. case 0x04:
  169. /* set alpha */
  170. if ((buf_size - pos) < 2)
  171. goto fail;
  172. alpha[0] = buf[pos] >> 4;
  173. alpha[1] = buf[pos] & 0x0f;
  174. alpha[2] = buf[pos + 1] >> 4;
  175. alpha[3] = buf[pos + 1] & 0x0f;
  176. pos += 2;
  177. break;
  178. case 0x05:
  179. if ((buf_size - pos) < 6)
  180. goto fail;
  181. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  182. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  183. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  184. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  185. #ifdef DEBUG
  186. av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
  187. x1, x2, y1, y2);
  188. #endif
  189. pos += 6;
  190. break;
  191. case 0x06:
  192. if ((buf_size - pos) < 4)
  193. goto fail;
  194. offset1 = getbe16(buf + pos);
  195. offset2 = getbe16(buf + pos + 2);
  196. #ifdef DEBUG
  197. av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  198. #endif
  199. pos += 4;
  200. break;
  201. case 0xff:
  202. default:
  203. goto the_end;
  204. }
  205. }
  206. the_end:
  207. if (offset1 >= 0) {
  208. int w, h;
  209. uint8_t *bitmap;
  210. /* decode the bitmap */
  211. w = x2 - x1 + 1;
  212. if (w < 0)
  213. w = 0;
  214. h = y2 - y1;
  215. if (h < 0)
  216. h = 0;
  217. if (w > 0 && h > 0) {
  218. if (sub_header->rects != NULL) {
  219. for (i = 0; i < sub_header->num_rects; i++) {
  220. av_free(sub_header->rects[i].bitmap);
  221. av_free(sub_header->rects[i].rgba_palette);
  222. }
  223. av_freep(&sub_header->rects);
  224. sub_header->num_rects = 0;
  225. }
  226. bitmap = av_malloc(w * h);
  227. sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
  228. sub_header->num_rects = 1;
  229. sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
  230. decode_rle(bitmap, w * 2, w, h / 2,
  231. buf, offset1 * 2, buf_size);
  232. decode_rle(bitmap + w, w * 2, w, h / 2,
  233. buf, offset2 * 2, buf_size);
  234. guess_palette(sub_header->rects[0].rgba_palette,
  235. palette, alpha, 0xffff00);
  236. sub_header->rects[0].x = x1;
  237. sub_header->rects[0].y = y1;
  238. sub_header->rects[0].w = w;
  239. sub_header->rects[0].h = h;
  240. sub_header->rects[0].nb_colors = 4;
  241. sub_header->rects[0].linesize = w;
  242. sub_header->rects[0].bitmap = bitmap;
  243. }
  244. }
  245. if (next_cmd_pos == cmd_pos)
  246. break;
  247. cmd_pos = next_cmd_pos;
  248. }
  249. if (sub_header->num_rects > 0)
  250. return 0;
  251. fail:
  252. return -1;
  253. }
  254. static int is_transp(const uint8_t *buf, int pitch, int n,
  255. const uint8_t *transp_color)
  256. {
  257. int i;
  258. for(i = 0; i < n; i++) {
  259. if (!transp_color[*buf])
  260. return 0;
  261. buf += pitch;
  262. }
  263. return 1;
  264. }
  265. /* return 0 if empty rectangle, 1 if non empty */
  266. static int find_smallest_bouding_rectangle(AVSubtitle *s)
  267. {
  268. uint8_t transp_color[256];
  269. int y1, y2, x1, x2, y, w, h, i;
  270. uint8_t *bitmap;
  271. if (s->num_rects == 0 || s->rects == NULL || s->rects[0].w <= 0 || s->rects[0].h <= 0)
  272. return 0;
  273. memset(transp_color, 0, 256);
  274. for(i = 0; i < s->rects[0].nb_colors; i++) {
  275. if ((s->rects[0].rgba_palette[i] >> 24) == 0)
  276. transp_color[i] = 1;
  277. }
  278. y1 = 0;
  279. while (y1 < s->rects[0].h && is_transp(s->rects[0].bitmap + y1 * s->rects[0].linesize,
  280. 1, s->rects[0].w, transp_color))
  281. y1++;
  282. if (y1 == s->rects[0].h) {
  283. av_freep(&s->rects[0].bitmap);
  284. s->rects[0].w = s->rects[0].h = 0;
  285. return 0;
  286. }
  287. y2 = s->rects[0].h - 1;
  288. while (y2 > 0 && is_transp(s->rects[0].bitmap + y2 * s->rects[0].linesize, 1,
  289. s->rects[0].w, transp_color))
  290. y2--;
  291. x1 = 0;
  292. while (x1 < (s->rects[0].w - 1) && is_transp(s->rects[0].bitmap + x1, s->rects[0].linesize,
  293. s->rects[0].h, transp_color))
  294. x1++;
  295. x2 = s->rects[0].w - 1;
  296. while (x2 > 0 && is_transp(s->rects[0].bitmap + x2, s->rects[0].linesize, s->rects[0].h,
  297. transp_color))
  298. x2--;
  299. w = x2 - x1 + 1;
  300. h = y2 - y1 + 1;
  301. bitmap = av_malloc(w * h);
  302. if (!bitmap)
  303. return 1;
  304. for(y = 0; y < h; y++) {
  305. memcpy(bitmap + w * y, s->rects[0].bitmap + x1 + (y1 + y) * s->rects[0].linesize, w);
  306. }
  307. av_freep(&s->rects[0].bitmap);
  308. s->rects[0].bitmap = bitmap;
  309. s->rects[0].linesize = w;
  310. s->rects[0].w = w;
  311. s->rects[0].h = h;
  312. s->rects[0].x += x1;
  313. s->rects[0].y += y1;
  314. return 1;
  315. }
  316. static int dvdsub_close_decoder(AVCodecContext *avctx)
  317. {
  318. return 0;
  319. }
  320. #ifdef DEBUG
  321. #undef fprintf
  322. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  323. uint32_t *rgba_palette)
  324. {
  325. int x, y, v;
  326. FILE *f;
  327. f = fopen(filename, "w");
  328. if (!f) {
  329. perror(filename);
  330. exit(1);
  331. }
  332. fprintf(f, "P6\n"
  333. "%d %d\n"
  334. "%d\n",
  335. w, h, 255);
  336. for(y = 0; y < h; y++) {
  337. for(x = 0; x < w; x++) {
  338. v = rgba_palette[bitmap[y * w + x]];
  339. putc((v >> 16) & 0xff, f);
  340. putc((v >> 8) & 0xff, f);
  341. putc((v >> 0) & 0xff, f);
  342. }
  343. }
  344. fclose(f);
  345. }
  346. #endif
  347. static int dvdsub_decode(AVCodecContext *avctx,
  348. void *data, int *data_size,
  349. uint8_t *buf, int buf_size)
  350. {
  351. AVSubtitle *sub = (void *)data;
  352. if (decode_dvd_subtitles(sub, buf, buf_size) < 0) {
  353. no_subtitle:
  354. *data_size = 0;
  355. return buf_size;
  356. }
  357. if (find_smallest_bouding_rectangle(sub) == 0)
  358. goto no_subtitle;
  359. #if defined(DEBUG)
  360. av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
  361. sub->start_display_time,
  362. sub->end_display_time);
  363. ppm_save("/tmp/a.ppm", sub->rects[0].bitmap,
  364. sub->rects[0].w, sub->rects[0].h, sub->rects[0].rgba_palette);
  365. #endif
  366. *data_size = 1;
  367. return buf_size;
  368. }
  369. AVCodec dvdsub_decoder = {
  370. "dvdsub",
  371. CODEC_TYPE_SUBTITLE,
  372. CODEC_ID_DVD_SUBTITLE,
  373. sizeof(DVDSubContext),
  374. dvdsub_init_decoder,
  375. NULL,
  376. dvdsub_close_decoder,
  377. dvdsub_decode,
  378. };
  379. /* parser definition */
  380. typedef struct DVDSubParseContext {
  381. uint8_t *packet;
  382. int packet_len;
  383. int packet_index;
  384. } DVDSubParseContext;
  385. static int dvdsub_parse_init(AVCodecParserContext *s)
  386. {
  387. return 0;
  388. }
  389. static int dvdsub_parse(AVCodecParserContext *s,
  390. AVCodecContext *avctx,
  391. uint8_t **poutbuf, int *poutbuf_size,
  392. const uint8_t *buf, int buf_size)
  393. {
  394. DVDSubParseContext *pc = s->priv_data;
  395. if (pc->packet_index == 0) {
  396. if (buf_size < 2)
  397. return 0;
  398. pc->packet_len = (buf[0] << 8) | buf[1];
  399. av_freep(&pc->packet);
  400. pc->packet = av_malloc(pc->packet_len);
  401. }
  402. if (pc->packet) {
  403. if (pc->packet_index + buf_size <= pc->packet_len) {
  404. memcpy(pc->packet + pc->packet_index, buf, buf_size);
  405. pc->packet_index += buf_size;
  406. if (pc->packet_index >= pc->packet_len) {
  407. *poutbuf = pc->packet;
  408. *poutbuf_size = pc->packet_len;
  409. pc->packet_index = 0;
  410. return buf_size;
  411. }
  412. } else {
  413. /* erroneous size */
  414. pc->packet_index = 0;
  415. }
  416. }
  417. *poutbuf = NULL;
  418. *poutbuf_size = 0;
  419. return buf_size;
  420. }
  421. static void dvdsub_parse_close(AVCodecParserContext *s)
  422. {
  423. DVDSubParseContext *pc = s->priv_data;
  424. av_freep(&pc->packet);
  425. }
  426. AVCodecParser dvdsub_parser = {
  427. { CODEC_ID_DVD_SUBTITLE },
  428. sizeof(DVDSubParseContext),
  429. dvdsub_parse_init,
  430. dvdsub_parse,
  431. dvdsub_parse_close,
  432. };