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.

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