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.

494 lines
15KB

  1. /*
  2. * DVD subtitle decoding
  3. * Copyright (c) 2005 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. uint8_t color_used[16] = { 0 };
  114. int nb_opaque_colors, i, level, j, r, g, b;
  115. for(i = 0; i < 4; i++)
  116. rgba_palette[i] = 0;
  117. nb_opaque_colors = 0;
  118. for(i = 0; i < 4; i++) {
  119. if (alpha[i] != 0 && !color_used[colormap[i]]) {
  120. color_used[colormap[i]] = 1;
  121. nb_opaque_colors++;
  122. }
  123. }
  124. if (nb_opaque_colors == 0)
  125. return;
  126. j = nb_opaque_colors;
  127. memset(color_used, 0, 16);
  128. for(i = 0; i < 4; i++) {
  129. if (alpha[i] != 0) {
  130. if (!color_used[colormap[i]]) {
  131. level = (0xff * j) / nb_opaque_colors;
  132. r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
  133. g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
  134. b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
  135. rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
  136. color_used[colormap[i]] = (i + 1);
  137. j--;
  138. } else {
  139. rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
  140. ((alpha[i] * 17) << 24);
  141. }
  142. }
  143. }
  144. }
  145. #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
  146. static int decode_dvd_subtitles(AVSubtitle *sub_header,
  147. const uint8_t *buf, int buf_size)
  148. {
  149. int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
  150. int big_offsets, offset_size, is_8bit = 0;
  151. const uint8_t *yuv_palette = 0;
  152. uint8_t colormap[4], alpha[256];
  153. int date;
  154. int i;
  155. int is_menu = 0;
  156. if (buf_size < 10)
  157. return -1;
  158. memset(sub_header, 0, sizeof(*sub_header));
  159. if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
  160. big_offsets = 1;
  161. offset_size = 4;
  162. cmd_pos = 6;
  163. } else {
  164. big_offsets = 0;
  165. offset_size = 2;
  166. cmd_pos = 2;
  167. }
  168. cmd_pos = READ_OFFSET(buf + cmd_pos);
  169. while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
  170. date = AV_RB16(buf + cmd_pos);
  171. next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
  172. av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
  173. cmd_pos, next_cmd_pos, date);
  174. pos = cmd_pos + 2 + offset_size;
  175. offset1 = -1;
  176. offset2 = -1;
  177. x1 = y1 = x2 = y2 = 0;
  178. while (pos < buf_size) {
  179. cmd = buf[pos++];
  180. av_dlog(NULL, "cmd=%02x\n", cmd);
  181. switch(cmd) {
  182. case 0x00:
  183. /* menu subpicture */
  184. is_menu = 1;
  185. break;
  186. case 0x01:
  187. /* set start date */
  188. sub_header->start_display_time = (date << 10) / 90;
  189. break;
  190. case 0x02:
  191. /* set end date */
  192. sub_header->end_display_time = (date << 10) / 90;
  193. break;
  194. case 0x03:
  195. /* set colormap */
  196. if ((buf_size - pos) < 2)
  197. goto fail;
  198. colormap[3] = buf[pos] >> 4;
  199. colormap[2] = buf[pos] & 0x0f;
  200. colormap[1] = buf[pos + 1] >> 4;
  201. colormap[0] = buf[pos + 1] & 0x0f;
  202. pos += 2;
  203. break;
  204. case 0x04:
  205. /* set alpha */
  206. if ((buf_size - pos) < 2)
  207. goto fail;
  208. alpha[3] = buf[pos] >> 4;
  209. alpha[2] = buf[pos] & 0x0f;
  210. alpha[1] = buf[pos + 1] >> 4;
  211. alpha[0] = buf[pos + 1] & 0x0f;
  212. pos += 2;
  213. av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
  214. break;
  215. case 0x05:
  216. case 0x85:
  217. if ((buf_size - pos) < 6)
  218. goto fail;
  219. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  220. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  221. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  222. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  223. if (cmd & 0x80)
  224. is_8bit = 1;
  225. av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
  226. pos += 6;
  227. break;
  228. case 0x06:
  229. if ((buf_size - pos) < 4)
  230. goto fail;
  231. offset1 = AV_RB16(buf + pos);
  232. offset2 = AV_RB16(buf + pos + 2);
  233. av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  234. pos += 4;
  235. break;
  236. case 0x86:
  237. if ((buf_size - pos) < 8)
  238. goto fail;
  239. offset1 = AV_RB32(buf + pos);
  240. offset2 = AV_RB32(buf + pos + 4);
  241. av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  242. pos += 8;
  243. break;
  244. case 0x83:
  245. /* HD set palette */
  246. if ((buf_size - pos) < 768)
  247. goto fail;
  248. yuv_palette = buf + pos;
  249. pos += 768;
  250. break;
  251. case 0x84:
  252. /* HD set contrast (alpha) */
  253. if ((buf_size - pos) < 256)
  254. goto fail;
  255. for (i = 0; i < 256; i++)
  256. alpha[i] = 0xFF - buf[pos+i];
  257. pos += 256;
  258. break;
  259. case 0xff:
  260. goto the_end;
  261. default:
  262. av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
  263. goto the_end;
  264. }
  265. }
  266. the_end:
  267. if (offset1 >= 0) {
  268. int w, h;
  269. uint8_t *bitmap;
  270. /* decode the bitmap */
  271. w = x2 - x1 + 1;
  272. if (w < 0)
  273. w = 0;
  274. h = y2 - y1;
  275. if (h < 0)
  276. h = 0;
  277. if (w > 0 && h > 0) {
  278. if (sub_header->rects != NULL) {
  279. for (i = 0; i < sub_header->num_rects; i++) {
  280. av_freep(&sub_header->rects[i]->pict.data[0]);
  281. av_freep(&sub_header->rects[i]->pict.data[1]);
  282. av_freep(&sub_header->rects[i]);
  283. }
  284. av_freep(&sub_header->rects);
  285. sub_header->num_rects = 0;
  286. }
  287. bitmap = av_malloc(w * h);
  288. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  289. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  290. sub_header->num_rects = 1;
  291. sub_header->rects[0]->pict.data[0] = bitmap;
  292. decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  293. buf, offset1, buf_size, is_8bit);
  294. decode_rle(bitmap + w, w * 2, w, h / 2,
  295. buf, offset2, buf_size, is_8bit);
  296. sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  297. if (is_8bit) {
  298. if (yuv_palette == 0)
  299. goto fail;
  300. sub_header->rects[0]->nb_colors = 256;
  301. yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
  302. } else {
  303. sub_header->rects[0]->nb_colors = 4;
  304. guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1],
  305. colormap, alpha, 0xffff00);
  306. }
  307. sub_header->rects[0]->x = x1;
  308. sub_header->rects[0]->y = y1;
  309. sub_header->rects[0]->w = w;
  310. sub_header->rects[0]->h = h;
  311. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  312. sub_header->rects[0]->pict.linesize[0] = w;
  313. }
  314. }
  315. if (next_cmd_pos == cmd_pos)
  316. break;
  317. cmd_pos = next_cmd_pos;
  318. }
  319. if (sub_header->num_rects > 0)
  320. return is_menu;
  321. fail:
  322. if (sub_header->rects != NULL) {
  323. for (i = 0; i < sub_header->num_rects; i++) {
  324. av_freep(&sub_header->rects[i]->pict.data[0]);
  325. av_freep(&sub_header->rects[i]->pict.data[1]);
  326. av_freep(&sub_header->rects[i]);
  327. }
  328. av_freep(&sub_header->rects);
  329. sub_header->num_rects = 0;
  330. }
  331. return -1;
  332. }
  333. static int is_transp(const uint8_t *buf, int pitch, int n,
  334. const uint8_t *transp_color)
  335. {
  336. int i;
  337. for(i = 0; i < n; i++) {
  338. if (!transp_color[*buf])
  339. return 0;
  340. buf += pitch;
  341. }
  342. return 1;
  343. }
  344. /* return 0 if empty rectangle, 1 if non empty */
  345. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  346. {
  347. uint8_t transp_color[256] = { 0 };
  348. int y1, y2, x1, x2, y, w, h, i;
  349. uint8_t *bitmap;
  350. if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  351. return 0;
  352. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  353. if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
  354. transp_color[i] = 1;
  355. }
  356. y1 = 0;
  357. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
  358. 1, s->rects[0]->w, transp_color))
  359. y1++;
  360. if (y1 == s->rects[0]->h) {
  361. av_freep(&s->rects[0]->pict.data[0]);
  362. s->rects[0]->w = s->rects[0]->h = 0;
  363. return 0;
  364. }
  365. y2 = s->rects[0]->h - 1;
  366. while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
  367. s->rects[0]->w, transp_color))
  368. y2--;
  369. x1 = 0;
  370. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
  371. s->rects[0]->h, transp_color))
  372. x1++;
  373. x2 = s->rects[0]->w - 1;
  374. while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
  375. transp_color))
  376. x2--;
  377. w = x2 - x1 + 1;
  378. h = y2 - y1 + 1;
  379. bitmap = av_malloc(w * h);
  380. if (!bitmap)
  381. return 1;
  382. for(y = 0; y < h; y++) {
  383. memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
  384. }
  385. av_freep(&s->rects[0]->pict.data[0]);
  386. s->rects[0]->pict.data[0] = bitmap;
  387. s->rects[0]->pict.linesize[0] = w;
  388. s->rects[0]->w = w;
  389. s->rects[0]->h = h;
  390. s->rects[0]->x += x1;
  391. s->rects[0]->y += y1;
  392. return 1;
  393. }
  394. #ifdef DEBUG
  395. #undef fprintf
  396. #undef perror
  397. #undef exit
  398. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  399. uint32_t *rgba_palette)
  400. {
  401. int x, y, v;
  402. FILE *f;
  403. f = fopen(filename, "w");
  404. if (!f) {
  405. perror(filename);
  406. exit(1);
  407. }
  408. fprintf(f, "P6\n"
  409. "%d %d\n"
  410. "%d\n",
  411. w, h, 255);
  412. for(y = 0; y < h; y++) {
  413. for(x = 0; x < w; x++) {
  414. v = rgba_palette[bitmap[y * w + x]];
  415. putc((v >> 16) & 0xff, f);
  416. putc((v >> 8) & 0xff, f);
  417. putc((v >> 0) & 0xff, f);
  418. }
  419. }
  420. fclose(f);
  421. }
  422. #endif
  423. static int dvdsub_decode(AVCodecContext *avctx,
  424. void *data, int *data_size,
  425. AVPacket *avpkt)
  426. {
  427. const uint8_t *buf = avpkt->data;
  428. int buf_size = avpkt->size;
  429. AVSubtitle *sub = data;
  430. int is_menu;
  431. is_menu = decode_dvd_subtitles(sub, buf, buf_size);
  432. if (is_menu < 0) {
  433. no_subtitle:
  434. *data_size = 0;
  435. return buf_size;
  436. }
  437. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  438. goto no_subtitle;
  439. #if defined(DEBUG)
  440. av_dlog(NULL, "start=%d ms end =%d ms\n",
  441. sub->start_display_time,
  442. sub->end_display_time);
  443. ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
  444. sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
  445. #endif
  446. *data_size = 1;
  447. return buf_size;
  448. }
  449. AVCodec ff_dvdsub_decoder = {
  450. .name = "dvdsub",
  451. .type = AVMEDIA_TYPE_SUBTITLE,
  452. .id = CODEC_ID_DVD_SUBTITLE,
  453. .decode = dvdsub_decode,
  454. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  455. };