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.

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