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.

611 lines
18KB

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