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.

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