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.

753 lines
22KB

  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;
  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]->pict.data[0]);
  189. av_freep(&sub_header->rects[i]->pict.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. if (buf_size < 10)
  208. return -1;
  209. if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
  210. big_offsets = 1;
  211. offset_size = 4;
  212. cmd_pos = 6;
  213. } else {
  214. big_offsets = 0;
  215. offset_size = 2;
  216. cmd_pos = 2;
  217. }
  218. cmd_pos = READ_OFFSET(buf + cmd_pos);
  219. if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size)
  220. return AVERROR(EAGAIN);
  221. while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
  222. date = AV_RB16(buf + cmd_pos);
  223. next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
  224. av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
  225. cmd_pos, next_cmd_pos, date);
  226. pos = cmd_pos + 2 + offset_size;
  227. offset1 = -1;
  228. offset2 = -1;
  229. x1 = y1 = x2 = y2 = 0;
  230. while (pos < buf_size) {
  231. cmd = buf[pos++];
  232. av_dlog(NULL, "cmd=%02x\n", cmd);
  233. switch(cmd) {
  234. case 0x00:
  235. /* menu subpicture */
  236. is_menu = 1;
  237. break;
  238. case 0x01:
  239. /* set start date */
  240. sub_header->start_display_time = (date << 10) / 90;
  241. break;
  242. case 0x02:
  243. /* set end date */
  244. sub_header->end_display_time = (date << 10) / 90;
  245. break;
  246. case 0x03:
  247. /* set colormap */
  248. if ((buf_size - pos) < 2)
  249. goto fail;
  250. colormap[3] = buf[pos] >> 4;
  251. colormap[2] = buf[pos] & 0x0f;
  252. colormap[1] = buf[pos + 1] >> 4;
  253. colormap[0] = buf[pos + 1] & 0x0f;
  254. pos += 2;
  255. break;
  256. case 0x04:
  257. /* set alpha */
  258. if ((buf_size - pos) < 2)
  259. goto fail;
  260. alpha[3] = buf[pos] >> 4;
  261. alpha[2] = buf[pos] & 0x0f;
  262. alpha[1] = buf[pos + 1] >> 4;
  263. alpha[0] = buf[pos + 1] & 0x0f;
  264. pos += 2;
  265. av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
  266. break;
  267. case 0x05:
  268. case 0x85:
  269. if ((buf_size - pos) < 6)
  270. goto fail;
  271. x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
  272. x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
  273. y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
  274. y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
  275. if (cmd & 0x80)
  276. is_8bit = 1;
  277. av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
  278. pos += 6;
  279. break;
  280. case 0x06:
  281. if ((buf_size - pos) < 4)
  282. goto fail;
  283. offset1 = AV_RB16(buf + pos);
  284. offset2 = AV_RB16(buf + pos + 2);
  285. av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  286. pos += 4;
  287. break;
  288. case 0x86:
  289. if ((buf_size - pos) < 8)
  290. goto fail;
  291. offset1 = AV_RB32(buf + pos);
  292. offset2 = AV_RB32(buf + pos + 4);
  293. av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
  294. pos += 8;
  295. break;
  296. case 0x83:
  297. /* HD set palette */
  298. if ((buf_size - pos) < 768)
  299. goto fail;
  300. yuv_palette = buf + pos;
  301. pos += 768;
  302. break;
  303. case 0x84:
  304. /* HD set contrast (alpha) */
  305. if ((buf_size - pos) < 256)
  306. goto fail;
  307. for (i = 0; i < 256; i++)
  308. alpha[i] = 0xFF - buf[pos+i];
  309. pos += 256;
  310. break;
  311. case 0xff:
  312. goto the_end;
  313. default:
  314. av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
  315. goto the_end;
  316. }
  317. }
  318. the_end:
  319. if (offset1 >= 0) {
  320. int w, h;
  321. uint8_t *bitmap;
  322. /* decode the bitmap */
  323. w = x2 - x1 + 1;
  324. if (w < 0)
  325. w = 0;
  326. h = y2 - y1 + 1;
  327. if (h < 0)
  328. h = 0;
  329. if (w > 0 && h > 0) {
  330. reset_rects(sub_header);
  331. bitmap = av_malloc(w * h);
  332. sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
  333. sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
  334. sub_header->num_rects = 1;
  335. sub_header->rects[0]->pict.data[0] = bitmap;
  336. if (decode_rle(bitmap, w * 2, w, (h + 1) / 2,
  337. buf, offset1, buf_size, is_8bit) < 0)
  338. goto fail;
  339. if (decode_rle(bitmap + w, w * 2, w, h / 2,
  340. buf, offset2, buf_size, is_8bit) < 0)
  341. goto fail;
  342. sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  343. if (is_8bit) {
  344. if (!yuv_palette)
  345. goto fail;
  346. sub_header->rects[0]->nb_colors = 256;
  347. yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
  348. } else {
  349. sub_header->rects[0]->nb_colors = 4;
  350. guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1],
  351. 0xffff00);
  352. }
  353. sub_header->rects[0]->x = x1;
  354. sub_header->rects[0]->y = y1;
  355. sub_header->rects[0]->w = w;
  356. sub_header->rects[0]->h = h;
  357. sub_header->rects[0]->type = SUBTITLE_BITMAP;
  358. sub_header->rects[0]->pict.linesize[0] = w;
  359. sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
  360. }
  361. }
  362. if (next_cmd_pos < cmd_pos) {
  363. av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
  364. break;
  365. }
  366. if (next_cmd_pos == cmd_pos)
  367. break;
  368. cmd_pos = next_cmd_pos;
  369. }
  370. if (sub_header->num_rects > 0)
  371. return is_menu;
  372. fail:
  373. reset_rects(sub_header);
  374. return -1;
  375. }
  376. static int is_transp(const uint8_t *buf, int pitch, int n,
  377. const uint8_t *transp_color)
  378. {
  379. int i;
  380. for(i = 0; i < n; i++) {
  381. if (!transp_color[*buf])
  382. return 0;
  383. buf += pitch;
  384. }
  385. return 1;
  386. }
  387. /* return 0 if empty rectangle, 1 if non empty */
  388. static int find_smallest_bounding_rectangle(AVSubtitle *s)
  389. {
  390. uint8_t transp_color[256] = { 0 };
  391. int y1, y2, x1, x2, y, w, h, i;
  392. uint8_t *bitmap;
  393. if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
  394. return 0;
  395. for(i = 0; i < s->rects[0]->nb_colors; i++) {
  396. if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
  397. transp_color[i] = 1;
  398. }
  399. y1 = 0;
  400. while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
  401. 1, s->rects[0]->w, transp_color))
  402. y1++;
  403. if (y1 == s->rects[0]->h) {
  404. av_freep(&s->rects[0]->pict.data[0]);
  405. s->rects[0]->w = s->rects[0]->h = 0;
  406. return 0;
  407. }
  408. y2 = s->rects[0]->h - 1;
  409. while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
  410. s->rects[0]->w, transp_color))
  411. y2--;
  412. x1 = 0;
  413. while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
  414. s->rects[0]->h, transp_color))
  415. x1++;
  416. x2 = s->rects[0]->w - 1;
  417. while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
  418. transp_color))
  419. x2--;
  420. w = x2 - x1 + 1;
  421. h = y2 - y1 + 1;
  422. bitmap = av_malloc(w * h);
  423. if (!bitmap)
  424. return 1;
  425. for(y = 0; y < h; y++) {
  426. memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
  427. }
  428. av_freep(&s->rects[0]->pict.data[0]);
  429. s->rects[0]->pict.data[0] = bitmap;
  430. s->rects[0]->pict.linesize[0] = w;
  431. s->rects[0]->w = w;
  432. s->rects[0]->h = h;
  433. s->rects[0]->x += x1;
  434. s->rects[0]->y += y1;
  435. return 1;
  436. }
  437. #ifdef DEBUG
  438. #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
  439. static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
  440. uint32_t *rgba_palette)
  441. {
  442. int x, y, alpha;
  443. uint32_t v;
  444. int back[3] = {0, 255, 0}; /* green background */
  445. FILE *f;
  446. f = fopen(filename, "w");
  447. if (!f) {
  448. perror(filename);
  449. return;
  450. }
  451. fprintf(f, "P6\n"
  452. "%d %d\n"
  453. "%d\n",
  454. w, h, 255);
  455. for(y = 0; y < h; y++) {
  456. for(x = 0; x < w; x++) {
  457. v = rgba_palette[bitmap[y * w + x]];
  458. alpha = v >> 24;
  459. putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
  460. putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
  461. putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
  462. }
  463. }
  464. fclose(f);
  465. }
  466. #endif
  467. static int append_to_cached_buf(AVCodecContext *avctx,
  468. const uint8_t *buf, int buf_size)
  469. {
  470. DVDSubContext *ctx = avctx->priv_data;
  471. if (ctx->buf_size > 0xffff - buf_size) {
  472. av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
  473. "too large SPU packets aborted.\n");
  474. av_freep(&ctx->buf);
  475. return AVERROR_INVALIDDATA;
  476. }
  477. ctx->buf = av_realloc(ctx->buf, ctx->buf_size + buf_size);
  478. if (!ctx->buf)
  479. return AVERROR(ENOMEM);
  480. memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
  481. ctx->buf_size += buf_size;
  482. return 0;
  483. }
  484. static int dvdsub_decode(AVCodecContext *avctx,
  485. void *data, int *data_size,
  486. AVPacket *avpkt)
  487. {
  488. DVDSubContext *ctx = avctx->priv_data;
  489. const uint8_t *buf = avpkt->data;
  490. int buf_size = avpkt->size;
  491. AVSubtitle *sub = data;
  492. int is_menu;
  493. if (ctx->buf) {
  494. int ret = append_to_cached_buf(avctx, buf, buf_size);
  495. if (ret < 0) {
  496. *data_size = 0;
  497. return ret;
  498. }
  499. buf = ctx->buf;
  500. buf_size = ctx->buf_size;
  501. }
  502. is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
  503. if (is_menu == AVERROR(EAGAIN)) {
  504. *data_size = 0;
  505. return append_to_cached_buf(avctx, buf, buf_size);
  506. }
  507. if (is_menu < 0) {
  508. no_subtitle:
  509. reset_rects(sub);
  510. *data_size = 0;
  511. return buf_size;
  512. }
  513. if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
  514. goto no_subtitle;
  515. if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
  516. goto no_subtitle;
  517. #if defined(DEBUG)
  518. {
  519. char ppm_name[32];
  520. snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
  521. av_dlog(NULL, "start=%d ms end =%d ms\n",
  522. sub->start_display_time,
  523. sub->end_display_time);
  524. ppm_save(ppm_name, sub->rects[0]->pict.data[0],
  525. sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->pict.data[1]);
  526. }
  527. #endif
  528. av_freep(&ctx->buf);
  529. ctx->buf_size = 0;
  530. *data_size = 1;
  531. return buf_size;
  532. }
  533. static void parse_palette(DVDSubContext *ctx, char *p)
  534. {
  535. int i;
  536. ctx->has_palette = 1;
  537. for(i=0;i<16;i++) {
  538. ctx->palette[i] = strtoul(p, &p, 16);
  539. while(*p == ',' || av_isspace(*p))
  540. p++;
  541. }
  542. }
  543. static int parse_ifo_palette(DVDSubContext *ctx, char *p)
  544. {
  545. FILE *ifo;
  546. char ifostr[12];
  547. uint32_t sp_pgci, pgci, off_pgc, pgc;
  548. uint8_t r, g, b, yuv[65], *buf;
  549. int i, y, cb, cr, r_add, g_add, b_add;
  550. int ret = 0;
  551. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  552. ctx->has_palette = 0;
  553. if ((ifo = fopen(p, "r")) == NULL) {
  554. av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno)));
  555. return AVERROR_EOF;
  556. }
  557. if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
  558. av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
  559. ret = AVERROR_INVALIDDATA;
  560. goto end;
  561. }
  562. if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
  563. ret = AVERROR(errno);
  564. goto end;
  565. }
  566. if (fread(&sp_pgci, 4, 1, ifo) == 1) {
  567. pgci = av_be2ne32(sp_pgci) * 2048;
  568. if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
  569. ret = AVERROR(errno);
  570. goto end;
  571. }
  572. if (fread(&off_pgc, 4, 1, ifo) == 1) {
  573. pgc = pgci + av_be2ne32(off_pgc);
  574. if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
  575. ret = AVERROR(errno);
  576. goto end;
  577. }
  578. if (fread(yuv, 64, 1, ifo) == 1) {
  579. buf = yuv;
  580. for(i=0; i<16; i++) {
  581. y = *++buf;
  582. cr = *++buf;
  583. cb = *++buf;
  584. YUV_TO_RGB1_CCIR(cb, cr);
  585. YUV_TO_RGB2_CCIR(r, g, b, y);
  586. ctx->palette[i] = (r << 16) + (g << 8) + b;
  587. buf++;
  588. }
  589. ctx->has_palette = 1;
  590. }
  591. }
  592. }
  593. if (ctx->has_palette == 0) {
  594. av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
  595. ret = AVERROR_INVALIDDATA;
  596. }
  597. end:
  598. fclose(ifo);
  599. return ret;
  600. }
  601. static int dvdsub_parse_extradata(AVCodecContext *avctx)
  602. {
  603. DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
  604. char *dataorig, *data;
  605. int ret = 1;
  606. if (!avctx->extradata || !avctx->extradata_size)
  607. return 1;
  608. dataorig = data = av_malloc(avctx->extradata_size+1);
  609. if (!data)
  610. return AVERROR(ENOMEM);
  611. memcpy(data, avctx->extradata, avctx->extradata_size);
  612. data[avctx->extradata_size] = '\0';
  613. for(;;) {
  614. int pos = strcspn(data, "\n\r");
  615. if (pos==0 && *data==0)
  616. break;
  617. if (strncmp("palette:", data, 8) == 0) {
  618. parse_palette(ctx, data + 8);
  619. } else if (strncmp("size:", data, 5) == 0) {
  620. int w, h;
  621. if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
  622. ret = ff_set_dimensions(avctx, w, h);
  623. if (ret < 0)
  624. goto fail;
  625. }
  626. }
  627. data += pos;
  628. data += strspn(data, "\n\r");
  629. }
  630. fail:
  631. av_free(dataorig);
  632. return ret;
  633. }
  634. static av_cold int dvdsub_init(AVCodecContext *avctx)
  635. {
  636. DVDSubContext *ctx = avctx->priv_data;
  637. int ret;
  638. if ((ret = dvdsub_parse_extradata(avctx)) < 0)
  639. return ret;
  640. if (ctx->ifo_str)
  641. parse_ifo_palette(ctx, ctx->ifo_str);
  642. if (ctx->palette_str)
  643. parse_palette(ctx, ctx->palette_str);
  644. if (ctx->has_palette) {
  645. int i;
  646. av_log(avctx, AV_LOG_DEBUG, "palette:");
  647. for(i=0;i<16;i++)
  648. av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
  649. av_log(avctx, AV_LOG_DEBUG, "\n");
  650. }
  651. return 1;
  652. }
  653. static av_cold int dvdsub_close(AVCodecContext *avctx)
  654. {
  655. DVDSubContext *ctx = avctx->priv_data;
  656. av_freep(&ctx->buf);
  657. ctx->buf_size = 0;
  658. return 0;
  659. }
  660. #define OFFSET(field) offsetof(DVDSubContext, field)
  661. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  662. static const AVOption options[] = {
  663. { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
  664. { "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
  665. { "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SD},
  666. { NULL }
  667. };
  668. static const AVClass dvdsub_class = {
  669. .class_name = "dvdsubdec",
  670. .item_name = av_default_item_name,
  671. .option = options,
  672. .version = LIBAVUTIL_VERSION_INT,
  673. };
  674. AVCodec ff_dvdsub_decoder = {
  675. .name = "dvdsub",
  676. .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
  677. .type = AVMEDIA_TYPE_SUBTITLE,
  678. .id = AV_CODEC_ID_DVD_SUBTITLE,
  679. .priv_data_size = sizeof(DVDSubContext),
  680. .init = dvdsub_init,
  681. .decode = dvdsub_decode,
  682. .close = dvdsub_close,
  683. .priv_class = &dvdsub_class,
  684. };