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.

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