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.

544 lines
17KB

  1. /*
  2. * DVD subtitle decoding
  3. * Copyright (c) 2005 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "dsputil.h"
  24. #include "libavutil/colorspace.h"
  25. //#define DEBUG
  26. typedef struct DVDSubContext {
  27. uint32_t palette[16];
  28. int has_palette;
  29. } DVDSubContext;
  30. static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
  31. {
  32. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  33. uint8_t r, g, b;
  34. int i, y, cb, cr;
  35. int r_add, g_add, b_add;
  36. for (i = num_values; i > 0; i--) {
  37. y = *ycbcr++;
  38. cr = *ycbcr++;
  39. cb = *ycbcr++;
  40. YUV_TO_RGB1_CCIR(cb, cr);
  41. YUV_TO_RGB2_CCIR(r, g, b, y);
  42. *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
  43. }
  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(DVDSubContext* ctx,
  113. uint32_t *rgba_palette,
  114. uint8_t *colormap,
  115. uint8_t *alpha,
  116. uint32_t subtitle_color)
  117. {
  118. uint8_t color_used[16] = { 0 };
  119. int nb_opaque_colors, i, level, j, r, g, b;
  120. if (ctx->has_palette) {
  121. for (i = 0; i < 4; i++)
  122. rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
  123. | ((alpha[i] * 17) << 24);
  124. return;
  125. }
  126. for(i = 0; i < 4; i++)
  127. rgba_palette[i] = 0;
  128. nb_opaque_colors = 0;
  129. for(i = 0; i < 4; i++) {
  130. if (alpha[i] != 0 && !color_used[colormap[i]]) {
  131. color_used[colormap[i]] = 1;
  132. nb_opaque_colors++;
  133. }
  134. }
  135. if (nb_opaque_colors == 0)
  136. return;
  137. j = nb_opaque_colors;
  138. memset(color_used, 0, 16);
  139. for(i = 0; i < 4; i++) {
  140. if (alpha[i] != 0) {
  141. if (!color_used[colormap[i]]) {
  142. level = (0xff * j) / nb_opaque_colors;
  143. r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
  144. g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
  145. b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
  146. rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
  147. color_used[colormap[i]] = (i + 1);
  148. j--;
  149. } else {
  150. rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
  151. ((alpha[i] * 17) << 24);
  152. }
  153. }
  154. }
  155. }
  156. #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
  157. static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
  158. const uint8_t *buf, int buf_size)
  159. {
  160. int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
  161. int big_offsets, offset_size, is_8bit = 0;
  162. const uint8_t *yuv_palette = 0;
  163. uint8_t colormap[4] = { 0 }, alpha[256] = { 0 };
  164. int date;
  165. int i;
  166. int is_menu = 0;
  167. if (buf_size < 10)
  168. return -1;
  169. memset(sub_header, 0, sizeof(*sub_header));
  170. if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
  171. big_offsets = 1;
  172. offset_size = 4;
  173. cmd_pos = 6;
  174. } else {
  175. big_offsets = 0;
  176. offset_size = 2;
  177. cmd_pos = 2;
  178. }
  179. cmd_pos = READ_OFFSET(buf + cmd_pos);
  180. while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
  181. date = AV_RB16(buf + cmd_pos);
  182. next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
  183. av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
  184. cmd_pos, next_cmd_pos, date);
  185. pos = cmd_pos + 2 + offset_size;
  186. offset1 = -1;
  187. offset2 = -1;
  188. x1 = y1 = x2 = y2 = 0;
  189. while (pos < buf_size) {
  190. cmd = buf[pos++];
  191. av_dlog(NULL, "cmd=%02x\n", cmd);
  192. switch(cmd) {
  193. case 0x00:
  194. /* menu subpicture */
  195. is_menu = 1;
  196. break;
  197. case 0x01:
  198. /* set start date */
  199. sub_header->start_display_time = (date << 10) / 90;
  200. break;
  201. case 0x02:
  202. /* set end date */
  203. sub_header->end_display_time = (date << 10) / 90;
  204. break;
  205. case 0x03:
  206. /* set colormap */
  207. if ((buf_size - pos) < 2)
  208. goto fail;
  209. colormap[3] = buf[pos] >> 4;
  210. colormap[2] = buf[pos] & 0x0f;
  211. colormap[1] = buf[pos + 1] >> 4;
  212. colormap[0] = buf[pos + 1] & 0x0f;
  213. pos += 2;
  214. break;
  215. case 0x04:
  216. /* set alpha */
  217. if ((buf_size - pos) < 2)
  218. goto fail;
  219. alpha[3] = buf[pos] >> 4;
  220. alpha[2] = buf[pos] & 0x0f;
  221. alpha[1] = buf[pos + 1] >> 4;
  222. alpha[0] = buf[pos + 1] & 0x0f;
  223. pos += 2;
  224. av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
  225. break;
  226. case 0x05:
  227. case 0x85:
  228. if ((buf_size - pos) < 6)
  229. goto fail;
  230. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  231. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  232. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  233. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  234. if (cmd & 0x80)
  235. is_8bit = 1;
  236. av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
  237. pos += 6;
  238. break;
  239. case 0x06:
  240. if ((buf_size - pos) < 4)
  241. goto fail;
  242. offset1 = AV_RB16(buf + pos);
  243. offset2 = AV_RB16(buf + pos + 2);
  244. av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  245. pos += 4;
  246. break;
  247. case 0x86:
  248. if ((buf_size - pos) < 8)
  249. goto fail;
  250. offset1 = AV_RB32(buf + pos);
  251. offset2 = AV_RB32(buf + pos + 4);
  252. av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  253. pos += 8;
  254. break;
  255. case 0x83:
  256. /* HD set palette */
  257. if ((buf_size - pos) < 768)
  258. goto fail;
  259. yuv_palette = buf + pos;
  260. pos += 768;
  261. break;
  262. case 0x84:
  263. /* HD set contrast (alpha) */
  264. if ((buf_size - pos) < 256)
  265. goto fail;
  266. for (i = 0; i < 256; i++)
  267. alpha[i] = 0xFF - buf[pos+i];
  268. pos += 256;
  269. break;
  270. case 0xff:
  271. goto the_end;
  272. default:
  273. av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
  274. goto the_end;
  275. }
  276. }
  277. the_end:
  278. if (offset1 >= 0) {
  279. int w, h;
  280. uint8_t *bitmap;
  281. /* decode the bitmap */
  282. w = x2 - x1 + 1;
  283. if (w < 0)
  284. w = 0;
  285. h = y2 - y1;
  286. if (h < 0)
  287. h = 0;
  288. if (w > 0 && h > 0) {
  289. if (sub_header->rects != NULL) {
  290. for (i = 0; i < sub_header->num_rects; i++) {
  291. av_freep(&sub_header->rects[i]->pict.data[0]);
  292. av_freep(&sub_header->rects[i]->pict.data[1]);
  293. av_freep(&sub_header->rects[i]);
  294. }
  295. av_freep(&sub_header->rects);
  296. sub_header->num_rects = 0;
  297. }
  298. bitmap = av_malloc(w * h);
  299. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  300. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  301. sub_header->num_rects = 1;
  302. sub_header->rects[0]->pict.data[0] = bitmap;
  303. decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  304. buf, offset1, buf_size, is_8bit);
  305. decode_rle(bitmap + w, w * 2, w, h / 2,
  306. buf, offset2, buf_size, is_8bit);
  307. sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  308. if (is_8bit) {
  309. if (yuv_palette == 0)
  310. goto fail;
  311. sub_header->rects[0]->nb_colors = 256;
  312. yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
  313. } else {
  314. sub_header->rects[0]->nb_colors = 4;
  315. guess_palette(ctx,
  316. (uint32_t*)sub_header->rects[0]->pict.data[1],
  317. colormap, alpha, 0xffff00);
  318. }
  319. sub_header->rects[0]->x = x1;
  320. sub_header->rects[0]->y = y1;
  321. sub_header->rects[0]->w = w;
  322. sub_header->rects[0]->h = h;
  323. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  324. sub_header->rects[0]->pict.linesize[0] = w;
  325. }
  326. }
  327. if (next_cmd_pos == cmd_pos)
  328. break;
  329. cmd_pos = next_cmd_pos;
  330. }
  331. if (sub_header->num_rects > 0)
  332. return is_menu;
  333. fail:
  334. if (sub_header->rects != NULL) {
  335. for (i = 0; i < sub_header->num_rects; i++) {
  336. av_freep(&sub_header->rects[i]->pict.data[0]);
  337. av_freep(&sub_header->rects[i]->pict.data[1]);
  338. av_freep(&sub_header->rects[i]);
  339. }
  340. av_freep(&sub_header->rects);
  341. sub_header->num_rects = 0;
  342. }
  343. return -1;
  344. }
  345. static int is_transp(const uint8_t *buf, int pitch, int n,
  346. const uint8_t *transp_color)
  347. {
  348. int i;
  349. for(i = 0; i < n; i++) {
  350. if (!transp_color[*buf])
  351. return 0;
  352. buf += pitch;
  353. }
  354. return 1;
  355. }
  356. /* return 0 if empty rectangle, 1 if non empty */
  357. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  358. {
  359. uint8_t transp_color[256] = { 0 };
  360. int y1, y2, x1, x2, y, w, h, i;
  361. uint8_t *bitmap;
  362. if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  363. return 0;
  364. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  365. if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
  366. transp_color[i] = 1;
  367. }
  368. y1 = 0;
  369. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
  370. 1, s->rects[0]->w, transp_color))
  371. y1++;
  372. if (y1 == s->rects[0]->h) {
  373. av_freep(&s->rects[0]->pict.data[0]);
  374. s->rects[0]->w = s->rects[0]->h = 0;
  375. return 0;
  376. }
  377. y2 = s->rects[0]->h - 1;
  378. while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
  379. s->rects[0]->w, transp_color))
  380. y2--;
  381. x1 = 0;
  382. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
  383. s->rects[0]->h, transp_color))
  384. x1++;
  385. x2 = s->rects[0]->w - 1;
  386. while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
  387. transp_color))
  388. x2--;
  389. w = x2 - x1 + 1;
  390. h = y2 - y1 + 1;
  391. bitmap = av_malloc(w * h);
  392. if (!bitmap)
  393. return 1;
  394. for(y = 0; y < h; y++) {
  395. memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
  396. }
  397. av_freep(&s->rects[0]->pict.data[0]);
  398. s->rects[0]->pict.data[0] = bitmap;
  399. s->rects[0]->pict.linesize[0] = w;
  400. s->rects[0]->w = w;
  401. s->rects[0]->h = h;
  402. s->rects[0]->x += x1;
  403. s->rects[0]->y += y1;
  404. return 1;
  405. }
  406. #ifdef DEBUG
  407. #undef fprintf
  408. #undef perror
  409. #undef exit
  410. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  411. uint32_t *rgba_palette)
  412. {
  413. int x, y, v;
  414. FILE *f;
  415. f = fopen(filename, "w");
  416. if (!f) {
  417. perror(filename);
  418. exit(1);
  419. }
  420. fprintf(f, "P6\n"
  421. "%d %d\n"
  422. "%d\n",
  423. w, h, 255);
  424. for(y = 0; y < h; y++) {
  425. for(x = 0; x < w; x++) {
  426. v = rgba_palette[bitmap[y * w + x]];
  427. putc((v >> 16) & 0xff, f);
  428. putc((v >> 8) & 0xff, f);
  429. putc((v >> 0) & 0xff, f);
  430. }
  431. }
  432. fclose(f);
  433. }
  434. #endif
  435. static int dvdsub_decode(AVCodecContext *avctx,
  436. void *data, int *data_size,
  437. AVPacket *avpkt)
  438. {
  439. DVDSubContext *ctx = avctx->priv_data;
  440. const uint8_t *buf = avpkt->data;
  441. int buf_size = avpkt->size;
  442. AVSubtitle *sub = data;
  443. int is_menu;
  444. is_menu = decode_dvd_subtitles(ctx, 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_dlog(NULL, "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]->pict.data[0],
  457. sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
  458. #endif
  459. *data_size = 1;
  460. return buf_size;
  461. }
  462. static int dvdsub_init(AVCodecContext *avctx)
  463. {
  464. DVDSubContext *ctx = avctx->priv_data;
  465. char *data, *cur;
  466. if (!avctx->extradata || !avctx->extradata_size)
  467. return 0;
  468. data = av_malloc(avctx->extradata_size + 1);
  469. if (!data)
  470. return AVERROR(ENOMEM);
  471. memcpy(data, avctx->extradata, avctx->extradata_size);
  472. data[avctx->extradata_size] = '\0';
  473. cur = data;
  474. while (*cur) {
  475. if (strncmp("palette:", cur, 8) == 0) {
  476. int i;
  477. char *p = cur + 8;
  478. ctx->has_palette = 1;
  479. for (i = 0; i < 16; i++) {
  480. ctx->palette[i] = strtoul(p, &p, 16);
  481. while (*p == ',' || isspace(*p))
  482. p++;
  483. }
  484. }
  485. cur += strcspn(cur, "\n\r");
  486. cur += strspn(cur, "\n\r");
  487. }
  488. av_free(data);
  489. return 0;
  490. }
  491. AVCodec ff_dvdsub_decoder = {
  492. .name = "dvdsub",
  493. .type = AVMEDIA_TYPE_SUBTITLE,
  494. .id = AV_CODEC_ID_DVD_SUBTITLE,
  495. .priv_data_size = sizeof(DVDSubContext),
  496. .init = dvdsub_init,
  497. .decode = dvdsub_decode,
  498. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  499. };