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.

516 lines
16KB

  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_freep(&sub_header->rects[i]->pict.data[0]);
  300. av_freep(&sub_header->rects[i]->pict.data[1]);
  301. av_freep(&sub_header->rects[i]);
  302. }
  303. av_freep(&sub_header->rects);
  304. sub_header->num_rects = 0;
  305. }
  306. bitmap = av_malloc(w * h);
  307. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  308. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  309. sub_header->num_rects = 1;
  310. sub_header->rects[0]->pict.data[0] = bitmap;
  311. decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  312. buf, offset1, buf_size, is_8bit);
  313. decode_rle(bitmap + w, w * 2, w, h / 2,
  314. buf, offset2, buf_size, is_8bit);
  315. if (is_8bit) {
  316. if (yuv_palette == 0)
  317. goto fail;
  318. sub_header->rects[0]->pict.data[1] = av_malloc(256 * 4);
  319. sub_header->rects[0]->nb_colors = 256;
  320. yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
  321. } else {
  322. sub_header->rects[0]->pict.data[1] = av_malloc(4 * 4);
  323. sub_header->rects[0]->nb_colors = 4;
  324. guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1],
  325. colormap, alpha, 0xffff00);
  326. }
  327. sub_header->rects[0]->x = x1;
  328. sub_header->rects[0]->y = y1;
  329. sub_header->rects[0]->w = w;
  330. sub_header->rects[0]->h = h;
  331. sub_header->rects[0]->pict.linesize[0] = w;
  332. }
  333. }
  334. if (next_cmd_pos == cmd_pos)
  335. break;
  336. cmd_pos = next_cmd_pos;
  337. }
  338. if (sub_header->num_rects > 0)
  339. return is_menu;
  340. fail:
  341. if (sub_header->rects != NULL) {
  342. for (i = 0; i < sub_header->num_rects; i++) {
  343. av_freep(&sub_header->rects[i]->pict.data[0]);
  344. av_freep(&sub_header->rects[i]->pict.data[1]);
  345. av_freep(&sub_header->rects[i]);
  346. }
  347. av_freep(&sub_header->rects);
  348. sub_header->num_rects = 0;
  349. }
  350. return -1;
  351. }
  352. static int is_transp(const uint8_t *buf, int pitch, int n,
  353. const uint8_t *transp_color)
  354. {
  355. int i;
  356. for(i = 0; i < n; i++) {
  357. if (!transp_color[*buf])
  358. return 0;
  359. buf += pitch;
  360. }
  361. return 1;
  362. }
  363. /* return 0 if empty rectangle, 1 if non empty */
  364. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  365. {
  366. uint8_t transp_color[256];
  367. int y1, y2, x1, x2, y, w, h, i;
  368. uint8_t *bitmap;
  369. if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  370. return 0;
  371. memset(transp_color, 0, 256);
  372. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  373. if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
  374. transp_color[i] = 1;
  375. }
  376. y1 = 0;
  377. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
  378. 1, s->rects[0]->w, transp_color))
  379. y1++;
  380. if (y1 == s->rects[0]->h) {
  381. av_freep(&s->rects[0]->pict.data[0]);
  382. s->rects[0]->w = s->rects[0]->h = 0;
  383. return 0;
  384. }
  385. y2 = s->rects[0]->h - 1;
  386. while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
  387. s->rects[0]->w, transp_color))
  388. y2--;
  389. x1 = 0;
  390. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
  391. s->rects[0]->h, transp_color))
  392. x1++;
  393. x2 = s->rects[0]->w - 1;
  394. while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
  395. transp_color))
  396. x2--;
  397. w = x2 - x1 + 1;
  398. h = y2 - y1 + 1;
  399. bitmap = av_malloc(w * h);
  400. if (!bitmap)
  401. return 1;
  402. for(y = 0; y < h; y++) {
  403. memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
  404. }
  405. av_freep(&s->rects[0]->pict.data[0]);
  406. s->rects[0]->pict.data[0] = bitmap;
  407. s->rects[0]->pict.linesize[0] = w;
  408. s->rects[0]->w = w;
  409. s->rects[0]->h = h;
  410. s->rects[0]->x += x1;
  411. s->rects[0]->y += y1;
  412. return 1;
  413. }
  414. #ifdef DEBUG
  415. #undef fprintf
  416. #undef perror
  417. #undef exit
  418. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  419. uint32_t *rgba_palette)
  420. {
  421. int x, y, v;
  422. FILE *f;
  423. f = fopen(filename, "w");
  424. if (!f) {
  425. perror(filename);
  426. exit(1);
  427. }
  428. fprintf(f, "P6\n"
  429. "%d %d\n"
  430. "%d\n",
  431. w, h, 255);
  432. for(y = 0; y < h; y++) {
  433. for(x = 0; x < w; x++) {
  434. v = rgba_palette[bitmap[y * w + x]];
  435. putc((v >> 16) & 0xff, f);
  436. putc((v >> 8) & 0xff, f);
  437. putc((v >> 0) & 0xff, f);
  438. }
  439. }
  440. fclose(f);
  441. }
  442. #endif
  443. static int dvdsub_decode(AVCodecContext *avctx,
  444. void *data, int *data_size,
  445. const uint8_t *buf, int buf_size)
  446. {
  447. AVSubtitle *sub = (void *)data;
  448. int is_menu;
  449. is_menu = decode_dvd_subtitles(sub, buf, buf_size);
  450. if (is_menu < 0) {
  451. no_subtitle:
  452. *data_size = 0;
  453. return buf_size;
  454. }
  455. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  456. goto no_subtitle;
  457. #if defined(DEBUG)
  458. av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
  459. sub->start_display_time,
  460. sub->end_display_time);
  461. ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
  462. sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
  463. #endif
  464. *data_size = 1;
  465. return buf_size;
  466. }
  467. AVCodec dvdsub_decoder = {
  468. "dvdsub",
  469. CODEC_TYPE_SUBTITLE,
  470. CODEC_ID_DVD_SUBTITLE,
  471. 0,
  472. NULL,
  473. NULL,
  474. NULL,
  475. dvdsub_decode,
  476. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  477. };