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.

569 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. }
  334. }
  335. if (next_cmd_pos < cmd_pos) {
  336. av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
  337. break;
  338. }
  339. if (next_cmd_pos == cmd_pos)
  340. break;
  341. cmd_pos = next_cmd_pos;
  342. }
  343. if (sub_header->num_rects > 0)
  344. return is_menu;
  345. fail:
  346. if (sub_header->rects != NULL) {
  347. for (i = 0; i < sub_header->num_rects; i++) {
  348. av_freep(&sub_header->rects[i]->pict.data[0]);
  349. av_freep(&sub_header->rects[i]->pict.data[1]);
  350. av_freep(&sub_header->rects[i]);
  351. }
  352. av_freep(&sub_header->rects);
  353. sub_header->num_rects = 0;
  354. }
  355. return -1;
  356. }
  357. static int is_transp(const uint8_t *buf, int pitch, int n,
  358. const uint8_t *transp_color)
  359. {
  360. int i;
  361. for(i = 0; i < n; i++) {
  362. if (!transp_color[*buf])
  363. return 0;
  364. buf += pitch;
  365. }
  366. return 1;
  367. }
  368. /* return 0 if empty rectangle, 1 if non empty */
  369. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  370. {
  371. uint8_t transp_color[256] = { 0 };
  372. int y1, y2, x1, x2, y, w, h, i;
  373. uint8_t *bitmap;
  374. if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  375. return 0;
  376. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  377. if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
  378. transp_color[i] = 1;
  379. }
  380. y1 = 0;
  381. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
  382. 1, s->rects[0]->w, transp_color))
  383. y1++;
  384. if (y1 == s->rects[0]->h) {
  385. av_freep(&s->rects[0]->pict.data[0]);
  386. s->rects[0]->w = s->rects[0]->h = 0;
  387. return 0;
  388. }
  389. y2 = s->rects[0]->h - 1;
  390. while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
  391. s->rects[0]->w, transp_color))
  392. y2--;
  393. x1 = 0;
  394. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
  395. s->rects[0]->h, transp_color))
  396. x1++;
  397. x2 = s->rects[0]->w - 1;
  398. while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
  399. transp_color))
  400. x2--;
  401. w = x2 - x1 + 1;
  402. h = y2 - y1 + 1;
  403. bitmap = av_malloc(w * h);
  404. if (!bitmap)
  405. return 1;
  406. for(y = 0; y < h; y++) {
  407. memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
  408. }
  409. av_freep(&s->rects[0]->pict.data[0]);
  410. s->rects[0]->pict.data[0] = bitmap;
  411. s->rects[0]->pict.linesize[0] = w;
  412. s->rects[0]->w = w;
  413. s->rects[0]->h = h;
  414. s->rects[0]->x += x1;
  415. s->rects[0]->y += y1;
  416. return 1;
  417. }
  418. #ifdef DEBUG
  419. #undef fprintf
  420. #undef perror
  421. #undef exit
  422. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  423. uint32_t *rgba_palette)
  424. {
  425. int x, y, v;
  426. FILE *f;
  427. f = fopen(filename, "w");
  428. if (!f) {
  429. perror(filename);
  430. exit(1);
  431. }
  432. fprintf(f, "P6\n"
  433. "%d %d\n"
  434. "%d\n",
  435. w, h, 255);
  436. for(y = 0; y < h; y++) {
  437. for(x = 0; x < w; x++) {
  438. v = rgba_palette[bitmap[y * w + x]];
  439. putc((v >> 16) & 0xff, f);
  440. putc((v >> 8) & 0xff, f);
  441. putc((v >> 0) & 0xff, f);
  442. }
  443. }
  444. fclose(f);
  445. }
  446. #endif
  447. static int dvdsub_decode(AVCodecContext *avctx,
  448. void *data, int *data_size,
  449. AVPacket *avpkt)
  450. {
  451. DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
  452. const uint8_t *buf = avpkt->data;
  453. int buf_size = avpkt->size;
  454. AVSubtitle *sub = data;
  455. int is_menu;
  456. is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
  457. if (is_menu < 0) {
  458. no_subtitle:
  459. *data_size = 0;
  460. return buf_size;
  461. }
  462. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  463. goto no_subtitle;
  464. #if defined(DEBUG)
  465. av_dlog(NULL, "start=%d ms end =%d ms\n",
  466. sub->start_display_time,
  467. sub->end_display_time);
  468. ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
  469. sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
  470. #endif
  471. *data_size = 1;
  472. return buf_size;
  473. }
  474. static int dvdsub_init(AVCodecContext *avctx)
  475. {
  476. DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
  477. char *dataorig, *data;
  478. if (!avctx->extradata || !avctx->extradata_size)
  479. return 1;
  480. dataorig = data = av_malloc(avctx->extradata_size+1);
  481. if (!data)
  482. return AVERROR(ENOMEM);
  483. memcpy(data, avctx->extradata, avctx->extradata_size);
  484. data[avctx->extradata_size] = '\0';
  485. for(;;) {
  486. int pos = strcspn(data, "\n\r");
  487. if (pos==0 && *data==0)
  488. break;
  489. if (strncmp("palette:", data, 8) == 0) {
  490. int i;
  491. char *p = data+8;
  492. ctx->has_palette = 1;
  493. for(i=0;i<16;i++) {
  494. ctx->palette[i] = strtoul(p, &p, 16);
  495. while(*p == ',' || isspace(*p))
  496. p++;
  497. }
  498. }
  499. data += pos;
  500. data += strspn(data, "\n\r");
  501. }
  502. if (ctx->has_palette) {
  503. int i;
  504. av_log(avctx, AV_LOG_DEBUG, "palette:");
  505. for(i=0;i<16;i++)
  506. av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
  507. av_log(avctx, AV_LOG_DEBUG, "\n");
  508. }
  509. av_free(dataorig);
  510. return 1;
  511. }
  512. AVCodec ff_dvdsub_decoder = {
  513. .name = "dvdsub",
  514. .type = AVMEDIA_TYPE_SUBTITLE,
  515. .id = CODEC_ID_DVD_SUBTITLE,
  516. .priv_data_size = sizeof(DVDSubContext),
  517. .init = dvdsub_init,
  518. .decode = dvdsub_decode,
  519. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  520. };