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.

571 lines
17KB

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