dvdsubdec.c 15 KB

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