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.

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