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.

575 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 "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, offset1, offset2, 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. if (buf_size < 10)
  170. return -1;
  171. memset(sub_header, 0, sizeof(*sub_header));
  172. if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
  173. big_offsets = 1;
  174. offset_size = 4;
  175. cmd_pos = 6;
  176. } else {
  177. big_offsets = 0;
  178. offset_size = 2;
  179. cmd_pos = 2;
  180. }
  181. cmd_pos = READ_OFFSET(buf + cmd_pos);
  182. while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
  183. date = AV_RB16(buf + cmd_pos);
  184. next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
  185. ff_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
  186. cmd_pos, next_cmd_pos, date);
  187. pos = cmd_pos + 2 + offset_size;
  188. offset1 = -1;
  189. offset2 = -1;
  190. x1 = y1 = x2 = y2 = 0;
  191. while (pos < buf_size) {
  192. cmd = buf[pos++];
  193. ff_dlog(NULL, "cmd=%02x\n", cmd);
  194. switch(cmd) {
  195. case 0x00:
  196. /* menu subpicture */
  197. is_menu = 1;
  198. break;
  199. case 0x01:
  200. /* set start date */
  201. sub_header->start_display_time = (date << 10) / 90;
  202. break;
  203. case 0x02:
  204. /* set end date */
  205. sub_header->end_display_time = (date << 10) / 90;
  206. break;
  207. case 0x03:
  208. /* set colormap */
  209. if ((buf_size - pos) < 2)
  210. goto fail;
  211. colormap[3] = buf[pos] >> 4;
  212. colormap[2] = buf[pos] & 0x0f;
  213. colormap[1] = buf[pos + 1] >> 4;
  214. colormap[0] = buf[pos + 1] & 0x0f;
  215. pos += 2;
  216. break;
  217. case 0x04:
  218. /* set alpha */
  219. if ((buf_size - pos) < 2)
  220. goto fail;
  221. alpha[3] = buf[pos] >> 4;
  222. alpha[2] = buf[pos] & 0x0f;
  223. alpha[1] = buf[pos + 1] >> 4;
  224. alpha[0] = buf[pos + 1] & 0x0f;
  225. pos += 2;
  226. ff_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
  227. break;
  228. case 0x05:
  229. case 0x85:
  230. if ((buf_size - pos) < 6)
  231. goto fail;
  232. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  233. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  234. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  235. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  236. if (cmd & 0x80)
  237. is_8bit = 1;
  238. ff_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
  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. ff_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  247. pos += 4;
  248. break;
  249. case 0x86:
  250. if ((buf_size - pos) < 8)
  251. goto fail;
  252. offset1 = AV_RB32(buf + pos);
  253. offset2 = AV_RB32(buf + pos + 4);
  254. ff_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  255. pos += 8;
  256. break;
  257. case 0x83:
  258. /* HD set palette */
  259. if ((buf_size - pos) < 768)
  260. goto fail;
  261. yuv_palette = buf + pos;
  262. pos += 768;
  263. break;
  264. case 0x84:
  265. /* HD set contrast (alpha) */
  266. if ((buf_size - pos) < 256)
  267. goto fail;
  268. for (i = 0; i < 256; i++)
  269. alpha[i] = 0xFF - buf[pos+i];
  270. pos += 256;
  271. break;
  272. case 0xff:
  273. goto the_end;
  274. default:
  275. ff_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
  276. goto the_end;
  277. }
  278. }
  279. the_end:
  280. if (offset1 >= 0) {
  281. int w, h;
  282. uint8_t *bitmap;
  283. /* decode the bitmap */
  284. w = x2 - x1 + 1;
  285. if (w < 0)
  286. w = 0;
  287. h = y2 - y1;
  288. if (h < 0)
  289. h = 0;
  290. if (w > 0 && h > 0) {
  291. int j;
  292. AVSubtitleRect *rect;
  293. if (sub_header->rects) {
  294. for (i = 0; i < sub_header->num_rects; i++) {
  295. av_freep(&sub_header->rects[i]->data[0]);
  296. av_freep(&sub_header->rects[i]->data[1]);
  297. av_freep(&sub_header->rects[i]);
  298. }
  299. av_freep(&sub_header->rects);
  300. sub_header->num_rects = 0;
  301. }
  302. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  303. if (!sub_header->rects)
  304. goto fail;
  305. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  306. if (!sub_header->rects[0])
  307. goto fail;
  308. sub_header->num_rects = 1;
  309. bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
  310. if (!bitmap)
  311. goto fail;
  312. decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  313. buf, offset1, buf_size, is_8bit);
  314. decode_rle(bitmap + w, w * 2, w, h / 2,
  315. buf, offset2, buf_size, is_8bit);
  316. sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
  317. if (!sub_header->rects[0]->data[1])
  318. goto fail;
  319. if (is_8bit) {
  320. if (yuv_palette == 0)
  321. goto fail;
  322. sub_header->rects[0]->nb_colors = 256;
  323. yuv_a_to_rgba(yuv_palette, alpha,
  324. (uint32_t *)sub_header->rects[0]->data[1],
  325. 256);
  326. } else {
  327. sub_header->rects[0]->nb_colors = 4;
  328. guess_palette(ctx,
  329. (uint32_t *)sub_header->rects[0]->data[1],
  330. colormap, alpha, 0xffff00);
  331. }
  332. sub_header->rects[0]->x = x1;
  333. sub_header->rects[0]->y = y1;
  334. sub_header->rects[0]->w = w;
  335. sub_header->rects[0]->h = h;
  336. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  337. sub_header->rects[0]->linesize[0] = w;
  338. #if FF_API_AVPICTURE
  339. FF_DISABLE_DEPRECATION_WARNINGS
  340. rect = sub_header->rects[0];
  341. for (j = 0; j < 4; j++) {
  342. rect->pict.data[j] = rect->data[j];
  343. rect->pict.linesize[j] = rect->linesize[j];
  344. }
  345. FF_ENABLE_DEPRECATION_WARNINGS
  346. #endif
  347. }
  348. }
  349. if (next_cmd_pos == cmd_pos)
  350. break;
  351. cmd_pos = next_cmd_pos;
  352. }
  353. if (sub_header->num_rects > 0)
  354. return is_menu;
  355. fail:
  356. if (!sub_header->rects) {
  357. for (i = 0; i < sub_header->num_rects; i++) {
  358. av_freep(&sub_header->rects[i]->data[0]);
  359. av_freep(&sub_header->rects[i]->data[1]);
  360. av_freep(&sub_header->rects[i]);
  361. }
  362. av_freep(&sub_header->rects);
  363. sub_header->num_rects = 0;
  364. }
  365. return -1;
  366. }
  367. static int is_transp(const uint8_t *buf, int pitch, int n,
  368. const uint8_t *transp_color)
  369. {
  370. int i;
  371. for(i = 0; i < n; i++) {
  372. if (!transp_color[*buf])
  373. return 0;
  374. buf += pitch;
  375. }
  376. return 1;
  377. }
  378. /* return 0 if empty rectangle, 1 if non empty */
  379. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  380. {
  381. uint8_t transp_color[256] = { 0 };
  382. int y1, y2, x1, x2, y, w, h, i;
  383. uint8_t *bitmap;
  384. if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  385. return 0;
  386. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  387. if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0)
  388. transp_color[i] = 1;
  389. }
  390. y1 = 0;
  391. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
  392. 1, s->rects[0]->w, transp_color))
  393. y1++;
  394. if (y1 == s->rects[0]->h) {
  395. av_freep(&s->rects[0]->data[0]);
  396. s->rects[0]->w = s->rects[0]->h = 0;
  397. return 0;
  398. }
  399. y2 = s->rects[0]->h - 1;
  400. while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
  401. s->rects[0]->w, transp_color))
  402. y2--;
  403. x1 = 0;
  404. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
  405. s->rects[0]->h, transp_color))
  406. x1++;
  407. x2 = s->rects[0]->w - 1;
  408. while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
  409. transp_color))
  410. x2--;
  411. w = x2 - x1 + 1;
  412. h = y2 - y1 + 1;
  413. bitmap = av_malloc(w * h);
  414. if (!bitmap)
  415. return 1;
  416. for(y = 0; y < h; y++) {
  417. memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
  418. }
  419. av_freep(&s->rects[0]->data[0]);
  420. s->rects[0]->data[0] = bitmap;
  421. s->rects[0]->linesize[0] = w;
  422. s->rects[0]->w = w;
  423. s->rects[0]->h = h;
  424. s->rects[0]->x += x1;
  425. s->rects[0]->y += y1;
  426. return 1;
  427. }
  428. #ifdef DEBUG
  429. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  430. uint32_t *rgba_palette)
  431. {
  432. int x, y, v;
  433. FILE *f;
  434. f = fopen(filename, "w");
  435. if (!f) {
  436. perror(filename);
  437. exit(1);
  438. }
  439. fprintf(f, "P6\n"
  440. "%d %d\n"
  441. "%d\n",
  442. w, h, 255);
  443. for(y = 0; y < h; y++) {
  444. for(x = 0; x < w; x++) {
  445. v = rgba_palette[bitmap[y * w + x]];
  446. putc((v >> 16) & 0xff, f);
  447. putc((v >> 8) & 0xff, f);
  448. putc((v >> 0) & 0xff, f);
  449. }
  450. }
  451. fclose(f);
  452. }
  453. #endif
  454. static int dvdsub_decode(AVCodecContext *avctx,
  455. void *data, int *data_size,
  456. AVPacket *avpkt)
  457. {
  458. DVDSubContext *ctx = avctx->priv_data;
  459. const uint8_t *buf = avpkt->data;
  460. int buf_size = avpkt->size;
  461. AVSubtitle *sub = data;
  462. int is_menu;
  463. is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
  464. if (is_menu < 0) {
  465. no_subtitle:
  466. *data_size = 0;
  467. return buf_size;
  468. }
  469. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  470. goto no_subtitle;
  471. #if defined(DEBUG)
  472. ff_dlog(NULL, "start=%d ms end =%d ms\n",
  473. sub->start_display_time,
  474. sub->end_display_time);
  475. ppm_save("/tmp/a.ppm", sub->rects[0]->data[0],
  476. sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->data[1]);
  477. #endif
  478. *data_size = 1;
  479. return buf_size;
  480. }
  481. static av_cold int dvdsub_init(AVCodecContext *avctx)
  482. {
  483. DVDSubContext *ctx = avctx->priv_data;
  484. char *data, *cur;
  485. int ret = 0;
  486. if (!avctx->extradata || !avctx->extradata_size)
  487. return 0;
  488. data = av_malloc(avctx->extradata_size + 1);
  489. if (!data)
  490. return AVERROR(ENOMEM);
  491. memcpy(data, avctx->extradata, avctx->extradata_size);
  492. data[avctx->extradata_size] = '\0';
  493. cur = data;
  494. while (*cur) {
  495. if (strncmp("palette:", cur, 8) == 0) {
  496. int i;
  497. char *p = cur + 8;
  498. ctx->has_palette = 1;
  499. for (i = 0; i < 16; i++) {
  500. ctx->palette[i] = strtoul(p, &p, 16);
  501. while (*p == ',' || av_isspace(*p))
  502. p++;
  503. }
  504. } else if (!strncmp("size:", cur, 5)) {
  505. int w, h;
  506. if (sscanf(cur + 5, "%dx%d", &w, &h) == 2) {
  507. ret = ff_set_dimensions(avctx, w, h);
  508. if (ret < 0)
  509. goto fail;
  510. }
  511. }
  512. cur += strcspn(cur, "\n\r");
  513. cur += strspn(cur, "\n\r");
  514. }
  515. fail:
  516. av_free(data);
  517. return ret;
  518. }
  519. AVCodec ff_dvdsub_decoder = {
  520. .name = "dvdsub",
  521. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  522. .type = AVMEDIA_TYPE_SUBTITLE,
  523. .id = AV_CODEC_ID_DVD_SUBTITLE,
  524. .priv_data_size = sizeof(DVDSubContext),
  525. .init = dvdsub_init,
  526. .decode = dvdsub_decode,
  527. };