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.

791 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][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] * 17) << 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] * 17) << 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, offset1, offset2, 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. 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%04x offset2=0x%04x\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%04x offset2=0x%04x\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 >= 0) {
  327. int w, h;
  328. uint8_t *bitmap;
  329. /* decode the bitmap */
  330. w = x2 - x1 + 1;
  331. if (w < 0)
  332. w = 0;
  333. h = y2 - y1 + 1;
  334. if (h < 0)
  335. h = 0;
  336. if (w > 0 && h > 0) {
  337. reset_rects(sub_header);
  338. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  339. if (!sub_header->rects)
  340. goto fail;
  341. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  342. if (!sub_header->rects[0])
  343. goto fail;
  344. sub_header->num_rects = 1;
  345. bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
  346. if (!bitmap)
  347. goto fail;
  348. if (decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  349. buf, offset1, buf_size, is_8bit) < 0)
  350. goto fail;
  351. if (decode_rle(bitmap + w, w * 2, w, h / 2,
  352. buf, offset2, buf_size, is_8bit) < 0)
  353. goto fail;
  354. sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
  355. if (!sub_header->rects[0]->data[1])
  356. goto fail;
  357. if (is_8bit) {
  358. if (!yuv_palette)
  359. goto fail;
  360. sub_header->rects[0]->nb_colors = 256;
  361. yuv_a_to_rgba(yuv_palette, alpha,
  362. (uint32_t *)sub_header->rects[0]->data[1],
  363. 256);
  364. } else {
  365. sub_header->rects[0]->nb_colors = 4;
  366. guess_palette(ctx, (uint32_t*)sub_header->rects[0]->data[1],
  367. 0xffff00);
  368. }
  369. sub_header->rects[0]->x = x1;
  370. sub_header->rects[0]->y = y1;
  371. sub_header->rects[0]->w = w;
  372. sub_header->rects[0]->h = h;
  373. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  374. sub_header->rects[0]->linesize[0] = w;
  375. sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
  376. #if FF_API_AVPICTURE
  377. FF_DISABLE_DEPRECATION_WARNINGS
  378. for (i = 0; i < 4; i++) {
  379. sub_header->rects[0]->pict.data[i] = sub_header->rects[0]->data[i];
  380. sub_header->rects[0]->pict.linesize[i] = sub_header->rects[0]->linesize[i];
  381. }
  382. FF_ENABLE_DEPRECATION_WARNINGS
  383. #endif
  384. }
  385. }
  386. if (next_cmd_pos < cmd_pos) {
  387. av_log(ctx, AV_LOG_ERROR, "Invalid command offset\n");
  388. break;
  389. }
  390. if (next_cmd_pos == cmd_pos)
  391. break;
  392. cmd_pos = next_cmd_pos;
  393. }
  394. if (sub_header->num_rects > 0)
  395. return is_menu;
  396. fail:
  397. reset_rects(sub_header);
  398. return -1;
  399. }
  400. static int is_transp(const uint8_t *buf, int pitch, int n,
  401. const uint8_t *transp_color)
  402. {
  403. int i;
  404. for(i = 0; i < n; i++) {
  405. if (!transp_color[*buf])
  406. return 0;
  407. buf += pitch;
  408. }
  409. return 1;
  410. }
  411. /* return 0 if empty rectangle, 1 if non empty */
  412. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  413. {
  414. uint8_t transp_color[256] = { 0 };
  415. int y1, y2, x1, x2, y, w, h, i;
  416. uint8_t *bitmap;
  417. if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  418. return 0;
  419. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  420. if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0)
  421. transp_color[i] = 1;
  422. }
  423. y1 = 0;
  424. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
  425. 1, s->rects[0]->w, transp_color))
  426. y1++;
  427. if (y1 == s->rects[0]->h) {
  428. av_freep(&s->rects[0]->data[0]);
  429. s->rects[0]->w = s->rects[0]->h = 0;
  430. return 0;
  431. }
  432. y2 = s->rects[0]->h - 1;
  433. while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
  434. s->rects[0]->w, transp_color))
  435. y2--;
  436. x1 = 0;
  437. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
  438. s->rects[0]->h, transp_color))
  439. x1++;
  440. x2 = s->rects[0]->w - 1;
  441. while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
  442. transp_color))
  443. x2--;
  444. w = x2 - x1 + 1;
  445. h = y2 - y1 + 1;
  446. bitmap = av_malloc(w * h);
  447. if (!bitmap)
  448. return 1;
  449. for(y = 0; y < h; y++) {
  450. memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
  451. }
  452. av_freep(&s->rects[0]->data[0]);
  453. s->rects[0]->data[0] = bitmap;
  454. s->rects[0]->linesize[0] = w;
  455. s->rects[0]->w = w;
  456. s->rects[0]->h = h;
  457. s->rects[0]->x += x1;
  458. s->rects[0]->y += y1;
  459. #if FF_API_AVPICTURE
  460. FF_DISABLE_DEPRECATION_WARNINGS
  461. for (i = 0; i < 4; i++) {
  462. s->rects[0]->pict.data[i] = s->rects[0]->data[i];
  463. s->rects[0]->pict.linesize[i] = s->rects[0]->linesize[i];
  464. }
  465. FF_ENABLE_DEPRECATION_WARNINGS
  466. #endif
  467. return 1;
  468. }
  469. #ifdef DEBUG
  470. #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
  471. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  472. uint32_t *rgba_palette)
  473. {
  474. int x, y, alpha;
  475. uint32_t v;
  476. int back[3] = {0, 255, 0}; /* green background */
  477. FILE *f;
  478. f = fopen(filename, "w");
  479. if (!f) {
  480. perror(filename);
  481. return;
  482. }
  483. fprintf(f, "P6\n"
  484. "%d %d\n"
  485. "%d\n",
  486. w, h, 255);
  487. for(y = 0; y < h; y++) {
  488. for(x = 0; x < w; x++) {
  489. v = rgba_palette[bitmap[y * w + x]];
  490. alpha = v >> 24;
  491. putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
  492. putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
  493. putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
  494. }
  495. }
  496. fclose(f);
  497. }
  498. #endif
  499. static int append_to_cached_buf(AVCodecContext *avctx,
  500. const uint8_t *buf, int buf_size)
  501. {
  502. DVDSubContext *ctx = avctx->priv_data;
  503. if (ctx->buf_size >= sizeof(ctx->buf) - buf_size) {
  504. av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
  505. "too large SPU packets aborted.\n");
  506. ctx->buf_size = 0;
  507. return AVERROR_INVALIDDATA;
  508. }
  509. memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
  510. ctx->buf_size += buf_size;
  511. return 0;
  512. }
  513. static int dvdsub_decode(AVCodecContext *avctx,
  514. void *data, int *data_size,
  515. AVPacket *avpkt)
  516. {
  517. DVDSubContext *ctx = avctx->priv_data;
  518. const uint8_t *buf = avpkt->data;
  519. int buf_size = avpkt->size;
  520. AVSubtitle *sub = data;
  521. int appended = 0;
  522. int is_menu;
  523. if (ctx->buf_size) {
  524. int ret = append_to_cached_buf(avctx, buf, buf_size);
  525. if (ret < 0) {
  526. *data_size = 0;
  527. return ret;
  528. }
  529. buf = ctx->buf;
  530. buf_size = ctx->buf_size;
  531. appended = 1;
  532. }
  533. is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
  534. if (is_menu == AVERROR(EAGAIN)) {
  535. *data_size = 0;
  536. return appended ? 0 : append_to_cached_buf(avctx, buf, buf_size);
  537. }
  538. if (is_menu < 0) {
  539. no_subtitle:
  540. reset_rects(sub);
  541. *data_size = 0;
  542. return buf_size;
  543. }
  544. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  545. goto no_subtitle;
  546. if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
  547. goto no_subtitle;
  548. #if defined(DEBUG)
  549. {
  550. char ppm_name[32];
  551. snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
  552. ff_dlog(NULL, "start=%d ms end =%d ms\n",
  553. sub->start_display_time,
  554. sub->end_display_time);
  555. ppm_save(ppm_name, sub->rects[0]->data[0],
  556. sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->data[1]);
  557. }
  558. #endif
  559. ctx->buf_size = 0;
  560. *data_size = 1;
  561. return buf_size;
  562. }
  563. static void parse_palette(DVDSubContext *ctx, char *p)
  564. {
  565. int i;
  566. ctx->has_palette = 1;
  567. for(i=0;i<16;i++) {
  568. ctx->palette[i] = strtoul(p, &p, 16);
  569. while(*p == ',' || av_isspace(*p))
  570. p++;
  571. }
  572. }
  573. static int parse_ifo_palette(DVDSubContext *ctx, char *p)
  574. {
  575. FILE *ifo;
  576. char ifostr[12];
  577. uint32_t sp_pgci, pgci, off_pgc, pgc;
  578. uint8_t r, g, b, yuv[65], *buf;
  579. int i, y, cb, cr, r_add, g_add, b_add;
  580. int ret = 0;
  581. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  582. ctx->has_palette = 0;
  583. if ((ifo = fopen(p, "r")) == NULL) {
  584. av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno)));
  585. return AVERROR_EOF;
  586. }
  587. if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
  588. av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
  589. ret = AVERROR_INVALIDDATA;
  590. goto end;
  591. }
  592. if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
  593. ret = AVERROR(errno);
  594. goto end;
  595. }
  596. if (fread(&sp_pgci, 4, 1, ifo) == 1) {
  597. pgci = av_be2ne32(sp_pgci) * 2048;
  598. if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
  599. ret = AVERROR(errno);
  600. goto end;
  601. }
  602. if (fread(&off_pgc, 4, 1, ifo) == 1) {
  603. pgc = pgci + av_be2ne32(off_pgc);
  604. if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
  605. ret = AVERROR(errno);
  606. goto end;
  607. }
  608. if (fread(yuv, 64, 1, ifo) == 1) {
  609. buf = yuv;
  610. for(i=0; i<16; i++) {
  611. y = *++buf;
  612. cr = *++buf;
  613. cb = *++buf;
  614. YUV_TO_RGB1_CCIR(cb, cr);
  615. YUV_TO_RGB2_CCIR(r, g, b, y);
  616. ctx->palette[i] = (r << 16) + (g << 8) + b;
  617. buf++;
  618. }
  619. ctx->has_palette = 1;
  620. }
  621. }
  622. }
  623. if (ctx->has_palette == 0) {
  624. av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
  625. ret = AVERROR_INVALIDDATA;
  626. }
  627. end:
  628. fclose(ifo);
  629. return ret;
  630. }
  631. static int dvdsub_parse_extradata(AVCodecContext *avctx)
  632. {
  633. DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
  634. char *dataorig, *data;
  635. int ret = 1;
  636. if (!avctx->extradata || !avctx->extradata_size)
  637. return 1;
  638. dataorig = data = av_malloc(avctx->extradata_size+1);
  639. if (!data)
  640. return AVERROR(ENOMEM);
  641. memcpy(data, avctx->extradata, avctx->extradata_size);
  642. data[avctx->extradata_size] = '\0';
  643. for(;;) {
  644. int pos = strcspn(data, "\n\r");
  645. if (pos==0 && *data==0)
  646. break;
  647. if (strncmp("palette:", data, 8) == 0) {
  648. parse_palette(ctx, data + 8);
  649. } else if (strncmp("size:", data, 5) == 0) {
  650. int w, h;
  651. if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
  652. ret = ff_set_dimensions(avctx, w, h);
  653. if (ret < 0)
  654. goto fail;
  655. }
  656. }
  657. data += pos;
  658. data += strspn(data, "\n\r");
  659. }
  660. fail:
  661. av_free(dataorig);
  662. return ret;
  663. }
  664. static av_cold int dvdsub_init(AVCodecContext *avctx)
  665. {
  666. DVDSubContext *ctx = avctx->priv_data;
  667. int ret;
  668. if ((ret = dvdsub_parse_extradata(avctx)) < 0)
  669. return ret;
  670. if (ctx->ifo_str)
  671. parse_ifo_palette(ctx, ctx->ifo_str);
  672. if (ctx->palette_str)
  673. parse_palette(ctx, ctx->palette_str);
  674. if (ctx->has_palette) {
  675. int i;
  676. av_log(avctx, AV_LOG_DEBUG, "palette:");
  677. for(i=0;i<16;i++)
  678. av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
  679. av_log(avctx, AV_LOG_DEBUG, "\n");
  680. }
  681. return 1;
  682. }
  683. static void dvdsub_flush(AVCodecContext *avctx)
  684. {
  685. DVDSubContext *ctx = avctx->priv_data;
  686. ctx->buf_size = 0;
  687. }
  688. static av_cold int dvdsub_close(AVCodecContext *avctx)
  689. {
  690. dvdsub_flush(avctx);
  691. return 0;
  692. }
  693. #define OFFSET(field) offsetof(DVDSubContext, field)
  694. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  695. static const AVOption options[] = {
  696. { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
  697. { "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
  698. { "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
  699. { NULL }
  700. };
  701. static const AVClass dvdsub_class = {
  702. .class_name = "dvdsubdec",
  703. .item_name = av_default_item_name,
  704. .option = options,
  705. .version = LIBAVUTIL_VERSION_INT,
  706. };
  707. AVCodec ff_dvdsub_decoder = {
  708. .name = "dvdsub",
  709. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  710. .type = AVMEDIA_TYPE_SUBTITLE,
  711. .id = AV_CODEC_ID_DVD_SUBTITLE,
  712. .priv_data_size = sizeof(DVDSubContext),
  713. .init = dvdsub_init,
  714. .decode = dvdsub_decode,
  715. .flush = dvdsub_flush,
  716. .close = dvdsub_close,
  717. .priv_class = &dvdsub_class,
  718. };