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