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.

551 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 "internal.h"
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/colorspace.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/avstring.h"
  29. typedef struct DVDSubContext {
  30. uint32_t palette[16];
  31. int has_palette;
  32. } DVDSubContext;
  33. static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
  34. {
  35. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  36. uint8_t r, g, b;
  37. int i, y, cb, cr;
  38. int r_add, g_add, b_add;
  39. for (i = num_values; i > 0; i--) {
  40. y = *ycbcr++;
  41. cr = *ycbcr++;
  42. cb = *ycbcr++;
  43. YUV_TO_RGB1_CCIR(cb, cr);
  44. YUV_TO_RGB2_CCIR(r, g, b, y);
  45. *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
  46. }
  47. }
  48. static int decode_run_2bit(GetBitContext *gb, int *color)
  49. {
  50. unsigned int v, t;
  51. v = 0;
  52. for (t = 1; v < t && t <= 0x40; t <<= 2)
  53. v = (v << 4) | get_bits(gb, 4);
  54. *color = v & 3;
  55. if (v < 4) { /* Code for fill rest of line */
  56. return INT_MAX;
  57. }
  58. return v >> 2;
  59. }
  60. static int decode_run_8bit(GetBitContext *gb, int *color)
  61. {
  62. int len;
  63. int has_run = get_bits1(gb);
  64. if (get_bits1(gb))
  65. *color = get_bits(gb, 8);
  66. else
  67. *color = get_bits(gb, 2);
  68. if (has_run) {
  69. if (get_bits1(gb)) {
  70. len = get_bits(gb, 7);
  71. if (len == 0)
  72. len = INT_MAX;
  73. else
  74. len += 9;
  75. } else
  76. len = get_bits(gb, 3) + 2;
  77. } else
  78. len = 1;
  79. return len;
  80. }
  81. static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
  82. const uint8_t *buf, int start, int buf_size, int is_8bit)
  83. {
  84. GetBitContext gb;
  85. int bit_len;
  86. int x, y, len, color;
  87. uint8_t *d;
  88. bit_len = (buf_size - start) * 8;
  89. init_get_bits(&gb, buf + start, bit_len);
  90. x = 0;
  91. y = 0;
  92. d = bitmap;
  93. for(;;) {
  94. if (get_bits_count(&gb) > bit_len)
  95. return -1;
  96. if (is_8bit)
  97. len = decode_run_8bit(&gb, &color);
  98. else
  99. len = decode_run_2bit(&gb, &color);
  100. len = FFMIN(len, w - x);
  101. memset(d + x, color, len);
  102. x += len;
  103. if (x >= w) {
  104. y++;
  105. if (y >= h)
  106. break;
  107. d += linesize;
  108. x = 0;
  109. /* byte align */
  110. align_get_bits(&gb);
  111. }
  112. }
  113. return 0;
  114. }
  115. static void guess_palette(DVDSubContext* ctx,
  116. uint32_t *rgba_palette,
  117. uint8_t *colormap,
  118. uint8_t *alpha,
  119. uint32_t subtitle_color)
  120. {
  121. uint8_t color_used[16] = { 0 };
  122. int nb_opaque_colors, i, level, j, r, g, b;
  123. if (ctx->has_palette) {
  124. for (i = 0; i < 4; i++)
  125. rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
  126. | ((alpha[i] * 17) << 24);
  127. return;
  128. }
  129. for(i = 0; i < 4; i++)
  130. rgba_palette[i] = 0;
  131. nb_opaque_colors = 0;
  132. for(i = 0; i < 4; i++) {
  133. if (alpha[i] != 0 && !color_used[colormap[i]]) {
  134. color_used[colormap[i]] = 1;
  135. nb_opaque_colors++;
  136. }
  137. }
  138. if (nb_opaque_colors == 0)
  139. return;
  140. j = nb_opaque_colors;
  141. memset(color_used, 0, 16);
  142. for(i = 0; i < 4; i++) {
  143. if (alpha[i] != 0) {
  144. if (!color_used[colormap[i]]) {
  145. level = (0xff * j) / nb_opaque_colors;
  146. r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
  147. g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
  148. b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
  149. rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
  150. color_used[colormap[i]] = (i + 1);
  151. j--;
  152. } else {
  153. rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
  154. ((alpha[i] * 17) << 24);
  155. }
  156. }
  157. }
  158. }
  159. #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
  160. static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
  161. const uint8_t *buf, int buf_size)
  162. {
  163. int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
  164. int big_offsets, offset_size, is_8bit = 0;
  165. const uint8_t *yuv_palette = 0;
  166. uint8_t colormap[4] = { 0 }, alpha[256] = { 0 };
  167. int date;
  168. int i;
  169. int is_menu = 0;
  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. av_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. av_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. av_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. av_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. av_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. av_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. av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
  277. goto the_end;
  278. }
  279. }
  280. the_end:
  281. if (offset1 >= 0) {
  282. int w, h;
  283. uint8_t *bitmap;
  284. /* decode the bitmap */
  285. w = x2 - x1 + 1;
  286. if (w < 0)
  287. w = 0;
  288. h = y2 - y1;
  289. if (h < 0)
  290. h = 0;
  291. if (w > 0 && h > 0) {
  292. if (sub_header->rects != NULL) {
  293. for (i = 0; i < sub_header->num_rects; i++) {
  294. av_freep(&sub_header->rects[i]->pict.data[0]);
  295. av_freep(&sub_header->rects[i]->pict.data[1]);
  296. av_freep(&sub_header->rects[i]);
  297. }
  298. av_freep(&sub_header->rects);
  299. sub_header->num_rects = 0;
  300. }
  301. bitmap = av_malloc(w * h);
  302. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  303. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  304. sub_header->num_rects = 1;
  305. sub_header->rects[0]->pict.data[0] = bitmap;
  306. decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  307. buf, offset1, buf_size, is_8bit);
  308. decode_rle(bitmap + w, w * 2, w, h / 2,
  309. buf, offset2, buf_size, is_8bit);
  310. sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  311. if (is_8bit) {
  312. if (yuv_palette == 0)
  313. goto fail;
  314. sub_header->rects[0]->nb_colors = 256;
  315. yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
  316. } else {
  317. sub_header->rects[0]->nb_colors = 4;
  318. guess_palette(ctx,
  319. (uint32_t*)sub_header->rects[0]->pict.data[1],
  320. colormap, alpha, 0xffff00);
  321. }
  322. sub_header->rects[0]->x = x1;
  323. sub_header->rects[0]->y = y1;
  324. sub_header->rects[0]->w = w;
  325. sub_header->rects[0]->h = h;
  326. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  327. sub_header->rects[0]->pict.linesize[0] = w;
  328. }
  329. }
  330. if (next_cmd_pos == cmd_pos)
  331. break;
  332. cmd_pos = next_cmd_pos;
  333. }
  334. if (sub_header->num_rects > 0)
  335. return is_menu;
  336. fail:
  337. if (sub_header->rects != NULL) {
  338. for (i = 0; i < sub_header->num_rects; i++) {
  339. av_freep(&sub_header->rects[i]->pict.data[0]);
  340. av_freep(&sub_header->rects[i]->pict.data[1]);
  341. av_freep(&sub_header->rects[i]);
  342. }
  343. av_freep(&sub_header->rects);
  344. sub_header->num_rects = 0;
  345. }
  346. return -1;
  347. }
  348. static int is_transp(const uint8_t *buf, int pitch, int n,
  349. const uint8_t *transp_color)
  350. {
  351. int i;
  352. for(i = 0; i < n; i++) {
  353. if (!transp_color[*buf])
  354. return 0;
  355. buf += pitch;
  356. }
  357. return 1;
  358. }
  359. /* return 0 if empty rectangle, 1 if non empty */
  360. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  361. {
  362. uint8_t transp_color[256] = { 0 };
  363. int y1, y2, x1, x2, y, w, h, i;
  364. uint8_t *bitmap;
  365. if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  366. return 0;
  367. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  368. if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
  369. transp_color[i] = 1;
  370. }
  371. y1 = 0;
  372. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
  373. 1, s->rects[0]->w, transp_color))
  374. y1++;
  375. if (y1 == s->rects[0]->h) {
  376. av_freep(&s->rects[0]->pict.data[0]);
  377. s->rects[0]->w = s->rects[0]->h = 0;
  378. return 0;
  379. }
  380. y2 = s->rects[0]->h - 1;
  381. while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
  382. s->rects[0]->w, transp_color))
  383. y2--;
  384. x1 = 0;
  385. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
  386. s->rects[0]->h, transp_color))
  387. x1++;
  388. x2 = s->rects[0]->w - 1;
  389. while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
  390. transp_color))
  391. x2--;
  392. w = x2 - x1 + 1;
  393. h = y2 - y1 + 1;
  394. bitmap = av_malloc(w * h);
  395. if (!bitmap)
  396. return 1;
  397. for(y = 0; y < h; y++) {
  398. memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
  399. }
  400. av_freep(&s->rects[0]->pict.data[0]);
  401. s->rects[0]->pict.data[0] = bitmap;
  402. s->rects[0]->pict.linesize[0] = w;
  403. s->rects[0]->w = w;
  404. s->rects[0]->h = h;
  405. s->rects[0]->x += x1;
  406. s->rects[0]->y += y1;
  407. return 1;
  408. }
  409. #ifdef DEBUG
  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 av_cold 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 == ',' || av_isspace(*p))
  482. p++;
  483. }
  484. } else if (!strncmp("size:", cur, 5)) {
  485. int w, h;
  486. if (sscanf(cur + 5, "%dx%d", &w, &h) == 2) {
  487. int ret = ff_set_dimensions(avctx, w, h);
  488. if (ret < 0)
  489. return ret;
  490. }
  491. }
  492. cur += strcspn(cur, "\n\r");
  493. cur += strspn(cur, "\n\r");
  494. }
  495. av_free(data);
  496. return 0;
  497. }
  498. AVCodec ff_dvdsub_decoder = {
  499. .name = "dvdsub",
  500. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  501. .type = AVMEDIA_TYPE_SUBTITLE,
  502. .id = AV_CODEC_ID_DVD_SUBTITLE,
  503. .priv_data_size = sizeof(DVDSubContext),
  504. .init = dvdsub_init,
  505. .decode = dvdsub_decode,
  506. };