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.

803 lines
24KB

  1. /*
  2. * DVD subtitle decoding
  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 "internal.h"
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/colorspace.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/bswap.h"
  30. typedef struct DVDSubContext
  31. {
  32. AVClass *class;
  33. uint32_t palette[16];
  34. char *palette_str;
  35. char *ifo_str;
  36. int has_palette;
  37. uint8_t colormap[4];
  38. uint8_t alpha[256];
  39. uint8_t buf[0x10000];
  40. int buf_size;
  41. int forced_subs_only;
  42. uint8_t used_color[256];
  43. #ifdef DEBUG
  44. int sub_id;
  45. #endif
  46. } DVDSubContext;
  47. static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
  48. {
  49. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  50. uint8_t r, g, b;
  51. int i, y, cb, cr;
  52. int r_add, g_add, b_add;
  53. for (i = num_values; i > 0; i--) {
  54. y = *ycbcr++;
  55. cr = *ycbcr++;
  56. cb = *ycbcr++;
  57. YUV_TO_RGB1_CCIR(cb, cr);
  58. YUV_TO_RGB2_CCIR(r, g, b, y);
  59. *rgba++ = ((unsigned)*alpha++ << 24) | (r << 16) | (g << 8) | b;
  60. }
  61. }
  62. static int decode_run_2bit(GetBitContext *gb, int *color)
  63. {
  64. unsigned int v, t;
  65. v = 0;
  66. for (t = 1; v < t && t <= 0x40; t <<= 2)
  67. v = (v << 4) | get_bits(gb, 4);
  68. *color = v & 3;
  69. if (v < 4) { /* Code for fill rest of line */
  70. return INT_MAX;
  71. }
  72. return v >> 2;
  73. }
  74. static int decode_run_8bit(GetBitContext *gb, int *color)
  75. {
  76. int len;
  77. int has_run = get_bits1(gb);
  78. if (get_bits1(gb))
  79. *color = get_bits(gb, 8);
  80. else
  81. *color = get_bits(gb, 2);
  82. if (has_run) {
  83. if (get_bits1(gb)) {
  84. len = get_bits(gb, 7);
  85. if (len == 0)
  86. len = INT_MAX;
  87. else
  88. len += 9;
  89. } else
  90. len = get_bits(gb, 3) + 2;
  91. } else
  92. len = 1;
  93. return len;
  94. }
  95. static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, uint8_t used_color[256],
  96. const uint8_t *buf, int start, int buf_size, int is_8bit)
  97. {
  98. GetBitContext gb;
  99. int bit_len;
  100. int x, y, len, color;
  101. uint8_t *d;
  102. if (start >= buf_size)
  103. return -1;
  104. if (w <= 0 || h <= 0)
  105. return -1;
  106. bit_len = (buf_size - start) * 8;
  107. init_get_bits(&gb, buf + start, bit_len);
  108. x = 0;
  109. y = 0;
  110. d = bitmap;
  111. for(;;) {
  112. if (get_bits_count(&gb) > bit_len)
  113. return -1;
  114. if (is_8bit)
  115. len = decode_run_8bit(&gb, &color);
  116. else
  117. len = decode_run_2bit(&gb, &color);
  118. len = FFMIN(len, w - x);
  119. memset(d + x, color, len);
  120. used_color[color] = 1;
  121. x += len;
  122. if (x >= w) {
  123. y++;
  124. if (y >= h)
  125. break;
  126. d += linesize;
  127. x = 0;
  128. /* byte align */
  129. align_get_bits(&gb);
  130. }
  131. }
  132. return 0;
  133. }
  134. static void guess_palette(DVDSubContext* ctx,
  135. uint32_t *rgba_palette,
  136. uint32_t subtitle_color)
  137. {
  138. static const uint8_t level_map[4][4] = {
  139. // this configuration (full range, lowest to highest) in tests
  140. // seemed most common, so assume this
  141. {0xff},
  142. {0x00, 0xff},
  143. {0x00, 0x80, 0xff},
  144. {0x00, 0x55, 0xaa, 0xff},
  145. };
  146. uint8_t color_used[16] = { 0 };
  147. int nb_opaque_colors, i, level, j, r, g, b;
  148. uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
  149. if(ctx->has_palette) {
  150. for(i = 0; i < 4; i++)
  151. rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
  152. | ((alpha[i] * 17U) << 24);
  153. return;
  154. }
  155. for(i = 0; i < 4; i++)
  156. rgba_palette[i] = 0;
  157. nb_opaque_colors = 0;
  158. for(i = 0; i < 4; i++) {
  159. if (alpha[i] != 0 && !color_used[colormap[i]]) {
  160. color_used[colormap[i]] = 1;
  161. nb_opaque_colors++;
  162. }
  163. }
  164. if (nb_opaque_colors == 0)
  165. return;
  166. j = 0;
  167. memset(color_used, 0, 16);
  168. for(i = 0; i < 4; i++) {
  169. if (alpha[i] != 0) {
  170. if (!color_used[colormap[i]]) {
  171. level = level_map[nb_opaque_colors - 1][j];
  172. r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
  173. g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
  174. b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
  175. rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17U) << 24);
  176. color_used[colormap[i]] = (i + 1);
  177. j++;
  178. } else {
  179. rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
  180. ((alpha[i] * 17U) << 24);
  181. }
  182. }
  183. }
  184. }
  185. static void reset_rects(AVSubtitle *sub_header)
  186. {
  187. int i;
  188. if (sub_header->rects) {
  189. for (i = 0; i < sub_header->num_rects; i++) {
  190. av_freep(&sub_header->rects[i]->data[0]);
  191. av_freep(&sub_header->rects[i]->data[1]);
  192. av_freep(&sub_header->rects[i]);
  193. }
  194. av_freep(&sub_header->rects);
  195. sub_header->num_rects = 0;
  196. }
  197. }
  198. #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
  199. static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
  200. const uint8_t *buf, int buf_size)
  201. {
  202. int cmd_pos, pos, cmd, x1, y1, x2, y2, next_cmd_pos;
  203. int big_offsets, offset_size, is_8bit = 0;
  204. const uint8_t *yuv_palette = NULL;
  205. uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
  206. int date;
  207. int i;
  208. int is_menu = 0;
  209. uint32_t size;
  210. int64_t offset1, offset2;
  211. if (buf_size < 10)
  212. return -1;
  213. if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
  214. big_offsets = 1;
  215. offset_size = 4;
  216. cmd_pos = 6;
  217. } else {
  218. big_offsets = 0;
  219. offset_size = 2;
  220. cmd_pos = 2;
  221. }
  222. size = READ_OFFSET(buf + (big_offsets ? 2 : 0));
  223. cmd_pos = READ_OFFSET(buf + cmd_pos);
  224. if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
  225. if (cmd_pos > size) {
  226. av_log(ctx, AV_LOG_ERROR, "Discarding invalid packet\n");
  227. return 0;
  228. }
  229. return AVERROR(EAGAIN);
  230. }
  231. while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
  232. date = AV_RB16(buf + cmd_pos);
  233. next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
  234. ff_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
  235. cmd_pos, next_cmd_pos, date);
  236. pos = cmd_pos + 2 + offset_size;
  237. offset1 = -1;
  238. offset2 = -1;
  239. x1 = y1 = x2 = y2 = 0;
  240. while (pos < buf_size) {
  241. cmd = buf[pos++];
  242. ff_dlog(NULL, "cmd=%02x\n", cmd);
  243. switch(cmd) {
  244. case 0x00:
  245. /* menu subpicture */
  246. is_menu = 1;
  247. break;
  248. case 0x01:
  249. /* set start date */
  250. sub_header->start_display_time = (date << 10) / 90;
  251. break;
  252. case 0x02:
  253. /* set end date */
  254. sub_header->end_display_time = (date << 10) / 90;
  255. break;
  256. case 0x03:
  257. /* set colormap */
  258. if ((buf_size - pos) < 2)
  259. goto fail;
  260. colormap[3] = buf[pos] >> 4;
  261. colormap[2] = buf[pos] & 0x0f;
  262. colormap[1] = buf[pos + 1] >> 4;
  263. colormap[0] = buf[pos + 1] & 0x0f;
  264. pos += 2;
  265. break;
  266. case 0x04:
  267. /* set alpha */
  268. if ((buf_size - pos) < 2)
  269. goto fail;
  270. alpha[3] = buf[pos] >> 4;
  271. alpha[2] = buf[pos] & 0x0f;
  272. alpha[1] = buf[pos + 1] >> 4;
  273. alpha[0] = buf[pos + 1] & 0x0f;
  274. pos += 2;
  275. ff_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
  276. break;
  277. case 0x05:
  278. case 0x85:
  279. if ((buf_size - pos) < 6)
  280. goto fail;
  281. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  282. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  283. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  284. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  285. if (cmd & 0x80)
  286. is_8bit = 1;
  287. ff_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
  288. pos += 6;
  289. break;
  290. case 0x06:
  291. if ((buf_size - pos) < 4)
  292. goto fail;
  293. offset1 = AV_RB16(buf + pos);
  294. offset2 = AV_RB16(buf + pos + 2);
  295. ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
  296. pos += 4;
  297. break;
  298. case 0x86:
  299. if ((buf_size - pos) < 8)
  300. goto fail;
  301. offset1 = AV_RB32(buf + pos);
  302. offset2 = AV_RB32(buf + pos + 4);
  303. ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
  304. pos += 8;
  305. break;
  306. case 0x83:
  307. /* HD set palette */
  308. if ((buf_size - pos) < 768)
  309. goto fail;
  310. yuv_palette = buf + pos;
  311. pos += 768;
  312. break;
  313. case 0x84:
  314. /* HD set contrast (alpha) */
  315. if ((buf_size - pos) < 256)
  316. goto fail;
  317. for (i = 0; i < 256; i++)
  318. alpha[i] = 0xFF - buf[pos+i];
  319. pos += 256;
  320. break;
  321. case 0xff:
  322. goto the_end;
  323. default:
  324. ff_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
  325. goto the_end;
  326. }
  327. }
  328. the_end:
  329. if (offset1 >= buf_size || offset2 >= buf_size)
  330. goto fail;
  331. if (offset1 >= 0 && offset2 >= 0) {
  332. int w, h;
  333. uint8_t *bitmap;
  334. /* decode the bitmap */
  335. w = x2 - x1 + 1;
  336. if (w < 0)
  337. w = 0;
  338. h = y2 - y1 + 1;
  339. if (h < 0)
  340. h = 0;
  341. if (w > 0 && h > 1) {
  342. reset_rects(sub_header);
  343. memset(ctx->used_color, 0, sizeof(ctx->used_color));
  344. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  345. if (!sub_header->rects)
  346. goto fail;
  347. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  348. if (!sub_header->rects[0])
  349. goto fail;
  350. sub_header->num_rects = 1;
  351. bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
  352. if (!bitmap)
  353. goto fail;
  354. if (decode_rle(bitmap, w * 2, w, (h + 1) / 2, ctx->used_color,
  355. buf, offset1, buf_size, is_8bit) < 0)
  356. goto fail;
  357. if (decode_rle(bitmap + w, w * 2, w, h / 2, ctx->used_color,
  358. buf, offset2, buf_size, is_8bit) < 0)
  359. goto fail;
  360. sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
  361. if (!sub_header->rects[0]->data[1])
  362. goto fail;
  363. if (is_8bit) {
  364. if (!yuv_palette)
  365. goto fail;
  366. sub_header->rects[0]->nb_colors = 256;
  367. yuv_a_to_rgba(yuv_palette, alpha,
  368. (uint32_t *)sub_header->rects[0]->data[1],
  369. 256);
  370. } else {
  371. sub_header->rects[0]->nb_colors = 4;
  372. guess_palette(ctx, (uint32_t*)sub_header->rects[0]->data[1],
  373. 0xffff00);
  374. }
  375. sub_header->rects[0]->x = x1;
  376. sub_header->rects[0]->y = y1;
  377. sub_header->rects[0]->w = w;
  378. sub_header->rects[0]->h = h;
  379. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  380. sub_header->rects[0]->linesize[0] = w;
  381. sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
  382. #if FF_API_AVPICTURE
  383. FF_DISABLE_DEPRECATION_WARNINGS
  384. for (i = 0; i < 4; i++) {
  385. sub_header->rects[0]->pict.data[i] = sub_header->rects[0]->data[i];
  386. sub_header->rects[0]->pict.linesize[i] = sub_header->rects[0]->linesize[i];
  387. }
  388. FF_ENABLE_DEPRECATION_WARNINGS
  389. #endif
  390. }
  391. }
  392. if (next_cmd_pos < cmd_pos) {
  393. av_log(ctx, AV_LOG_ERROR, "Invalid command offset\n");
  394. break;
  395. }
  396. if (next_cmd_pos == cmd_pos)
  397. break;
  398. cmd_pos = next_cmd_pos;
  399. }
  400. if (sub_header->num_rects > 0)
  401. return is_menu;
  402. fail:
  403. reset_rects(sub_header);
  404. return -1;
  405. }
  406. static int is_transp(const uint8_t *buf, int pitch, int n,
  407. const uint8_t *transp_color)
  408. {
  409. int i;
  410. for(i = 0; i < n; i++) {
  411. if (!transp_color[*buf])
  412. return 0;
  413. buf += pitch;
  414. }
  415. return 1;
  416. }
  417. /* return 0 if empty rectangle, 1 if non empty */
  418. static int find_smallest_bounding_rectangle(DVDSubContext *ctx, AVSubtitle *s)
  419. {
  420. uint8_t transp_color[256] = { 0 };
  421. int y1, y2, x1, x2, y, w, h, i;
  422. uint8_t *bitmap;
  423. int transparent = 1;
  424. if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  425. return 0;
  426. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  427. if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0) {
  428. transp_color[i] = 1;
  429. } else if (ctx->used_color[i])
  430. transparent = 0;
  431. }
  432. if (transparent)
  433. return 0;
  434. y1 = 0;
  435. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
  436. 1, s->rects[0]->w, transp_color))
  437. y1++;
  438. if (y1 == s->rects[0]->h) {
  439. av_freep(&s->rects[0]->data[0]);
  440. s->rects[0]->w = s->rects[0]->h = 0;
  441. return 0;
  442. }
  443. y2 = s->rects[0]->h - 1;
  444. while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
  445. s->rects[0]->w, transp_color))
  446. y2--;
  447. x1 = 0;
  448. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
  449. s->rects[0]->h, transp_color))
  450. x1++;
  451. x2 = s->rects[0]->w - 1;
  452. while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
  453. transp_color))
  454. x2--;
  455. w = x2 - x1 + 1;
  456. h = y2 - y1 + 1;
  457. bitmap = av_malloc(w * h);
  458. if (!bitmap)
  459. return 1;
  460. for(y = 0; y < h; y++) {
  461. memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
  462. }
  463. av_freep(&s->rects[0]->data[0]);
  464. s->rects[0]->data[0] = bitmap;
  465. s->rects[0]->linesize[0] = w;
  466. s->rects[0]->w = w;
  467. s->rects[0]->h = h;
  468. s->rects[0]->x += x1;
  469. s->rects[0]->y += y1;
  470. #if FF_API_AVPICTURE
  471. FF_DISABLE_DEPRECATION_WARNINGS
  472. for (i = 0; i < 4; i++) {
  473. s->rects[0]->pict.data[i] = s->rects[0]->data[i];
  474. s->rects[0]->pict.linesize[i] = s->rects[0]->linesize[i];
  475. }
  476. FF_ENABLE_DEPRECATION_WARNINGS
  477. #endif
  478. return 1;
  479. }
  480. #ifdef DEBUG
  481. #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
  482. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  483. uint32_t *rgba_palette)
  484. {
  485. int x, y, alpha;
  486. uint32_t v;
  487. int back[3] = {0, 255, 0}; /* green background */
  488. FILE *f;
  489. f = fopen(filename, "w");
  490. if (!f) {
  491. perror(filename);
  492. return;
  493. }
  494. fprintf(f, "P6\n"
  495. "%d %d\n"
  496. "%d\n",
  497. w, h, 255);
  498. for(y = 0; y < h; y++) {
  499. for(x = 0; x < w; x++) {
  500. v = rgba_palette[bitmap[y * w + x]];
  501. alpha = v >> 24;
  502. putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
  503. putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
  504. putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
  505. }
  506. }
  507. fclose(f);
  508. }
  509. #endif
  510. static int append_to_cached_buf(AVCodecContext *avctx,
  511. const uint8_t *buf, int buf_size)
  512. {
  513. DVDSubContext *ctx = avctx->priv_data;
  514. av_assert0(buf_size >= 0 && ctx->buf_size <= sizeof(ctx->buf));
  515. if (buf_size >= sizeof(ctx->buf) - ctx->buf_size) {
  516. av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
  517. "too large SPU packets aborted.\n");
  518. ctx->buf_size = 0;
  519. return AVERROR_INVALIDDATA;
  520. }
  521. memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
  522. ctx->buf_size += buf_size;
  523. return 0;
  524. }
  525. static int dvdsub_decode(AVCodecContext *avctx,
  526. void *data, int *data_size,
  527. AVPacket *avpkt)
  528. {
  529. DVDSubContext *ctx = avctx->priv_data;
  530. const uint8_t *buf = avpkt->data;
  531. int buf_size = avpkt->size;
  532. AVSubtitle *sub = data;
  533. int appended = 0;
  534. int is_menu;
  535. if (ctx->buf_size) {
  536. int ret = append_to_cached_buf(avctx, buf, buf_size);
  537. if (ret < 0) {
  538. *data_size = 0;
  539. return ret;
  540. }
  541. buf = ctx->buf;
  542. buf_size = ctx->buf_size;
  543. appended = 1;
  544. }
  545. is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
  546. if (is_menu == AVERROR(EAGAIN)) {
  547. *data_size = 0;
  548. return appended ? 0 : append_to_cached_buf(avctx, buf, buf_size);
  549. }
  550. if (is_menu < 0) {
  551. no_subtitle:
  552. reset_rects(sub);
  553. *data_size = 0;
  554. return buf_size;
  555. }
  556. if (!is_menu && find_smallest_bounding_rectangle(ctx, sub) == 0)
  557. goto no_subtitle;
  558. if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
  559. goto no_subtitle;
  560. #if defined(DEBUG)
  561. {
  562. char ppm_name[32];
  563. snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
  564. ff_dlog(NULL, "start=%d ms end =%d ms\n",
  565. sub->start_display_time,
  566. sub->end_display_time);
  567. ppm_save(ppm_name, sub->rects[0]->data[0],
  568. sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->data[1]);
  569. }
  570. #endif
  571. ctx->buf_size = 0;
  572. *data_size = 1;
  573. return buf_size;
  574. }
  575. static void parse_palette(DVDSubContext *ctx, char *p)
  576. {
  577. int i;
  578. ctx->has_palette = 1;
  579. for(i=0;i<16;i++) {
  580. ctx->palette[i] = strtoul(p, &p, 16);
  581. while(*p == ',' || av_isspace(*p))
  582. p++;
  583. }
  584. }
  585. static int parse_ifo_palette(DVDSubContext *ctx, char *p)
  586. {
  587. FILE *ifo;
  588. char ifostr[12];
  589. uint32_t sp_pgci, pgci, off_pgc, pgc;
  590. uint8_t r, g, b, yuv[65], *buf;
  591. int i, y, cb, cr, r_add, g_add, b_add;
  592. int ret = 0;
  593. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  594. ctx->has_palette = 0;
  595. if ((ifo = fopen(p, "r")) == NULL) {
  596. av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno)));
  597. return AVERROR_EOF;
  598. }
  599. if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
  600. av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
  601. ret = AVERROR_INVALIDDATA;
  602. goto end;
  603. }
  604. if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
  605. ret = AVERROR(errno);
  606. goto end;
  607. }
  608. if (fread(&sp_pgci, 4, 1, ifo) == 1) {
  609. pgci = av_be2ne32(sp_pgci) * 2048;
  610. if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
  611. ret = AVERROR(errno);
  612. goto end;
  613. }
  614. if (fread(&off_pgc, 4, 1, ifo) == 1) {
  615. pgc = pgci + av_be2ne32(off_pgc);
  616. if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
  617. ret = AVERROR(errno);
  618. goto end;
  619. }
  620. if (fread(yuv, 64, 1, ifo) == 1) {
  621. buf = yuv;
  622. for(i=0; i<16; i++) {
  623. y = *++buf;
  624. cr = *++buf;
  625. cb = *++buf;
  626. YUV_TO_RGB1_CCIR(cb, cr);
  627. YUV_TO_RGB2_CCIR(r, g, b, y);
  628. ctx->palette[i] = (r << 16) + (g << 8) + b;
  629. buf++;
  630. }
  631. ctx->has_palette = 1;
  632. }
  633. }
  634. }
  635. if (ctx->has_palette == 0) {
  636. av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
  637. ret = AVERROR_INVALIDDATA;
  638. }
  639. end:
  640. fclose(ifo);
  641. return ret;
  642. }
  643. static int dvdsub_parse_extradata(AVCodecContext *avctx)
  644. {
  645. DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
  646. char *dataorig, *data;
  647. int ret = 1;
  648. if (!avctx->extradata || !avctx->extradata_size)
  649. return 1;
  650. dataorig = data = av_malloc(avctx->extradata_size+1);
  651. if (!data)
  652. return AVERROR(ENOMEM);
  653. memcpy(data, avctx->extradata, avctx->extradata_size);
  654. data[avctx->extradata_size] = '\0';
  655. for(;;) {
  656. int pos = strcspn(data, "\n\r");
  657. if (pos==0 && *data==0)
  658. break;
  659. if (strncmp("palette:", data, 8) == 0) {
  660. parse_palette(ctx, data + 8);
  661. } else if (strncmp("size:", data, 5) == 0) {
  662. int w, h;
  663. if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
  664. ret = ff_set_dimensions(avctx, w, h);
  665. if (ret < 0)
  666. goto fail;
  667. }
  668. }
  669. data += pos;
  670. data += strspn(data, "\n\r");
  671. }
  672. fail:
  673. av_free(dataorig);
  674. return ret;
  675. }
  676. static av_cold int dvdsub_init(AVCodecContext *avctx)
  677. {
  678. DVDSubContext *ctx = avctx->priv_data;
  679. int ret;
  680. if ((ret = dvdsub_parse_extradata(avctx)) < 0)
  681. return ret;
  682. if (ctx->ifo_str)
  683. parse_ifo_palette(ctx, ctx->ifo_str);
  684. if (ctx->palette_str)
  685. parse_palette(ctx, ctx->palette_str);
  686. if (ctx->has_palette) {
  687. int i;
  688. av_log(avctx, AV_LOG_DEBUG, "palette:");
  689. for(i=0;i<16;i++)
  690. av_log(avctx, AV_LOG_DEBUG, " 0x%06"PRIx32, ctx->palette[i]);
  691. av_log(avctx, AV_LOG_DEBUG, "\n");
  692. }
  693. return 1;
  694. }
  695. static void dvdsub_flush(AVCodecContext *avctx)
  696. {
  697. DVDSubContext *ctx = avctx->priv_data;
  698. ctx->buf_size = 0;
  699. }
  700. static av_cold int dvdsub_close(AVCodecContext *avctx)
  701. {
  702. dvdsub_flush(avctx);
  703. return 0;
  704. }
  705. #define OFFSET(field) offsetof(DVDSubContext, field)
  706. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  707. static const AVOption options[] = {
  708. { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
  709. { "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
  710. { "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD},
  711. { NULL }
  712. };
  713. static const AVClass dvdsub_class = {
  714. .class_name = "dvdsubdec",
  715. .item_name = av_default_item_name,
  716. .option = options,
  717. .version = LIBAVUTIL_VERSION_INT,
  718. };
  719. AVCodec ff_dvdsub_decoder = {
  720. .name = "dvdsub",
  721. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  722. .type = AVMEDIA_TYPE_SUBTITLE,
  723. .id = AV_CODEC_ID_DVD_SUBTITLE,
  724. .priv_data_size = sizeof(DVDSubContext),
  725. .init = dvdsub_init,
  726. .decode = dvdsub_decode,
  727. .flush = dvdsub_flush,
  728. .close = dvdsub_close,
  729. .priv_class = &dvdsub_class,
  730. };