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.

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