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.

582 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 "bitstream.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(BitstreamContext *bc, 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) | bitstream_read(bc, 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(BitstreamContext *bc, int *color)
  60. {
  61. int len;
  62. int has_run = bitstream_read_bit(bc);
  63. if (bitstream_read_bit(bc))
  64. *color = bitstream_read(bc, 8);
  65. else
  66. *color = bitstream_read(bc, 2);
  67. if (has_run) {
  68. if (bitstream_read_bit(bc)) {
  69. len = bitstream_read(bc, 7);
  70. if (len == 0)
  71. len = INT_MAX;
  72. else
  73. len += 9;
  74. } else
  75. len = bitstream_read(bc, 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. BitstreamContext bc;
  84. int bit_len;
  85. int x, y, len, color;
  86. uint8_t *d;
  87. bit_len = (buf_size - start) * 8;
  88. bitstream_init(&bc, buf + start, bit_len);
  89. x = 0;
  90. y = 0;
  91. d = bitmap;
  92. for(;;) {
  93. if (bitstream_tell(&bc) > bit_len)
  94. return -1;
  95. if (is_8bit)
  96. len = decode_run_8bit(&bc, &color);
  97. else
  98. len = decode_run_2bit(&bc, &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. bitstream_align(&bc);
  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=%"PRIx8"%"PRIx8"%"PRIx8"%"PRIx8"\n",
  228. alpha[0], alpha[1], alpha[2], alpha[3]);
  229. break;
  230. case 0x05:
  231. case 0x85:
  232. if ((buf_size - pos) < 6)
  233. goto fail;
  234. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  235. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  236. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  237. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  238. if (cmd & 0x80)
  239. is_8bit = 1;
  240. ff_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
  241. pos += 6;
  242. break;
  243. case 0x06:
  244. if ((buf_size - pos) < 4)
  245. goto fail;
  246. offset1 = AV_RB16(buf + pos);
  247. offset2 = AV_RB16(buf + pos + 2);
  248. ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
  249. pos += 4;
  250. break;
  251. case 0x86:
  252. if ((buf_size - pos) < 8)
  253. goto fail;
  254. offset1 = AV_RB32(buf + pos);
  255. offset2 = AV_RB32(buf + pos + 4);
  256. ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
  257. pos += 8;
  258. break;
  259. case 0x83:
  260. /* HD set palette */
  261. if ((buf_size - pos) < 768)
  262. goto fail;
  263. yuv_palette = buf + pos;
  264. pos += 768;
  265. break;
  266. case 0x84:
  267. /* HD set contrast (alpha) */
  268. if ((buf_size - pos) < 256)
  269. goto fail;
  270. for (i = 0; i < 256; i++)
  271. alpha[i] = 0xFF - buf[pos+i];
  272. pos += 256;
  273. break;
  274. case 0xff:
  275. goto the_end;
  276. default:
  277. ff_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
  278. goto the_end;
  279. }
  280. }
  281. the_end:
  282. if (offset1 >= buf_size || offset2 >= buf_size)
  283. goto fail;
  284. if (offset1 >= 0) {
  285. int w, h;
  286. uint8_t *bitmap;
  287. /* decode the bitmap */
  288. w = x2 - x1 + 1;
  289. if (w < 0)
  290. w = 0;
  291. h = y2 - y1;
  292. if (h < 0)
  293. h = 0;
  294. if (w > 0 && h > 0) {
  295. if (sub_header->rects) {
  296. for (i = 0; i < sub_header->num_rects; i++) {
  297. av_freep(&sub_header->rects[i]->data[0]);
  298. av_freep(&sub_header->rects[i]->data[1]);
  299. av_freep(&sub_header->rects[i]);
  300. }
  301. av_freep(&sub_header->rects);
  302. sub_header->num_rects = 0;
  303. }
  304. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  305. if (!sub_header->rects)
  306. goto fail;
  307. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  308. if (!sub_header->rects[0])
  309. goto fail;
  310. sub_header->num_rects = 1;
  311. bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
  312. if (!bitmap)
  313. goto fail;
  314. decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  315. buf, offset1, buf_size, is_8bit);
  316. decode_rle(bitmap + w, w * 2, w, h / 2,
  317. buf, offset2, buf_size, is_8bit);
  318. sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
  319. if (!sub_header->rects[0]->data[1])
  320. goto fail;
  321. if (is_8bit) {
  322. if (yuv_palette == 0)
  323. goto fail;
  324. sub_header->rects[0]->nb_colors = 256;
  325. yuv_a_to_rgba(yuv_palette, alpha,
  326. (uint32_t *)sub_header->rects[0]->data[1],
  327. 256);
  328. } else {
  329. sub_header->rects[0]->nb_colors = 4;
  330. guess_palette(ctx,
  331. (uint32_t *)sub_header->rects[0]->data[1],
  332. colormap, alpha, 0xffff00);
  333. }
  334. sub_header->rects[0]->x = x1;
  335. sub_header->rects[0]->y = y1;
  336. sub_header->rects[0]->w = w;
  337. sub_header->rects[0]->h = h;
  338. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  339. sub_header->rects[0]->linesize[0] = w;
  340. #if FF_API_AVPICTURE
  341. FF_DISABLE_DEPRECATION_WARNINGS
  342. {
  343. int j;
  344. AVSubtitleRect *rect;
  345. rect = sub_header->rects[0];
  346. for (j = 0; j < 4; j++) {
  347. rect->pict.data[j] = rect->data[j];
  348. rect->pict.linesize[j] = rect->linesize[j];
  349. }
  350. }
  351. FF_ENABLE_DEPRECATION_WARNINGS
  352. #endif
  353. }
  354. }
  355. if (next_cmd_pos == cmd_pos)
  356. break;
  357. cmd_pos = next_cmd_pos;
  358. }
  359. if (sub_header->num_rects > 0)
  360. return is_menu;
  361. fail:
  362. if (!sub_header->rects) {
  363. for (i = 0; i < sub_header->num_rects; i++) {
  364. av_freep(&sub_header->rects[i]->data[0]);
  365. av_freep(&sub_header->rects[i]->data[1]);
  366. av_freep(&sub_header->rects[i]);
  367. }
  368. av_freep(&sub_header->rects);
  369. sub_header->num_rects = 0;
  370. }
  371. return -1;
  372. }
  373. static int is_transp(const uint8_t *buf, int pitch, int n,
  374. const uint8_t *transp_color)
  375. {
  376. int i;
  377. for(i = 0; i < n; i++) {
  378. if (!transp_color[*buf])
  379. return 0;
  380. buf += pitch;
  381. }
  382. return 1;
  383. }
  384. /* return 0 if empty rectangle, 1 if non empty */
  385. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  386. {
  387. uint8_t transp_color[256] = { 0 };
  388. int y1, y2, x1, x2, y, w, h, i;
  389. uint8_t *bitmap;
  390. if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  391. return 0;
  392. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  393. if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0)
  394. transp_color[i] = 1;
  395. }
  396. y1 = 0;
  397. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
  398. 1, s->rects[0]->w, transp_color))
  399. y1++;
  400. if (y1 == s->rects[0]->h) {
  401. av_freep(&s->rects[0]->data[0]);
  402. s->rects[0]->w = s->rects[0]->h = 0;
  403. return 0;
  404. }
  405. y2 = s->rects[0]->h - 1;
  406. while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
  407. s->rects[0]->w, transp_color))
  408. y2--;
  409. x1 = 0;
  410. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
  411. s->rects[0]->h, transp_color))
  412. x1++;
  413. x2 = s->rects[0]->w - 1;
  414. while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
  415. transp_color))
  416. x2--;
  417. w = x2 - x1 + 1;
  418. h = y2 - y1 + 1;
  419. bitmap = av_malloc(w * h);
  420. if (!bitmap)
  421. return 1;
  422. for(y = 0; y < h; y++) {
  423. memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
  424. }
  425. av_freep(&s->rects[0]->data[0]);
  426. s->rects[0]->data[0] = bitmap;
  427. s->rects[0]->linesize[0] = w;
  428. s->rects[0]->w = w;
  429. s->rects[0]->h = h;
  430. s->rects[0]->x += x1;
  431. s->rects[0]->y += y1;
  432. return 1;
  433. }
  434. #ifdef DEBUG
  435. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  436. uint32_t *rgba_palette)
  437. {
  438. int x, y, v;
  439. FILE *f;
  440. f = fopen(filename, "w");
  441. if (!f) {
  442. perror(filename);
  443. exit(1);
  444. }
  445. fprintf(f, "P6\n"
  446. "%d %d\n"
  447. "%d\n",
  448. w, h, 255);
  449. for(y = 0; y < h; y++) {
  450. for(x = 0; x < w; x++) {
  451. v = rgba_palette[bitmap[y * w + x]];
  452. putc((v >> 16) & 0xff, f);
  453. putc((v >> 8) & 0xff, f);
  454. putc((v >> 0) & 0xff, f);
  455. }
  456. }
  457. fclose(f);
  458. }
  459. #endif
  460. static int dvdsub_decode(AVCodecContext *avctx,
  461. void *data, int *data_size,
  462. AVPacket *avpkt)
  463. {
  464. DVDSubContext *ctx = avctx->priv_data;
  465. const uint8_t *buf = avpkt->data;
  466. int buf_size = avpkt->size;
  467. AVSubtitle *sub = data;
  468. int is_menu;
  469. is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
  470. if (is_menu < 0) {
  471. no_subtitle:
  472. *data_size = 0;
  473. return buf_size;
  474. }
  475. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  476. goto no_subtitle;
  477. #if defined(DEBUG)
  478. ff_dlog(NULL, "start=%"PRIu32" ms end =%"PRIu32" ms\n",
  479. sub->start_display_time,
  480. sub->end_display_time);
  481. ppm_save("/tmp/a.ppm", sub->rects[0]->data[0],
  482. sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->data[1]);
  483. #endif
  484. *data_size = 1;
  485. return buf_size;
  486. }
  487. static av_cold int dvdsub_init(AVCodecContext *avctx)
  488. {
  489. DVDSubContext *ctx = avctx->priv_data;
  490. char *data, *cur;
  491. int ret = 0;
  492. if (!avctx->extradata || !avctx->extradata_size)
  493. return 0;
  494. data = av_malloc(avctx->extradata_size + 1);
  495. if (!data)
  496. return AVERROR(ENOMEM);
  497. memcpy(data, avctx->extradata, avctx->extradata_size);
  498. data[avctx->extradata_size] = '\0';
  499. cur = data;
  500. while (*cur) {
  501. if (strncmp("palette:", cur, 8) == 0) {
  502. int i;
  503. char *p = cur + 8;
  504. ctx->has_palette = 1;
  505. for (i = 0; i < 16; i++) {
  506. ctx->palette[i] = strtoul(p, &p, 16);
  507. while (*p == ',' || av_isspace(*p))
  508. p++;
  509. }
  510. } else if (!strncmp("size:", cur, 5)) {
  511. int w, h;
  512. if (sscanf(cur + 5, "%dx%d", &w, &h) == 2) {
  513. ret = ff_set_dimensions(avctx, w, h);
  514. if (ret < 0)
  515. goto fail;
  516. }
  517. }
  518. cur += strcspn(cur, "\n\r");
  519. cur += strspn(cur, "\n\r");
  520. }
  521. fail:
  522. av_free(data);
  523. return ret;
  524. }
  525. AVCodec ff_dvdsub_decoder = {
  526. .name = "dvdsub",
  527. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  528. .type = AVMEDIA_TYPE_SUBTITLE,
  529. .id = AV_CODEC_ID_DVD_SUBTITLE,
  530. .priv_data_size = sizeof(DVDSubContext),
  531. .init = dvdsub_init,
  532. .decode = dvdsub_decode,
  533. };