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.

579 lines
18KB

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