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.

520 lines
15KB

  1. /*
  2. * DVD subtitle decoding 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. #include "bitstream.h"
  23. #include "colorspace.h"
  24. #include "dsputil.h"
  25. //#define DEBUG
  26. static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
  27. {
  28. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  29. uint8_t r, g, b;
  30. int i, y, cb, cr;
  31. int r_add, g_add, b_add;
  32. for (i = num_values; i > 0; i--) {
  33. y = *ycbcr++;
  34. cb = *ycbcr++;
  35. cr = *ycbcr++;
  36. YUV_TO_RGB1_CCIR(cb, cr);
  37. YUV_TO_RGB2_CCIR(r, g, b, y);
  38. *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
  39. }
  40. }
  41. static int dvdsub_init_decoder(AVCodecContext *avctx)
  42. {
  43. return 0;
  44. }
  45. static int decode_run_2bit(GetBitContext *gb, int *color)
  46. {
  47. unsigned int v, t;
  48. v = 0;
  49. for (t = 1; v < t && t <= 0x40; t <<= 2)
  50. v = (v << 4) | get_bits(gb, 4);
  51. *color = v & 3;
  52. if (v < 4) { /* Code for fill rest of line */
  53. return INT_MAX;
  54. }
  55. return v >> 2;
  56. }
  57. static int decode_run_8bit(GetBitContext *gb, int *color)
  58. {
  59. int len;
  60. int has_run = get_bits1(gb);
  61. if (get_bits1(gb))
  62. *color = get_bits(gb, 8);
  63. else
  64. *color = get_bits(gb, 2);
  65. if (has_run) {
  66. if (get_bits1(gb)) {
  67. len = get_bits(gb, 7);
  68. if (len == 0)
  69. len = INT_MAX;
  70. else
  71. len += 9;
  72. } else
  73. len = get_bits(gb, 3) + 2;
  74. } else
  75. len = 1;
  76. return len;
  77. }
  78. static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
  79. const uint8_t *buf, int start, int buf_size, int is_8bit)
  80. {
  81. GetBitContext gb;
  82. int bit_len;
  83. int x, y, len, color;
  84. uint8_t *d;
  85. bit_len = (buf_size - start) * 8;
  86. init_get_bits(&gb, buf + start, bit_len);
  87. x = 0;
  88. y = 0;
  89. d = bitmap;
  90. for(;;) {
  91. if (get_bits_count(&gb) > bit_len)
  92. return -1;
  93. if (is_8bit)
  94. len = decode_run_8bit(&gb, &color);
  95. else
  96. len = decode_run_2bit(&gb, &color);
  97. len = FFMIN(len, w - x);
  98. memset(d + x, color, len);
  99. x += len;
  100. if (x >= w) {
  101. y++;
  102. if (y >= h)
  103. break;
  104. d += linesize;
  105. x = 0;
  106. /* byte align */
  107. align_get_bits(&gb);
  108. }
  109. }
  110. return 0;
  111. }
  112. static void guess_palette(uint32_t *rgba_palette,
  113. uint8_t *colormap,
  114. uint8_t *alpha,
  115. uint32_t subtitle_color)
  116. {
  117. uint8_t color_used[16];
  118. int nb_opaque_colors, i, level, j, r, g, b;
  119. for(i = 0; i < 4; i++)
  120. rgba_palette[i] = 0;
  121. memset(color_used, 0, 16);
  122. nb_opaque_colors = 0;
  123. for(i = 0; i < 4; i++) {
  124. if (alpha[i] != 0 && !color_used[colormap[i]]) {
  125. color_used[colormap[i]] = 1;
  126. nb_opaque_colors++;
  127. }
  128. }
  129. if (nb_opaque_colors == 0)
  130. return;
  131. j = nb_opaque_colors;
  132. memset(color_used, 0, 16);
  133. for(i = 0; i < 4; i++) {
  134. if (alpha[i] != 0) {
  135. if (!color_used[colormap[i]]) {
  136. level = (0xff * j) / nb_opaque_colors;
  137. r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
  138. g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
  139. b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
  140. rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
  141. color_used[colormap[i]] = (i + 1);
  142. j--;
  143. } else {
  144. rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
  145. ((alpha[i] * 17) << 24);
  146. }
  147. }
  148. }
  149. }
  150. #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
  151. static int decode_dvd_subtitles(AVSubtitle *sub_header,
  152. const uint8_t *buf, int buf_size)
  153. {
  154. int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
  155. int big_offsets, offset_size, is_8bit = 0;
  156. const uint8_t *yuv_palette = 0;
  157. uint8_t colormap[4], alpha[256];
  158. int date;
  159. int i;
  160. int is_menu = 0;
  161. if (buf_size < 10)
  162. return -1;
  163. sub_header->rects = NULL;
  164. sub_header->num_rects = 0;
  165. sub_header->start_display_time = 0;
  166. sub_header->end_display_time = 0;
  167. if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
  168. big_offsets = 1;
  169. offset_size = 4;
  170. cmd_pos = 6;
  171. } else {
  172. big_offsets = 0;
  173. offset_size = 2;
  174. cmd_pos = 2;
  175. }
  176. cmd_pos = READ_OFFSET(buf + cmd_pos);
  177. while ((cmd_pos + 2 + offset_size) < buf_size) {
  178. date = AV_RB16(buf + cmd_pos);
  179. next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
  180. #ifdef DEBUG
  181. av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
  182. cmd_pos, next_cmd_pos, date);
  183. #endif
  184. pos = cmd_pos + 2 + offset_size;
  185. offset1 = -1;
  186. offset2 = -1;
  187. x1 = y1 = x2 = y2 = 0;
  188. while (pos < buf_size) {
  189. cmd = buf[pos++];
  190. #ifdef DEBUG
  191. av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
  192. #endif
  193. switch(cmd) {
  194. case 0x00:
  195. /* menu subpicture */
  196. is_menu = 1;
  197. break;
  198. case 0x01:
  199. /* set start date */
  200. sub_header->start_display_time = (date << 10) / 90;
  201. break;
  202. case 0x02:
  203. /* set end date */
  204. sub_header->end_display_time = (date << 10) / 90;
  205. break;
  206. case 0x03:
  207. /* set colormap */
  208. if ((buf_size - pos) < 2)
  209. goto fail;
  210. colormap[3] = buf[pos] >> 4;
  211. colormap[2] = buf[pos] & 0x0f;
  212. colormap[1] = buf[pos + 1] >> 4;
  213. colormap[0] = buf[pos + 1] & 0x0f;
  214. pos += 2;
  215. break;
  216. case 0x04:
  217. /* set alpha */
  218. if ((buf_size - pos) < 2)
  219. goto fail;
  220. alpha[3] = buf[pos] >> 4;
  221. alpha[2] = buf[pos] & 0x0f;
  222. alpha[1] = buf[pos + 1] >> 4;
  223. alpha[0] = buf[pos + 1] & 0x0f;
  224. pos += 2;
  225. #ifdef DEBUG
  226. av_log(NULL, AV_LOG_INFO, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
  227. #endif
  228. break;
  229. case 0x05:
  230. case 0x85:
  231. if ((buf_size - pos) < 6)
  232. goto fail;
  233. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  234. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  235. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  236. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  237. if (cmd & 0x80)
  238. is_8bit = 1;
  239. #ifdef DEBUG
  240. av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
  241. x1, x2, y1, y2);
  242. #endif
  243. pos += 6;
  244. break;
  245. case 0x06:
  246. if ((buf_size - pos) < 4)
  247. goto fail;
  248. offset1 = AV_RB16(buf + pos);
  249. offset2 = AV_RB16(buf + pos + 2);
  250. #ifdef DEBUG
  251. av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  252. #endif
  253. pos += 4;
  254. break;
  255. case 0x86:
  256. if ((buf_size - pos) < 8)
  257. goto fail;
  258. offset1 = AV_RB32(buf + pos);
  259. offset2 = AV_RB32(buf + pos + 4);
  260. #ifdef DEBUG
  261. av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  262. #endif
  263. pos += 8;
  264. break;
  265. case 0x83:
  266. /* HD set palette */
  267. if ((buf_size - pos) < 768)
  268. goto fail;
  269. yuv_palette = buf + pos;
  270. pos += 768;
  271. break;
  272. case 0x84:
  273. /* HD set contrast (alpha) */
  274. if ((buf_size - pos) < 256)
  275. goto fail;
  276. for (i = 0; i < 256; i++)
  277. alpha[i] = 0xFF - buf[pos+i];
  278. pos += 256;
  279. break;
  280. case 0xff:
  281. goto the_end;
  282. default:
  283. #ifdef DEBUG
  284. av_log(NULL, AV_LOG_INFO, "unrecognised subpicture command 0x%x\n", cmd);
  285. #endif
  286. goto the_end;
  287. }
  288. }
  289. the_end:
  290. if (offset1 >= 0) {
  291. int w, h;
  292. uint8_t *bitmap;
  293. /* decode the bitmap */
  294. w = x2 - x1 + 1;
  295. if (w < 0)
  296. w = 0;
  297. h = y2 - y1;
  298. if (h < 0)
  299. h = 0;
  300. if (w > 0 && h > 0) {
  301. if (sub_header->rects != NULL) {
  302. for (i = 0; i < sub_header->num_rects; i++) {
  303. av_free(sub_header->rects[i].bitmap);
  304. av_free(sub_header->rects[i].rgba_palette);
  305. }
  306. av_freep(&sub_header->rects);
  307. sub_header->num_rects = 0;
  308. }
  309. bitmap = av_malloc(w * h);
  310. sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
  311. sub_header->num_rects = 1;
  312. sub_header->rects[0].bitmap = bitmap;
  313. decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  314. buf, offset1, buf_size, is_8bit);
  315. decode_rle(bitmap + w, w * 2, w, h / 2,
  316. buf, offset2, buf_size, is_8bit);
  317. if (is_8bit) {
  318. if (yuv_palette == 0)
  319. goto fail;
  320. sub_header->rects[0].rgba_palette = av_malloc(256 * 4);
  321. sub_header->rects[0].nb_colors = 256;
  322. yuv_a_to_rgba(yuv_palette, alpha, sub_header->rects[0].rgba_palette, 256);
  323. } else {
  324. sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
  325. sub_header->rects[0].nb_colors = 4;
  326. guess_palette(sub_header->rects[0].rgba_palette,
  327. colormap, alpha, 0xffff00);
  328. }
  329. sub_header->rects[0].x = x1;
  330. sub_header->rects[0].y = y1;
  331. sub_header->rects[0].w = w;
  332. sub_header->rects[0].h = h;
  333. sub_header->rects[0].linesize = w;
  334. }
  335. }
  336. if (next_cmd_pos == cmd_pos)
  337. break;
  338. cmd_pos = next_cmd_pos;
  339. }
  340. if (sub_header->num_rects > 0)
  341. return is_menu;
  342. fail:
  343. if (sub_header->rects != NULL) {
  344. for (i = 0; i < sub_header->num_rects; i++) {
  345. av_free(sub_header->rects[i].bitmap);
  346. av_free(sub_header->rects[i].rgba_palette);
  347. }
  348. av_freep(&sub_header->rects);
  349. sub_header->num_rects = 0;
  350. }
  351. return -1;
  352. }
  353. static int is_transp(const uint8_t *buf, int pitch, int n,
  354. const uint8_t *transp_color)
  355. {
  356. int i;
  357. for(i = 0; i < n; i++) {
  358. if (!transp_color[*buf])
  359. return 0;
  360. buf += pitch;
  361. }
  362. return 1;
  363. }
  364. /* return 0 if empty rectangle, 1 if non empty */
  365. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  366. {
  367. uint8_t transp_color[256];
  368. int y1, y2, x1, x2, y, w, h, i;
  369. uint8_t *bitmap;
  370. if (s->num_rects == 0 || s->rects == NULL || s->rects[0].w <= 0 || s->rects[0].h <= 0)
  371. return 0;
  372. memset(transp_color, 0, 256);
  373. for(i = 0; i < s->rects[0].nb_colors; i++) {
  374. if ((s->rects[0].rgba_palette[i] >> 24) == 0)
  375. transp_color[i] = 1;
  376. }
  377. y1 = 0;
  378. while (y1 < s->rects[0].h && is_transp(s->rects[0].bitmap + y1 * s->rects[0].linesize,
  379. 1, s->rects[0].w, transp_color))
  380. y1++;
  381. if (y1 == s->rects[0].h) {
  382. av_freep(&s->rects[0].bitmap);
  383. s->rects[0].w = s->rects[0].h = 0;
  384. return 0;
  385. }
  386. y2 = s->rects[0].h - 1;
  387. while (y2 > 0 && is_transp(s->rects[0].bitmap + y2 * s->rects[0].linesize, 1,
  388. s->rects[0].w, transp_color))
  389. y2--;
  390. x1 = 0;
  391. while (x1 < (s->rects[0].w - 1) && is_transp(s->rects[0].bitmap + x1, s->rects[0].linesize,
  392. s->rects[0].h, transp_color))
  393. x1++;
  394. x2 = s->rects[0].w - 1;
  395. while (x2 > 0 && is_transp(s->rects[0].bitmap + x2, s->rects[0].linesize, s->rects[0].h,
  396. transp_color))
  397. x2--;
  398. w = x2 - x1 + 1;
  399. h = y2 - y1 + 1;
  400. bitmap = av_malloc(w * h);
  401. if (!bitmap)
  402. return 1;
  403. for(y = 0; y < h; y++) {
  404. memcpy(bitmap + w * y, s->rects[0].bitmap + x1 + (y1 + y) * s->rects[0].linesize, w);
  405. }
  406. av_freep(&s->rects[0].bitmap);
  407. s->rects[0].bitmap = bitmap;
  408. s->rects[0].linesize = w;
  409. s->rects[0].w = w;
  410. s->rects[0].h = h;
  411. s->rects[0].x += x1;
  412. s->rects[0].y += y1;
  413. return 1;
  414. }
  415. static int dvdsub_close_decoder(AVCodecContext *avctx)
  416. {
  417. return 0;
  418. }
  419. #ifdef DEBUG
  420. #undef fprintf
  421. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  422. uint32_t *rgba_palette)
  423. {
  424. int x, y, v;
  425. FILE *f;
  426. f = fopen(filename, "w");
  427. if (!f) {
  428. perror(filename);
  429. exit(1);
  430. }
  431. fprintf(f, "P6\n"
  432. "%d %d\n"
  433. "%d\n",
  434. w, h, 255);
  435. for(y = 0; y < h; y++) {
  436. for(x = 0; x < w; x++) {
  437. v = rgba_palette[bitmap[y * w + x]];
  438. putc((v >> 16) & 0xff, f);
  439. putc((v >> 8) & 0xff, f);
  440. putc((v >> 0) & 0xff, f);
  441. }
  442. }
  443. fclose(f);
  444. }
  445. #endif
  446. static int dvdsub_decode(AVCodecContext *avctx,
  447. void *data, int *data_size,
  448. uint8_t *buf, int buf_size)
  449. {
  450. AVSubtitle *sub = (void *)data;
  451. int is_menu;
  452. is_menu = decode_dvd_subtitles(sub, buf, buf_size);
  453. if (is_menu < 0) {
  454. no_subtitle:
  455. *data_size = 0;
  456. return buf_size;
  457. }
  458. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  459. goto no_subtitle;
  460. #if defined(DEBUG)
  461. av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
  462. sub->start_display_time,
  463. sub->end_display_time);
  464. ppm_save("/tmp/a.ppm", sub->rects[0].bitmap,
  465. sub->rects[0].w, sub->rects[0].h, sub->rects[0].rgba_palette);
  466. #endif
  467. *data_size = 1;
  468. return buf_size;
  469. }
  470. AVCodec dvdsub_decoder = {
  471. "dvdsub",
  472. CODEC_TYPE_SUBTITLE,
  473. CODEC_ID_DVD_SUBTITLE,
  474. 0,
  475. dvdsub_init_decoder,
  476. NULL,
  477. dvdsub_close_decoder,
  478. dvdsub_decode,
  479. };