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.

505 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 "get_bits.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->format = 0;
  162. sub_header->start_display_time = 0;
  163. sub_header->end_display_time = 0;
  164. if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
  165. big_offsets = 1;
  166. offset_size = 4;
  167. cmd_pos = 6;
  168. } else {
  169. big_offsets = 0;
  170. offset_size = 2;
  171. cmd_pos = 2;
  172. }
  173. cmd_pos = READ_OFFSET(buf + cmd_pos);
  174. while ((cmd_pos + 2 + offset_size) < buf_size) {
  175. date = AV_RB16(buf + cmd_pos);
  176. next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
  177. dprintf(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
  178. cmd_pos, next_cmd_pos, date);
  179. pos = cmd_pos + 2 + offset_size;
  180. offset1 = -1;
  181. offset2 = -1;
  182. x1 = y1 = x2 = y2 = 0;
  183. while (pos < buf_size) {
  184. cmd = buf[pos++];
  185. dprintf(NULL, "cmd=%02x\n", cmd);
  186. switch(cmd) {
  187. case 0x00:
  188. /* menu subpicture */
  189. is_menu = 1;
  190. break;
  191. case 0x01:
  192. /* set start date */
  193. sub_header->start_display_time = (date << 10) / 90;
  194. break;
  195. case 0x02:
  196. /* set end date */
  197. sub_header->end_display_time = (date << 10) / 90;
  198. break;
  199. case 0x03:
  200. /* set colormap */
  201. if ((buf_size - pos) < 2)
  202. goto fail;
  203. colormap[3] = buf[pos] >> 4;
  204. colormap[2] = buf[pos] & 0x0f;
  205. colormap[1] = buf[pos + 1] >> 4;
  206. colormap[0] = buf[pos + 1] & 0x0f;
  207. pos += 2;
  208. break;
  209. case 0x04:
  210. /* set alpha */
  211. if ((buf_size - pos) < 2)
  212. goto fail;
  213. alpha[3] = buf[pos] >> 4;
  214. alpha[2] = buf[pos] & 0x0f;
  215. alpha[1] = buf[pos + 1] >> 4;
  216. alpha[0] = buf[pos + 1] & 0x0f;
  217. pos += 2;
  218. dprintf(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
  219. break;
  220. case 0x05:
  221. case 0x85:
  222. if ((buf_size - pos) < 6)
  223. goto fail;
  224. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  225. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  226. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  227. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  228. if (cmd & 0x80)
  229. is_8bit = 1;
  230. dprintf(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
  231. pos += 6;
  232. break;
  233. case 0x06:
  234. if ((buf_size - pos) < 4)
  235. goto fail;
  236. offset1 = AV_RB16(buf + pos);
  237. offset2 = AV_RB16(buf + pos + 2);
  238. dprintf(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  239. pos += 4;
  240. break;
  241. case 0x86:
  242. if ((buf_size - pos) < 8)
  243. goto fail;
  244. offset1 = AV_RB32(buf + pos);
  245. offset2 = AV_RB32(buf + pos + 4);
  246. dprintf(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  247. pos += 8;
  248. break;
  249. case 0x83:
  250. /* HD set palette */
  251. if ((buf_size - pos) < 768)
  252. goto fail;
  253. yuv_palette = buf + pos;
  254. pos += 768;
  255. break;
  256. case 0x84:
  257. /* HD set contrast (alpha) */
  258. if ((buf_size - pos) < 256)
  259. goto fail;
  260. for (i = 0; i < 256; i++)
  261. alpha[i] = 0xFF - buf[pos+i];
  262. pos += 256;
  263. break;
  264. case 0xff:
  265. goto the_end;
  266. default:
  267. dprintf(NULL, "unrecognised subpicture command 0x%x\n", cmd);
  268. goto the_end;
  269. }
  270. }
  271. the_end:
  272. if (offset1 >= 0) {
  273. int w, h;
  274. uint8_t *bitmap;
  275. /* decode the bitmap */
  276. w = x2 - x1 + 1;
  277. if (w < 0)
  278. w = 0;
  279. h = y2 - y1;
  280. if (h < 0)
  281. h = 0;
  282. if (w > 0 && h > 0) {
  283. if (sub_header->rects != NULL) {
  284. for (i = 0; i < sub_header->num_rects; i++) {
  285. av_freep(&sub_header->rects[i]->pict.data[0]);
  286. av_freep(&sub_header->rects[i]->pict.data[1]);
  287. av_freep(&sub_header->rects[i]);
  288. }
  289. av_freep(&sub_header->rects);
  290. sub_header->num_rects = 0;
  291. }
  292. bitmap = av_malloc(w * h);
  293. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  294. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  295. sub_header->num_rects = 1;
  296. sub_header->rects[0]->pict.data[0] = bitmap;
  297. decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  298. buf, offset1, buf_size, is_8bit);
  299. decode_rle(bitmap + w, w * 2, w, h / 2,
  300. buf, offset2, buf_size, is_8bit);
  301. if (is_8bit) {
  302. if (yuv_palette == 0)
  303. goto fail;
  304. sub_header->rects[0]->pict.data[1] = av_malloc(256 * 4);
  305. sub_header->rects[0]->nb_colors = 256;
  306. yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
  307. } else {
  308. sub_header->rects[0]->pict.data[1] = av_malloc(4 * 4);
  309. sub_header->rects[0]->nb_colors = 4;
  310. guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1],
  311. colormap, alpha, 0xffff00);
  312. }
  313. sub_header->rects[0]->x = x1;
  314. sub_header->rects[0]->y = y1;
  315. sub_header->rects[0]->w = w;
  316. sub_header->rects[0]->h = h;
  317. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  318. sub_header->rects[0]->pict.linesize[0] = w;
  319. }
  320. }
  321. if (next_cmd_pos == cmd_pos)
  322. break;
  323. cmd_pos = next_cmd_pos;
  324. }
  325. if (sub_header->num_rects > 0)
  326. return is_menu;
  327. fail:
  328. if (sub_header->rects != NULL) {
  329. for (i = 0; i < sub_header->num_rects; i++) {
  330. av_freep(&sub_header->rects[i]->pict.data[0]);
  331. av_freep(&sub_header->rects[i]->pict.data[1]);
  332. av_freep(&sub_header->rects[i]);
  333. }
  334. av_freep(&sub_header->rects);
  335. sub_header->num_rects = 0;
  336. }
  337. return -1;
  338. }
  339. static int is_transp(const uint8_t *buf, int pitch, int n,
  340. const uint8_t *transp_color)
  341. {
  342. int i;
  343. for(i = 0; i < n; i++) {
  344. if (!transp_color[*buf])
  345. return 0;
  346. buf += pitch;
  347. }
  348. return 1;
  349. }
  350. /* return 0 if empty rectangle, 1 if non empty */
  351. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  352. {
  353. uint8_t transp_color[256];
  354. int y1, y2, x1, x2, y, w, h, i;
  355. uint8_t *bitmap;
  356. if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  357. return 0;
  358. memset(transp_color, 0, 256);
  359. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  360. if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
  361. transp_color[i] = 1;
  362. }
  363. y1 = 0;
  364. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
  365. 1, s->rects[0]->w, transp_color))
  366. y1++;
  367. if (y1 == s->rects[0]->h) {
  368. av_freep(&s->rects[0]->pict.data[0]);
  369. s->rects[0]->w = s->rects[0]->h = 0;
  370. return 0;
  371. }
  372. y2 = s->rects[0]->h - 1;
  373. while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
  374. s->rects[0]->w, transp_color))
  375. y2--;
  376. x1 = 0;
  377. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
  378. s->rects[0]->h, transp_color))
  379. x1++;
  380. x2 = s->rects[0]->w - 1;
  381. while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
  382. transp_color))
  383. x2--;
  384. w = x2 - x1 + 1;
  385. h = y2 - y1 + 1;
  386. bitmap = av_malloc(w * h);
  387. if (!bitmap)
  388. return 1;
  389. for(y = 0; y < h; y++) {
  390. memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
  391. }
  392. av_freep(&s->rects[0]->pict.data[0]);
  393. s->rects[0]->pict.data[0] = bitmap;
  394. s->rects[0]->pict.linesize[0] = w;
  395. s->rects[0]->w = w;
  396. s->rects[0]->h = h;
  397. s->rects[0]->x += x1;
  398. s->rects[0]->y += y1;
  399. return 1;
  400. }
  401. #ifdef DEBUG
  402. #undef fprintf
  403. #undef perror
  404. #undef exit
  405. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  406. uint32_t *rgba_palette)
  407. {
  408. int x, y, v;
  409. FILE *f;
  410. f = fopen(filename, "w");
  411. if (!f) {
  412. perror(filename);
  413. exit(1);
  414. }
  415. fprintf(f, "P6\n"
  416. "%d %d\n"
  417. "%d\n",
  418. w, h, 255);
  419. for(y = 0; y < h; y++) {
  420. for(x = 0; x < w; x++) {
  421. v = rgba_palette[bitmap[y * w + x]];
  422. putc((v >> 16) & 0xff, f);
  423. putc((v >> 8) & 0xff, f);
  424. putc((v >> 0) & 0xff, f);
  425. }
  426. }
  427. fclose(f);
  428. }
  429. #endif
  430. static int dvdsub_decode(AVCodecContext *avctx,
  431. void *data, int *data_size,
  432. AVPacket *avpkt)
  433. {
  434. const uint8_t *buf = avpkt->data;
  435. int buf_size = avpkt->size;
  436. AVSubtitle *sub = (void *)data;
  437. int is_menu;
  438. is_menu = decode_dvd_subtitles(sub, buf, buf_size);
  439. if (is_menu < 0) {
  440. no_subtitle:
  441. *data_size = 0;
  442. return buf_size;
  443. }
  444. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  445. goto no_subtitle;
  446. #if defined(DEBUG)
  447. dprintf(NULL, "start=%d ms end =%d ms\n",
  448. sub->start_display_time,
  449. sub->end_display_time);
  450. ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
  451. sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
  452. #endif
  453. *data_size = 1;
  454. return buf_size;
  455. }
  456. AVCodec dvdsub_decoder = {
  457. "dvdsub",
  458. CODEC_TYPE_SUBTITLE,
  459. CODEC_ID_DVD_SUBTITLE,
  460. 0,
  461. NULL,
  462. NULL,
  463. NULL,
  464. dvdsub_decode,
  465. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  466. };