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.

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