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.

507 lines
16KB

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