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.

680 lines
20KB

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