dvdsubdec.c 16 KB

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