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.

1620 lines
45KB

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