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.

570 lines
17KB

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