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.

1619 lines
44KB

  1. /*
  2. * DVB subtitle decoding
  3. * Copyright (c) 2005 Ian Caulfield
  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 "bytestream.h"
  24. #include "libavutil/colorspace.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/opt.h"
  27. #define DVBSUB_PAGE_SEGMENT 0x10
  28. #define DVBSUB_REGION_SEGMENT 0x11
  29. #define DVBSUB_CLUT_SEGMENT 0x12
  30. #define DVBSUB_OBJECT_SEGMENT 0x13
  31. #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
  32. #define DVBSUB_DISPLAY_SEGMENT 0x80
  33. #define cm (ff_crop_tab + MAX_NEG_CROP)
  34. #ifdef DEBUG
  35. #if 0
  36. static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
  37. uint32_t *rgba_palette)
  38. {
  39. int x, y, v;
  40. FILE *f;
  41. char fname[40], fname2[40];
  42. char command[1024];
  43. snprintf(fname, 40, "%s.ppm", filename);
  44. f = fopen(fname, "w");
  45. if (!f) {
  46. perror(fname);
  47. return;
  48. }
  49. fprintf(f, "P6\n"
  50. "%d %d\n"
  51. "%d\n",
  52. w, h, 255);
  53. for(y = 0; y < h; y++) {
  54. for(x = 0; x < w; x++) {
  55. v = rgba_palette[bitmap[y * w + x]];
  56. putc((v >> 16) & 0xff, f);
  57. putc((v >> 8) & 0xff, f);
  58. putc((v >> 0) & 0xff, f);
  59. }
  60. }
  61. fclose(f);
  62. snprintf(fname2, 40, "%s-a.pgm", filename);
  63. f = fopen(fname2, "w");
  64. if (!f) {
  65. perror(fname2);
  66. return;
  67. }
  68. fprintf(f, "P5\n"
  69. "%d %d\n"
  70. "%d\n",
  71. w, h, 255);
  72. for(y = 0; y < h; y++) {
  73. for(x = 0; x < w; x++) {
  74. v = rgba_palette[bitmap[y * w + x]];
  75. putc((v >> 24) & 0xff, f);
  76. }
  77. }
  78. fclose(f);
  79. snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
  80. system(command);
  81. snprintf(command, 1024, "rm %s %s", fname, fname2);
  82. system(command);
  83. }
  84. #endif
  85. static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
  86. {
  87. int x, y, v;
  88. FILE *f;
  89. char fname[40], fname2[40];
  90. char command[1024];
  91. snprintf(fname, sizeof(fname), "%s.ppm", filename);
  92. f = fopen(fname, "w");
  93. if (!f) {
  94. perror(fname);
  95. return;
  96. }
  97. fprintf(f, "P6\n"
  98. "%d %d\n"
  99. "%d\n",
  100. w, h, 255);
  101. for(y = 0; y < h; y++) {
  102. for(x = 0; x < w; x++) {
  103. v = bitmap[y * w + x];
  104. putc((v >> 16) & 0xff, f);
  105. putc((v >> 8) & 0xff, f);
  106. putc((v >> 0) & 0xff, f);
  107. }
  108. }
  109. fclose(f);
  110. snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
  111. f = fopen(fname2, "w");
  112. if (!f) {
  113. perror(fname2);
  114. return;
  115. }
  116. fprintf(f, "P5\n"
  117. "%d %d\n"
  118. "%d\n",
  119. w, h, 255);
  120. for(y = 0; y < h; y++) {
  121. for(x = 0; x < w; x++) {
  122. v = bitmap[y * w + x];
  123. putc((v >> 24) & 0xff, f);
  124. }
  125. }
  126. fclose(f);
  127. snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
  128. system(command);
  129. snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
  130. system(command);
  131. }
  132. #endif
  133. #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
  134. typedef struct DVBSubCLUT {
  135. int id;
  136. int version;
  137. uint32_t clut4[4];
  138. uint32_t clut16[16];
  139. uint32_t clut256[256];
  140. struct DVBSubCLUT *next;
  141. } DVBSubCLUT;
  142. static DVBSubCLUT default_clut;
  143. typedef struct DVBSubObjectDisplay {
  144. int object_id;
  145. int region_id;
  146. int x_pos;
  147. int y_pos;
  148. int fgcolor;
  149. int bgcolor;
  150. struct DVBSubObjectDisplay *region_list_next;
  151. struct DVBSubObjectDisplay *object_list_next;
  152. } DVBSubObjectDisplay;
  153. typedef struct DVBSubObject {
  154. int id;
  155. int version;
  156. int type;
  157. DVBSubObjectDisplay *display_list;
  158. struct DVBSubObject *next;
  159. } DVBSubObject;
  160. typedef struct DVBSubRegionDisplay {
  161. int region_id;
  162. int x_pos;
  163. int y_pos;
  164. struct DVBSubRegionDisplay *next;
  165. } DVBSubRegionDisplay;
  166. typedef struct DVBSubRegion {
  167. int id;
  168. int version;
  169. int width;
  170. int height;
  171. int depth;
  172. int clut;
  173. int bgcolor;
  174. uint8_t *pbuf;
  175. int buf_size;
  176. int dirty;
  177. DVBSubObjectDisplay *display_list;
  178. struct DVBSubRegion *next;
  179. } DVBSubRegion;
  180. typedef struct DVBSubDisplayDefinition {
  181. int version;
  182. int x;
  183. int y;
  184. int width;
  185. int height;
  186. } DVBSubDisplayDefinition;
  187. typedef struct DVBSubContext {
  188. AVClass *class;
  189. int composition_id;
  190. int ancillary_id;
  191. int version;
  192. int time_out;
  193. int compute_edt; /**< if 1 end display time calculated using pts
  194. if 0 (Default) calculated using time out */
  195. int64_t prev_start;
  196. DVBSubRegion *region_list;
  197. DVBSubCLUT *clut_list;
  198. DVBSubObject *object_list;
  199. DVBSubRegionDisplay *display_list;
  200. DVBSubDisplayDefinition *display_definition;
  201. } DVBSubContext;
  202. static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
  203. {
  204. DVBSubObject *ptr = ctx->object_list;
  205. while (ptr && ptr->id != object_id) {
  206. ptr = ptr->next;
  207. }
  208. return ptr;
  209. }
  210. static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
  211. {
  212. DVBSubCLUT *ptr = ctx->clut_list;
  213. while (ptr && ptr->id != clut_id) {
  214. ptr = ptr->next;
  215. }
  216. return ptr;
  217. }
  218. static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
  219. {
  220. DVBSubRegion *ptr = ctx->region_list;
  221. while (ptr && ptr->id != region_id) {
  222. ptr = ptr->next;
  223. }
  224. return ptr;
  225. }
  226. static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
  227. {
  228. DVBSubObject *object, *obj2, **obj2_ptr;
  229. DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
  230. while (region->display_list) {
  231. display = region->display_list;
  232. object = get_object(ctx, display->object_id);
  233. if (object) {
  234. obj_disp_ptr = &object->display_list;
  235. obj_disp = *obj_disp_ptr;
  236. while (obj_disp && obj_disp != display) {
  237. obj_disp_ptr = &obj_disp->object_list_next;
  238. obj_disp = *obj_disp_ptr;
  239. }
  240. if (obj_disp) {
  241. *obj_disp_ptr = obj_disp->object_list_next;
  242. if (!object->display_list) {
  243. obj2_ptr = &ctx->object_list;
  244. obj2 = *obj2_ptr;
  245. while (obj2 != object) {
  246. assert(obj2);
  247. obj2_ptr = &obj2->next;
  248. obj2 = *obj2_ptr;
  249. }
  250. *obj2_ptr = obj2->next;
  251. av_free(obj2);
  252. }
  253. }
  254. }
  255. region->display_list = display->region_list_next;
  256. av_free(display);
  257. }
  258. }
  259. static void delete_cluts(DVBSubContext *ctx)
  260. {
  261. DVBSubCLUT *clut;
  262. while (ctx->clut_list) {
  263. clut = ctx->clut_list;
  264. ctx->clut_list = clut->next;
  265. av_free(clut);
  266. }
  267. }
  268. static void delete_objects(DVBSubContext *ctx)
  269. {
  270. DVBSubObject *object;
  271. while (ctx->object_list) {
  272. object = ctx->object_list;
  273. ctx->object_list = object->next;
  274. av_free(object);
  275. }
  276. }
  277. static void delete_regions(DVBSubContext *ctx)
  278. {
  279. DVBSubRegion *region;
  280. while (ctx->region_list) {
  281. region = ctx->region_list;
  282. ctx->region_list = region->next;
  283. delete_region_display_list(ctx, region);
  284. av_free(region->pbuf);
  285. av_free(region);
  286. }
  287. }
  288. static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
  289. {
  290. int i, r, g, b, a = 0;
  291. DVBSubContext *ctx = avctx->priv_data;
  292. if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
  293. av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
  294. ctx->composition_id = -1;
  295. ctx->ancillary_id = -1;
  296. } else {
  297. if (avctx->extradata_size > 5) {
  298. av_log(avctx, AV_LOG_WARNING, "Decoding first DVB subtitles sub-stream\n");
  299. }
  300. ctx->composition_id = AV_RB16(avctx->extradata);
  301. ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
  302. }
  303. ctx->version = -1;
  304. ctx->prev_start = AV_NOPTS_VALUE;
  305. default_clut.id = -1;
  306. default_clut.next = NULL;
  307. default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
  308. default_clut.clut4[1] = RGBA(255, 255, 255, 255);
  309. default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
  310. default_clut.clut4[3] = RGBA(127, 127, 127, 255);
  311. default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
  312. for (i = 1; i < 16; i++) {
  313. if (i < 8) {
  314. r = (i & 1) ? 255 : 0;
  315. g = (i & 2) ? 255 : 0;
  316. b = (i & 4) ? 255 : 0;
  317. } else {
  318. r = (i & 1) ? 127 : 0;
  319. g = (i & 2) ? 127 : 0;
  320. b = (i & 4) ? 127 : 0;
  321. }
  322. default_clut.clut16[i] = RGBA(r, g, b, 255);
  323. }
  324. default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
  325. for (i = 1; i < 256; i++) {
  326. if (i < 8) {
  327. r = (i & 1) ? 255 : 0;
  328. g = (i & 2) ? 255 : 0;
  329. b = (i & 4) ? 255 : 0;
  330. a = 63;
  331. } else {
  332. switch (i & 0x88) {
  333. case 0x00:
  334. r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
  335. g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
  336. b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
  337. a = 255;
  338. break;
  339. case 0x08:
  340. r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
  341. g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
  342. b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
  343. a = 127;
  344. break;
  345. case 0x80:
  346. r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
  347. g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
  348. b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
  349. a = 255;
  350. break;
  351. case 0x88:
  352. r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
  353. g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
  354. b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
  355. a = 255;
  356. break;
  357. }
  358. }
  359. default_clut.clut256[i] = RGBA(r, g, b, a);
  360. }
  361. return 0;
  362. }
  363. static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
  364. {
  365. DVBSubContext *ctx = avctx->priv_data;
  366. DVBSubRegionDisplay *display;
  367. delete_regions(ctx);
  368. delete_objects(ctx);
  369. delete_cluts(ctx);
  370. av_freep(&ctx->display_definition);
  371. while (ctx->display_list) {
  372. display = ctx->display_list;
  373. ctx->display_list = display->next;
  374. av_free(display);
  375. }
  376. return 0;
  377. }
  378. static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
  379. const uint8_t **srcbuf, int buf_size,
  380. int non_mod, uint8_t *map_table, int x_pos)
  381. {
  382. GetBitContext gb;
  383. int bits;
  384. int run_length;
  385. int pixels_read = x_pos;
  386. init_get_bits(&gb, *srcbuf, buf_size << 3);
  387. destbuf += x_pos;
  388. while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
  389. bits = get_bits(&gb, 2);
  390. if (bits) {
  391. if (non_mod != 1 || bits != 1) {
  392. if (map_table)
  393. *destbuf++ = map_table[bits];
  394. else
  395. *destbuf++ = bits;
  396. }
  397. pixels_read++;
  398. } else {
  399. bits = get_bits1(&gb);
  400. if (bits == 1) {
  401. run_length = get_bits(&gb, 3) + 3;
  402. bits = get_bits(&gb, 2);
  403. if (non_mod == 1 && bits == 1)
  404. pixels_read += run_length;
  405. else {
  406. if (map_table)
  407. bits = map_table[bits];
  408. while (run_length-- > 0 && pixels_read < dbuf_len) {
  409. *destbuf++ = bits;
  410. pixels_read++;
  411. }
  412. }
  413. } else {
  414. bits = get_bits1(&gb);
  415. if (bits == 0) {
  416. bits = get_bits(&gb, 2);
  417. if (bits == 2) {
  418. run_length = get_bits(&gb, 4) + 12;
  419. bits = get_bits(&gb, 2);
  420. if (non_mod == 1 && bits == 1)
  421. pixels_read += run_length;
  422. else {
  423. if (map_table)
  424. bits = map_table[bits];
  425. while (run_length-- > 0 && pixels_read < dbuf_len) {
  426. *destbuf++ = bits;
  427. pixels_read++;
  428. }
  429. }
  430. } else if (bits == 3) {
  431. run_length = get_bits(&gb, 8) + 29;
  432. bits = get_bits(&gb, 2);
  433. if (non_mod == 1 && bits == 1)
  434. pixels_read += run_length;
  435. else {
  436. if (map_table)
  437. bits = map_table[bits];
  438. while (run_length-- > 0 && pixels_read < dbuf_len) {
  439. *destbuf++ = bits;
  440. pixels_read++;
  441. }
  442. }
  443. } else if (bits == 1) {
  444. if (map_table)
  445. bits = map_table[0];
  446. else
  447. bits = 0;
  448. run_length = 2;
  449. while (run_length-- > 0 && pixels_read < dbuf_len) {
  450. *destbuf++ = bits;
  451. pixels_read++;
  452. }
  453. } else {
  454. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  455. return pixels_read;
  456. }
  457. } else {
  458. if (map_table)
  459. bits = map_table[0];
  460. else
  461. bits = 0;
  462. *destbuf++ = bits;
  463. pixels_read++;
  464. }
  465. }
  466. }
  467. }
  468. if (get_bits(&gb, 6))
  469. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  470. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  471. return pixels_read;
  472. }
  473. static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
  474. const uint8_t **srcbuf, int buf_size,
  475. int non_mod, uint8_t *map_table, int x_pos)
  476. {
  477. GetBitContext gb;
  478. int bits;
  479. int run_length;
  480. int pixels_read = x_pos;
  481. init_get_bits(&gb, *srcbuf, buf_size << 3);
  482. destbuf += x_pos;
  483. while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
  484. bits = get_bits(&gb, 4);
  485. if (bits) {
  486. if (non_mod != 1 || bits != 1) {
  487. if (map_table)
  488. *destbuf++ = map_table[bits];
  489. else
  490. *destbuf++ = bits;
  491. }
  492. pixels_read++;
  493. } else {
  494. bits = get_bits1(&gb);
  495. if (bits == 0) {
  496. run_length = get_bits(&gb, 3);
  497. if (run_length == 0) {
  498. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  499. return pixels_read;
  500. }
  501. run_length += 2;
  502. if (map_table)
  503. bits = map_table[0];
  504. else
  505. bits = 0;
  506. while (run_length-- > 0 && pixels_read < dbuf_len) {
  507. *destbuf++ = bits;
  508. pixels_read++;
  509. }
  510. } else {
  511. bits = get_bits1(&gb);
  512. if (bits == 0) {
  513. run_length = get_bits(&gb, 2) + 4;
  514. bits = get_bits(&gb, 4);
  515. if (non_mod == 1 && bits == 1)
  516. pixels_read += run_length;
  517. else {
  518. if (map_table)
  519. bits = map_table[bits];
  520. while (run_length-- > 0 && pixels_read < dbuf_len) {
  521. *destbuf++ = bits;
  522. pixels_read++;
  523. }
  524. }
  525. } else {
  526. bits = get_bits(&gb, 2);
  527. if (bits == 2) {
  528. run_length = get_bits(&gb, 4) + 9;
  529. bits = get_bits(&gb, 4);
  530. if (non_mod == 1 && bits == 1)
  531. pixels_read += run_length;
  532. else {
  533. if (map_table)
  534. bits = map_table[bits];
  535. while (run_length-- > 0 && pixels_read < dbuf_len) {
  536. *destbuf++ = bits;
  537. pixels_read++;
  538. }
  539. }
  540. } else if (bits == 3) {
  541. run_length = get_bits(&gb, 8) + 25;
  542. bits = get_bits(&gb, 4);
  543. if (non_mod == 1 && bits == 1)
  544. pixels_read += run_length;
  545. else {
  546. if (map_table)
  547. bits = map_table[bits];
  548. while (run_length-- > 0 && pixels_read < dbuf_len) {
  549. *destbuf++ = bits;
  550. pixels_read++;
  551. }
  552. }
  553. } else if (bits == 1) {
  554. if (map_table)
  555. bits = map_table[0];
  556. else
  557. bits = 0;
  558. run_length = 2;
  559. while (run_length-- > 0 && pixels_read < dbuf_len) {
  560. *destbuf++ = bits;
  561. pixels_read++;
  562. }
  563. } else {
  564. if (map_table)
  565. bits = map_table[0];
  566. else
  567. bits = 0;
  568. *destbuf++ = bits;
  569. pixels_read ++;
  570. }
  571. }
  572. }
  573. }
  574. }
  575. if (get_bits(&gb, 8))
  576. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  577. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  578. return pixels_read;
  579. }
  580. static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
  581. const uint8_t **srcbuf, int buf_size,
  582. int non_mod, uint8_t *map_table, int x_pos)
  583. {
  584. const uint8_t *sbuf_end = (*srcbuf) + buf_size;
  585. int bits;
  586. int run_length;
  587. int pixels_read = x_pos;
  588. destbuf += x_pos;
  589. while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
  590. bits = *(*srcbuf)++;
  591. if (bits) {
  592. if (non_mod != 1 || bits != 1) {
  593. if (map_table)
  594. *destbuf++ = map_table[bits];
  595. else
  596. *destbuf++ = bits;
  597. }
  598. pixels_read++;
  599. } else {
  600. bits = *(*srcbuf)++;
  601. run_length = bits & 0x7f;
  602. if ((bits & 0x80) == 0) {
  603. if (run_length == 0) {
  604. return pixels_read;
  605. }
  606. if (map_table)
  607. bits = map_table[0];
  608. else
  609. bits = 0;
  610. while (run_length-- > 0 && pixels_read < dbuf_len) {
  611. *destbuf++ = bits;
  612. pixels_read++;
  613. }
  614. } else {
  615. bits = *(*srcbuf)++;
  616. if (non_mod == 1 && bits == 1)
  617. pixels_read += run_length;
  618. if (map_table)
  619. bits = map_table[bits];
  620. else while (run_length-- > 0 && pixels_read < dbuf_len) {
  621. *destbuf++ = bits;
  622. pixels_read++;
  623. }
  624. }
  625. }
  626. }
  627. if (*(*srcbuf)++)
  628. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  629. return pixels_read;
  630. }
  631. static void save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
  632. {
  633. DVBSubContext *ctx = avctx->priv_data;
  634. DVBSubRegionDisplay *display;
  635. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  636. DVBSubRegion *region;
  637. AVSubtitleRect *rect;
  638. DVBSubCLUT *clut;
  639. uint32_t *clut_table;
  640. int i;
  641. int offset_x=0, offset_y=0;
  642. if (display_def) {
  643. offset_x = display_def->x;
  644. offset_y = display_def->y;
  645. }
  646. /* Not touching AVSubtitles again*/
  647. if(sub->num_rects) {
  648. avpriv_request_sample(ctx, "Different Version of Segment asked Twice\n");
  649. return;
  650. }
  651. for (display = ctx->display_list; display; display = display->next) {
  652. region = get_region(ctx, display->region_id);
  653. if (region && region->dirty)
  654. sub->num_rects++;
  655. }
  656. if(ctx->compute_edt == 0) {
  657. sub->end_display_time = ctx->time_out * 1000;
  658. *got_output = 1;
  659. } else if (ctx->prev_start != AV_NOPTS_VALUE) {
  660. sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
  661. *got_output = 1;
  662. }
  663. if (sub->num_rects > 0) {
  664. sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
  665. for(i=0; i<sub->num_rects; i++)
  666. sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
  667. i = 0;
  668. for (display = ctx->display_list; display; display = display->next) {
  669. region = get_region(ctx, display->region_id);
  670. if (!region)
  671. continue;
  672. if (!region->dirty)
  673. continue;
  674. rect = sub->rects[i];
  675. rect->x = display->x_pos + offset_x;
  676. rect->y = display->y_pos + offset_y;
  677. rect->w = region->width;
  678. rect->h = region->height;
  679. rect->nb_colors = (1 << region->depth);
  680. rect->type = SUBTITLE_BITMAP;
  681. rect->pict.linesize[0] = region->width;
  682. clut = get_clut(ctx, region->clut);
  683. if (!clut)
  684. clut = &default_clut;
  685. switch (region->depth) {
  686. case 2:
  687. clut_table = clut->clut4;
  688. break;
  689. case 8:
  690. clut_table = clut->clut256;
  691. break;
  692. case 4:
  693. default:
  694. clut_table = clut->clut16;
  695. break;
  696. }
  697. rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  698. memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
  699. rect->pict.data[0] = av_malloc(region->buf_size);
  700. memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
  701. i++;
  702. }
  703. }
  704. }
  705. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  706. const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  707. {
  708. DVBSubContext *ctx = avctx->priv_data;
  709. DVBSubRegion *region = get_region(ctx, display->region_id);
  710. const uint8_t *buf_end = buf + buf_size;
  711. uint8_t *pbuf;
  712. int x_pos, y_pos;
  713. int i;
  714. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  715. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  716. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  717. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  718. uint8_t *map_table;
  719. #if 0
  720. av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
  721. top_bottom ? "bottom" : "top");
  722. for (i = 0; i < buf_size; i++) {
  723. if (i % 16 == 0)
  724. av_dlog(avctx, "0x%8p: ", buf+i);
  725. av_dlog(avctx, "%02x ", buf[i]);
  726. if (i % 16 == 15)
  727. av_dlog(avctx, "\n");
  728. }
  729. if (i % 16)
  730. av_dlog(avctx, "\n");
  731. #endif
  732. if (!region)
  733. return;
  734. pbuf = region->pbuf;
  735. region->dirty = 1;
  736. x_pos = display->x_pos;
  737. y_pos = display->y_pos;
  738. y_pos += top_bottom;
  739. while (buf < buf_end) {
  740. if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
  741. av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
  742. return;
  743. }
  744. switch (*buf++) {
  745. case 0x10:
  746. if (region->depth == 8)
  747. map_table = map2to8;
  748. else if (region->depth == 4)
  749. map_table = map2to4;
  750. else
  751. map_table = NULL;
  752. x_pos = dvbsub_read_2bit_string(pbuf + (y_pos * region->width),
  753. region->width, &buf, buf_end - buf,
  754. non_mod, map_table, x_pos);
  755. break;
  756. case 0x11:
  757. if (region->depth < 4) {
  758. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  759. return;
  760. }
  761. if (region->depth == 8)
  762. map_table = map4to8;
  763. else
  764. map_table = NULL;
  765. x_pos = dvbsub_read_4bit_string(pbuf + (y_pos * region->width),
  766. region->width, &buf, buf_end - buf,
  767. non_mod, map_table, x_pos);
  768. break;
  769. case 0x12:
  770. if (region->depth < 8) {
  771. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  772. return;
  773. }
  774. x_pos = dvbsub_read_8bit_string(pbuf + (y_pos * region->width),
  775. region->width, &buf, buf_end - buf,
  776. non_mod, NULL, x_pos);
  777. break;
  778. case 0x20:
  779. map2to4[0] = (*buf) >> 4;
  780. map2to4[1] = (*buf++) & 0xf;
  781. map2to4[2] = (*buf) >> 4;
  782. map2to4[3] = (*buf++) & 0xf;
  783. break;
  784. case 0x21:
  785. for (i = 0; i < 4; i++)
  786. map2to8[i] = *buf++;
  787. break;
  788. case 0x22:
  789. for (i = 0; i < 16; i++)
  790. map4to8[i] = *buf++;
  791. break;
  792. case 0xf0:
  793. x_pos = display->x_pos;
  794. y_pos += 2;
  795. break;
  796. default:
  797. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  798. }
  799. }
  800. }
  801. static void dvbsub_parse_object_segment(AVCodecContext *avctx,
  802. const uint8_t *buf, int buf_size)
  803. {
  804. DVBSubContext *ctx = avctx->priv_data;
  805. const uint8_t *buf_end = buf + buf_size;
  806. int object_id;
  807. DVBSubObject *object;
  808. DVBSubObjectDisplay *display;
  809. int top_field_len, bottom_field_len;
  810. int coding_method, non_modifying_color;
  811. object_id = AV_RB16(buf);
  812. buf += 2;
  813. object = get_object(ctx, object_id);
  814. if (!object)
  815. return;
  816. coding_method = ((*buf) >> 2) & 3;
  817. non_modifying_color = ((*buf++) >> 1) & 1;
  818. if (coding_method == 0) {
  819. top_field_len = AV_RB16(buf);
  820. buf += 2;
  821. bottom_field_len = AV_RB16(buf);
  822. buf += 2;
  823. if (buf + top_field_len + bottom_field_len > buf_end) {
  824. av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
  825. return;
  826. }
  827. for (display = object->display_list; display; display = display->object_list_next) {
  828. const uint8_t *block = buf;
  829. int bfl = bottom_field_len;
  830. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  831. non_modifying_color);
  832. if (bottom_field_len > 0)
  833. block = buf + top_field_len;
  834. else
  835. bfl = top_field_len;
  836. dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
  837. non_modifying_color);
  838. }
  839. /* } else if (coding_method == 1) {*/
  840. } else {
  841. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  842. }
  843. }
  844. static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
  845. const uint8_t *buf, int buf_size)
  846. {
  847. DVBSubContext *ctx = avctx->priv_data;
  848. const uint8_t *buf_end = buf + buf_size;
  849. int i, clut_id;
  850. int version;
  851. DVBSubCLUT *clut;
  852. int entry_id, depth , full_range;
  853. int y, cr, cb, alpha;
  854. int r, g, b, r_add, g_add, b_add;
  855. av_dlog(avctx, "DVB clut packet:\n");
  856. for (i=0; i < buf_size; i++) {
  857. av_dlog(avctx, "%02x ", buf[i]);
  858. if (i % 16 == 15)
  859. av_dlog(avctx, "\n");
  860. }
  861. if (i % 16)
  862. av_dlog(avctx, "\n");
  863. clut_id = *buf++;
  864. version = ((*buf)>>4)&15;
  865. buf += 1;
  866. clut = get_clut(ctx, clut_id);
  867. if (!clut) {
  868. clut = av_malloc(sizeof(DVBSubCLUT));
  869. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  870. clut->id = clut_id;
  871. clut->version = -1;
  872. clut->next = ctx->clut_list;
  873. ctx->clut_list = clut;
  874. }
  875. if (clut->version != version) {
  876. clut->version = version;
  877. while (buf + 4 < buf_end) {
  878. entry_id = *buf++;
  879. depth = (*buf) & 0xe0;
  880. if (depth == 0) {
  881. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  882. return 0;
  883. }
  884. full_range = (*buf++) & 1;
  885. if (full_range) {
  886. y = *buf++;
  887. cr = *buf++;
  888. cb = *buf++;
  889. alpha = *buf++;
  890. } else {
  891. y = buf[0] & 0xfc;
  892. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  893. cb = (buf[1] << 2) & 0xf0;
  894. alpha = (buf[1] << 6) & 0xc0;
  895. buf += 2;
  896. }
  897. if (y == 0)
  898. alpha = 0xff;
  899. YUV_TO_RGB1_CCIR(cb, cr);
  900. YUV_TO_RGB2_CCIR(r, g, b, y);
  901. av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  902. if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
  903. av_dlog(avctx, "More than one bit level marked: %x\n", depth);
  904. if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
  905. return AVERROR_INVALIDDATA;
  906. }
  907. if (depth & 0x80)
  908. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  909. else if (depth & 0x40)
  910. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  911. else if (depth & 0x20)
  912. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  913. }
  914. }
  915. return 0;
  916. }
  917. static void dvbsub_parse_region_segment(AVCodecContext *avctx,
  918. const uint8_t *buf, int buf_size)
  919. {
  920. DVBSubContext *ctx = avctx->priv_data;
  921. const uint8_t *buf_end = buf + buf_size;
  922. int region_id, object_id;
  923. int av_unused version;
  924. DVBSubRegion *region;
  925. DVBSubObject *object;
  926. DVBSubObjectDisplay *display;
  927. int fill;
  928. int ret;
  929. if (buf_size < 10)
  930. return;
  931. region_id = *buf++;
  932. region = get_region(ctx, region_id);
  933. if (!region) {
  934. region = av_mallocz(sizeof(DVBSubRegion));
  935. region->id = region_id;
  936. region->version = -1;
  937. region->next = ctx->region_list;
  938. ctx->region_list = region;
  939. }
  940. version = ((*buf)>>4) & 15;
  941. fill = ((*buf++) >> 3) & 1;
  942. region->width = AV_RB16(buf);
  943. buf += 2;
  944. region->height = AV_RB16(buf);
  945. buf += 2;
  946. ret = av_image_check_size(region->width, region->height, 0, avctx);
  947. if (ret < 0) {
  948. region->width= region->height= 0;
  949. return;
  950. }
  951. if (region->width * region->height != region->buf_size) {
  952. av_free(region->pbuf);
  953. region->buf_size = region->width * region->height;
  954. region->pbuf = av_malloc(region->buf_size);
  955. fill = 1;
  956. region->dirty = 0;
  957. }
  958. region->depth = 1 << (((*buf++) >> 2) & 7);
  959. if(region->depth<2 || region->depth>8){
  960. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  961. region->depth= 4;
  962. }
  963. region->clut = *buf++;
  964. if (region->depth == 8) {
  965. region->bgcolor = *buf++;
  966. buf += 1;
  967. } else {
  968. buf += 1;
  969. if (region->depth == 4)
  970. region->bgcolor = (((*buf++) >> 4) & 15);
  971. else
  972. region->bgcolor = (((*buf++) >> 2) & 3);
  973. }
  974. av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  975. if (fill) {
  976. memset(region->pbuf, region->bgcolor, region->buf_size);
  977. av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
  978. }
  979. delete_region_display_list(ctx, region);
  980. while (buf + 5 < buf_end) {
  981. object_id = AV_RB16(buf);
  982. buf += 2;
  983. object = get_object(ctx, object_id);
  984. if (!object) {
  985. object = av_mallocz(sizeof(DVBSubObject));
  986. object->id = object_id;
  987. object->next = ctx->object_list;
  988. ctx->object_list = object;
  989. }
  990. object->type = (*buf) >> 6;
  991. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  992. display->object_id = object_id;
  993. display->region_id = region_id;
  994. display->x_pos = AV_RB16(buf) & 0xfff;
  995. buf += 2;
  996. display->y_pos = AV_RB16(buf) & 0xfff;
  997. buf += 2;
  998. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  999. display->fgcolor = *buf++;
  1000. display->bgcolor = *buf++;
  1001. }
  1002. display->region_list_next = region->display_list;
  1003. region->display_list = display;
  1004. display->object_list_next = object->display_list;
  1005. object->display_list = display;
  1006. }
  1007. }
  1008. static void dvbsub_parse_page_segment(AVCodecContext *avctx,
  1009. const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
  1010. {
  1011. DVBSubContext *ctx = avctx->priv_data;
  1012. DVBSubRegionDisplay *display;
  1013. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  1014. const uint8_t *buf_end = buf + buf_size;
  1015. int region_id;
  1016. int page_state;
  1017. int timeout;
  1018. int version;
  1019. if (buf_size < 1)
  1020. return;
  1021. timeout = *buf++;
  1022. version = ((*buf)>>4) & 15;
  1023. page_state = ((*buf++) >> 2) & 3;
  1024. if (ctx->version == version) {
  1025. return;
  1026. }
  1027. ctx->time_out = timeout;
  1028. ctx->version = version;
  1029. av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  1030. if(ctx->compute_edt == 1)
  1031. save_subtitle_set(avctx, sub, got_output);
  1032. if (page_state == 1 || page_state == 2) {
  1033. delete_regions(ctx);
  1034. delete_objects(ctx);
  1035. delete_cluts(ctx);
  1036. }
  1037. tmp_display_list = ctx->display_list;
  1038. ctx->display_list = NULL;
  1039. while (buf + 5 < buf_end) {
  1040. region_id = *buf++;
  1041. buf += 1;
  1042. display = tmp_display_list;
  1043. tmp_ptr = &tmp_display_list;
  1044. while (display && display->region_id != region_id) {
  1045. tmp_ptr = &display->next;
  1046. display = display->next;
  1047. }
  1048. if (!display)
  1049. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  1050. display->region_id = region_id;
  1051. display->x_pos = AV_RB16(buf);
  1052. buf += 2;
  1053. display->y_pos = AV_RB16(buf);
  1054. buf += 2;
  1055. *tmp_ptr = display->next;
  1056. display->next = ctx->display_list;
  1057. ctx->display_list = display;
  1058. av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  1059. }
  1060. while (tmp_display_list) {
  1061. display = tmp_display_list;
  1062. tmp_display_list = display->next;
  1063. av_free(display);
  1064. }
  1065. }
  1066. #ifdef DEBUG
  1067. static void save_display_set(DVBSubContext *ctx)
  1068. {
  1069. DVBSubRegion *region;
  1070. DVBSubRegionDisplay *display;
  1071. DVBSubCLUT *clut;
  1072. uint32_t *clut_table;
  1073. int x_pos, y_pos, width, height;
  1074. int x, y, y_off, x_off;
  1075. uint32_t *pbuf;
  1076. char filename[32];
  1077. static int fileno_index = 0;
  1078. x_pos = -1;
  1079. y_pos = -1;
  1080. width = 0;
  1081. height = 0;
  1082. for (display = ctx->display_list; display; display = display->next) {
  1083. region = get_region(ctx, display->region_id);
  1084. if (x_pos == -1) {
  1085. x_pos = display->x_pos;
  1086. y_pos = display->y_pos;
  1087. width = region->width;
  1088. height = region->height;
  1089. } else {
  1090. if (display->x_pos < x_pos) {
  1091. width += (x_pos - display->x_pos);
  1092. x_pos = display->x_pos;
  1093. }
  1094. if (display->y_pos < y_pos) {
  1095. height += (y_pos - display->y_pos);
  1096. y_pos = display->y_pos;
  1097. }
  1098. if (display->x_pos + region->width > x_pos + width) {
  1099. width = display->x_pos + region->width - x_pos;
  1100. }
  1101. if (display->y_pos + region->height > y_pos + height) {
  1102. height = display->y_pos + region->height - y_pos;
  1103. }
  1104. }
  1105. }
  1106. if (x_pos >= 0) {
  1107. pbuf = av_malloc(width * height * 4);
  1108. for (display = ctx->display_list; display; display = display->next) {
  1109. region = get_region(ctx, display->region_id);
  1110. x_off = display->x_pos - x_pos;
  1111. y_off = display->y_pos - y_pos;
  1112. clut = get_clut(ctx, region->clut);
  1113. if (!clut)
  1114. clut = &default_clut;
  1115. switch (region->depth) {
  1116. case 2:
  1117. clut_table = clut->clut4;
  1118. break;
  1119. case 8:
  1120. clut_table = clut->clut256;
  1121. break;
  1122. case 4:
  1123. default:
  1124. clut_table = clut->clut16;
  1125. break;
  1126. }
  1127. for (y = 0; y < region->height; y++) {
  1128. for (x = 0; x < region->width; x++) {
  1129. pbuf[((y + y_off) * width) + x_off + x] =
  1130. clut_table[region->pbuf[y * region->width + x]];
  1131. }
  1132. }
  1133. }
  1134. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  1135. png_save2(filename, pbuf, width, height);
  1136. av_free(pbuf);
  1137. }
  1138. fileno_index++;
  1139. }
  1140. #endif
  1141. static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
  1142. const uint8_t *buf,
  1143. int buf_size)
  1144. {
  1145. DVBSubContext *ctx = avctx->priv_data;
  1146. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1147. int dds_version, info_byte;
  1148. if (buf_size < 5)
  1149. return;
  1150. info_byte = bytestream_get_byte(&buf);
  1151. dds_version = info_byte >> 4;
  1152. if (display_def && display_def->version == dds_version)
  1153. return; // already have this display definition version
  1154. if (!display_def) {
  1155. display_def = av_mallocz(sizeof(*display_def));
  1156. ctx->display_definition = display_def;
  1157. }
  1158. if (!display_def)
  1159. return;
  1160. display_def->version = dds_version;
  1161. display_def->x = 0;
  1162. display_def->y = 0;
  1163. display_def->width = bytestream_get_be16(&buf) + 1;
  1164. display_def->height = bytestream_get_be16(&buf) + 1;
  1165. if (!avctx->width || !avctx->height) {
  1166. avctx->width = display_def->width;
  1167. avctx->height = display_def->height;
  1168. }
  1169. if (buf_size < 13)
  1170. return;
  1171. if (info_byte & 1<<3) { // display_window_flag
  1172. display_def->x = bytestream_get_be16(&buf);
  1173. display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
  1174. display_def->y = bytestream_get_be16(&buf);
  1175. display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
  1176. }
  1177. }
  1178. static void dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1179. int buf_size, AVSubtitle *sub,int *got_output)
  1180. {
  1181. DVBSubContext *ctx = avctx->priv_data;
  1182. if(ctx->compute_edt == 0)
  1183. save_subtitle_set(avctx, sub, got_output);
  1184. #ifdef DEBUG
  1185. save_display_set(ctx);
  1186. #endif
  1187. }
  1188. static int dvbsub_decode(AVCodecContext *avctx,
  1189. void *data, int *data_size,
  1190. AVPacket *avpkt)
  1191. {
  1192. const uint8_t *buf = avpkt->data;
  1193. int buf_size = avpkt->size;
  1194. DVBSubContext *ctx = avctx->priv_data;
  1195. AVSubtitle *sub = data;
  1196. const uint8_t *p, *p_end;
  1197. int segment_type;
  1198. int page_id;
  1199. int segment_length;
  1200. int i;
  1201. int ret = 0;
  1202. int got_segment = 0;
  1203. av_dlog(avctx, "DVB sub packet:\n");
  1204. for (i=0; i < buf_size; i++) {
  1205. av_dlog(avctx, "%02x ", buf[i]);
  1206. if (i % 16 == 15)
  1207. av_dlog(avctx, "\n");
  1208. }
  1209. if (i % 16)
  1210. av_dlog(avctx, "\n");
  1211. if (buf_size <= 6 || *buf != 0x0f) {
  1212. av_dlog(avctx, "incomplete or broken packet");
  1213. return -1;
  1214. }
  1215. p = buf;
  1216. p_end = buf + buf_size;
  1217. while (p_end - p >= 6 && *p == 0x0f) {
  1218. p += 1;
  1219. segment_type = *p++;
  1220. page_id = AV_RB16(p);
  1221. p += 2;
  1222. segment_length = AV_RB16(p);
  1223. p += 2;
  1224. if (avctx->debug & FF_DEBUG_STARTCODE) {
  1225. av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
  1226. }
  1227. if (p_end - p < segment_length) {
  1228. av_dlog(avctx, "incomplete or broken packet");
  1229. ret = -1;
  1230. goto end;
  1231. }
  1232. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
  1233. ctx->composition_id == -1 || ctx->ancillary_id == -1) {
  1234. switch (segment_type) {
  1235. case DVBSUB_PAGE_SEGMENT:
  1236. dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
  1237. got_segment |= 1;
  1238. break;
  1239. case DVBSUB_REGION_SEGMENT:
  1240. dvbsub_parse_region_segment(avctx, p, segment_length);
  1241. got_segment |= 2;
  1242. break;
  1243. case DVBSUB_CLUT_SEGMENT:
  1244. ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
  1245. if (ret < 0) goto end;
  1246. got_segment |= 4;
  1247. break;
  1248. case DVBSUB_OBJECT_SEGMENT:
  1249. dvbsub_parse_object_segment(avctx, p, segment_length);
  1250. got_segment |= 8;
  1251. break;
  1252. case DVBSUB_DISPLAYDEFINITION_SEGMENT:
  1253. dvbsub_parse_display_definition_segment(avctx, p, segment_length);
  1254. break;
  1255. case DVBSUB_DISPLAY_SEGMENT:
  1256. dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
  1257. got_segment |= 16;
  1258. break;
  1259. default:
  1260. av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1261. segment_type, page_id, segment_length);
  1262. break;
  1263. }
  1264. }
  1265. p += segment_length;
  1266. }
  1267. // Some streams do not send a display segment but if we have all the other
  1268. // segments then we need no further data.
  1269. if (got_segment == 15) {
  1270. av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
  1271. dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
  1272. }
  1273. end:
  1274. if(ret < 0) {
  1275. *data_size = 0;
  1276. avsubtitle_free(sub);
  1277. return ret;
  1278. } else {
  1279. if(ctx->compute_edt == 1 )
  1280. FFSWAP(int64_t, ctx->prev_start, sub->pts);
  1281. }
  1282. return p - buf;
  1283. }
  1284. #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
  1285. static const AVOption options[] = {
  1286. {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DS},
  1287. {NULL}
  1288. };
  1289. static const AVClass dvbsubdec_class = {
  1290. .class_name = "DVB Sub Decoder",
  1291. .item_name = av_default_item_name,
  1292. .option = options,
  1293. .version = LIBAVUTIL_VERSION_INT,
  1294. };
  1295. AVCodec ff_dvbsub_decoder = {
  1296. .name = "dvbsub",
  1297. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1298. .type = AVMEDIA_TYPE_SUBTITLE,
  1299. .id = AV_CODEC_ID_DVB_SUBTITLE,
  1300. .priv_data_size = sizeof(DVBSubContext),
  1301. .init = dvbsub_init_decoder,
  1302. .close = dvbsub_close_decoder,
  1303. .decode = dvbsub_decode,
  1304. .priv_class = &dvbsubdec_class,
  1305. };