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.

460 lines
13KB

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