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.

1605 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/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 void 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;
  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. for(i=0; i<sub->num_rects; i++)
  662. sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
  663. i = 0;
  664. for (display = ctx->display_list; display; display = display->next) {
  665. region = get_region(ctx, display->region_id);
  666. if (!region)
  667. continue;
  668. if (!region->dirty)
  669. continue;
  670. rect = sub->rects[i];
  671. rect->x = display->x_pos + offset_x;
  672. rect->y = display->y_pos + offset_y;
  673. rect->w = region->width;
  674. rect->h = region->height;
  675. rect->nb_colors = (1 << region->depth);
  676. rect->type = SUBTITLE_BITMAP;
  677. rect->pict.linesize[0] = region->width;
  678. clut = get_clut(ctx, region->clut);
  679. if (!clut)
  680. clut = &default_clut;
  681. switch (region->depth) {
  682. case 2:
  683. clut_table = clut->clut4;
  684. break;
  685. case 8:
  686. clut_table = clut->clut256;
  687. break;
  688. case 4:
  689. default:
  690. clut_table = clut->clut16;
  691. break;
  692. }
  693. rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  694. memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
  695. rect->pict.data[0] = av_malloc(region->buf_size);
  696. memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
  697. i++;
  698. }
  699. }
  700. }
  701. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  702. const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  703. {
  704. DVBSubContext *ctx = avctx->priv_data;
  705. DVBSubRegion *region = get_region(ctx, display->region_id);
  706. const uint8_t *buf_end = buf + buf_size;
  707. uint8_t *pbuf;
  708. int x_pos, y_pos;
  709. int i;
  710. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  711. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  712. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  713. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  714. uint8_t *map_table;
  715. #if 0
  716. av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
  717. top_bottom ? "bottom" : "top");
  718. for (i = 0; i < buf_size; i++) {
  719. if (i % 16 == 0)
  720. av_dlog(avctx, "0x%8p: ", buf+i);
  721. av_dlog(avctx, "%02x ", buf[i]);
  722. if (i % 16 == 15)
  723. av_dlog(avctx, "\n");
  724. }
  725. if (i % 16)
  726. av_dlog(avctx, "\n");
  727. #endif
  728. if (!region)
  729. return;
  730. pbuf = region->pbuf;
  731. region->dirty = 1;
  732. x_pos = display->x_pos;
  733. y_pos = display->y_pos;
  734. y_pos += top_bottom;
  735. while (buf < buf_end) {
  736. if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
  737. av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
  738. return;
  739. }
  740. switch (*buf++) {
  741. case 0x10:
  742. if (region->depth == 8)
  743. map_table = map2to8;
  744. else if (region->depth == 4)
  745. map_table = map2to4;
  746. else
  747. map_table = NULL;
  748. x_pos = dvbsub_read_2bit_string(pbuf + (y_pos * region->width),
  749. region->width, &buf, buf_end - buf,
  750. non_mod, map_table, x_pos);
  751. break;
  752. case 0x11:
  753. if (region->depth < 4) {
  754. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  755. return;
  756. }
  757. if (region->depth == 8)
  758. map_table = map4to8;
  759. else
  760. map_table = NULL;
  761. x_pos = dvbsub_read_4bit_string(pbuf + (y_pos * region->width),
  762. region->width, &buf, buf_end - buf,
  763. non_mod, map_table, x_pos);
  764. break;
  765. case 0x12:
  766. if (region->depth < 8) {
  767. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  768. return;
  769. }
  770. x_pos = dvbsub_read_8bit_string(pbuf + (y_pos * region->width),
  771. region->width, &buf, buf_end - buf,
  772. non_mod, NULL, x_pos);
  773. break;
  774. case 0x20:
  775. map2to4[0] = (*buf) >> 4;
  776. map2to4[1] = (*buf++) & 0xf;
  777. map2to4[2] = (*buf) >> 4;
  778. map2to4[3] = (*buf++) & 0xf;
  779. break;
  780. case 0x21:
  781. for (i = 0; i < 4; i++)
  782. map2to8[i] = *buf++;
  783. break;
  784. case 0x22:
  785. for (i = 0; i < 16; i++)
  786. map4to8[i] = *buf++;
  787. break;
  788. case 0xf0:
  789. x_pos = display->x_pos;
  790. y_pos += 2;
  791. break;
  792. default:
  793. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  794. }
  795. }
  796. }
  797. static void dvbsub_parse_object_segment(AVCodecContext *avctx,
  798. const uint8_t *buf, int buf_size)
  799. {
  800. DVBSubContext *ctx = avctx->priv_data;
  801. const uint8_t *buf_end = buf + buf_size;
  802. int object_id;
  803. DVBSubObject *object;
  804. DVBSubObjectDisplay *display;
  805. int top_field_len, bottom_field_len;
  806. int coding_method, non_modifying_color;
  807. object_id = AV_RB16(buf);
  808. buf += 2;
  809. object = get_object(ctx, object_id);
  810. if (!object)
  811. return;
  812. coding_method = ((*buf) >> 2) & 3;
  813. non_modifying_color = ((*buf++) >> 1) & 1;
  814. if (coding_method == 0) {
  815. top_field_len = AV_RB16(buf);
  816. buf += 2;
  817. bottom_field_len = AV_RB16(buf);
  818. buf += 2;
  819. if (buf + top_field_len + bottom_field_len > buf_end) {
  820. av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
  821. return;
  822. }
  823. for (display = object->display_list; display; display = display->object_list_next) {
  824. const uint8_t *block = buf;
  825. int bfl = bottom_field_len;
  826. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  827. non_modifying_color);
  828. if (bottom_field_len > 0)
  829. block = buf + top_field_len;
  830. else
  831. bfl = top_field_len;
  832. dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
  833. non_modifying_color);
  834. }
  835. /* } else if (coding_method == 1) {*/
  836. } else {
  837. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  838. }
  839. }
  840. static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
  841. const uint8_t *buf, int buf_size)
  842. {
  843. DVBSubContext *ctx = avctx->priv_data;
  844. const uint8_t *buf_end = buf + buf_size;
  845. int i, clut_id;
  846. int version;
  847. DVBSubCLUT *clut;
  848. int entry_id, depth , full_range;
  849. int y, cr, cb, alpha;
  850. int r, g, b, r_add, g_add, b_add;
  851. av_dlog(avctx, "DVB clut packet:\n");
  852. for (i=0; i < buf_size; i++) {
  853. av_dlog(avctx, "%02x ", buf[i]);
  854. if (i % 16 == 15)
  855. av_dlog(avctx, "\n");
  856. }
  857. if (i % 16)
  858. av_dlog(avctx, "\n");
  859. clut_id = *buf++;
  860. version = ((*buf)>>4)&15;
  861. buf += 1;
  862. clut = get_clut(ctx, clut_id);
  863. if (!clut) {
  864. clut = av_malloc(sizeof(DVBSubCLUT));
  865. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  866. clut->id = clut_id;
  867. clut->version = -1;
  868. clut->next = ctx->clut_list;
  869. ctx->clut_list = clut;
  870. }
  871. if (clut->version != version) {
  872. clut->version = version;
  873. while (buf + 4 < buf_end) {
  874. entry_id = *buf++;
  875. depth = (*buf) & 0xe0;
  876. if (depth == 0) {
  877. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  878. return 0;
  879. }
  880. full_range = (*buf++) & 1;
  881. if (full_range) {
  882. y = *buf++;
  883. cr = *buf++;
  884. cb = *buf++;
  885. alpha = *buf++;
  886. } else {
  887. y = buf[0] & 0xfc;
  888. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  889. cb = (buf[1] << 2) & 0xf0;
  890. alpha = (buf[1] << 6) & 0xc0;
  891. buf += 2;
  892. }
  893. if (y == 0)
  894. alpha = 0xff;
  895. YUV_TO_RGB1_CCIR(cb, cr);
  896. YUV_TO_RGB2_CCIR(r, g, b, y);
  897. av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  898. if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
  899. av_dlog(avctx, "More than one bit level marked: %x\n", depth);
  900. if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
  901. return AVERROR_INVALIDDATA;
  902. }
  903. if (depth & 0x80)
  904. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  905. else if (depth & 0x40)
  906. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  907. else if (depth & 0x20)
  908. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  909. }
  910. }
  911. return 0;
  912. }
  913. static void dvbsub_parse_region_segment(AVCodecContext *avctx,
  914. const uint8_t *buf, int buf_size)
  915. {
  916. DVBSubContext *ctx = avctx->priv_data;
  917. const uint8_t *buf_end = buf + buf_size;
  918. int region_id, object_id;
  919. int av_unused version;
  920. DVBSubRegion *region;
  921. DVBSubObject *object;
  922. DVBSubObjectDisplay *display;
  923. int fill;
  924. if (buf_size < 10)
  925. return;
  926. region_id = *buf++;
  927. region = get_region(ctx, region_id);
  928. if (!region) {
  929. region = av_mallocz(sizeof(DVBSubRegion));
  930. region->id = region_id;
  931. region->version = -1;
  932. region->next = ctx->region_list;
  933. ctx->region_list = region;
  934. }
  935. version = ((*buf)>>4) & 15;
  936. fill = ((*buf++) >> 3) & 1;
  937. region->width = AV_RB16(buf);
  938. buf += 2;
  939. region->height = AV_RB16(buf);
  940. buf += 2;
  941. if (region->width * region->height != region->buf_size) {
  942. av_free(region->pbuf);
  943. region->buf_size = region->width * region->height;
  944. region->pbuf = av_malloc(region->buf_size);
  945. fill = 1;
  946. region->dirty = 0;
  947. }
  948. region->depth = 1 << (((*buf++) >> 2) & 7);
  949. if(region->depth<2 || region->depth>8){
  950. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  951. region->depth= 4;
  952. }
  953. region->clut = *buf++;
  954. if (region->depth == 8) {
  955. region->bgcolor = *buf++;
  956. buf += 1;
  957. } else {
  958. buf += 1;
  959. if (region->depth == 4)
  960. region->bgcolor = (((*buf++) >> 4) & 15);
  961. else
  962. region->bgcolor = (((*buf++) >> 2) & 3);
  963. }
  964. av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  965. if (fill) {
  966. memset(region->pbuf, region->bgcolor, region->buf_size);
  967. av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
  968. }
  969. delete_region_display_list(ctx, region);
  970. while (buf + 5 < buf_end) {
  971. object_id = AV_RB16(buf);
  972. buf += 2;
  973. object = get_object(ctx, object_id);
  974. if (!object) {
  975. object = av_mallocz(sizeof(DVBSubObject));
  976. object->id = object_id;
  977. object->next = ctx->object_list;
  978. ctx->object_list = object;
  979. }
  980. object->type = (*buf) >> 6;
  981. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  982. display->object_id = object_id;
  983. display->region_id = region_id;
  984. display->x_pos = AV_RB16(buf) & 0xfff;
  985. buf += 2;
  986. display->y_pos = AV_RB16(buf) & 0xfff;
  987. buf += 2;
  988. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  989. display->fgcolor = *buf++;
  990. display->bgcolor = *buf++;
  991. }
  992. display->region_list_next = region->display_list;
  993. region->display_list = display;
  994. display->object_list_next = object->display_list;
  995. object->display_list = display;
  996. }
  997. }
  998. static void dvbsub_parse_page_segment(AVCodecContext *avctx,
  999. const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
  1000. {
  1001. DVBSubContext *ctx = avctx->priv_data;
  1002. DVBSubRegionDisplay *display;
  1003. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  1004. const uint8_t *buf_end = buf + buf_size;
  1005. int region_id;
  1006. int page_state;
  1007. int timeout;
  1008. int version;
  1009. if (buf_size < 1)
  1010. return;
  1011. timeout = *buf++;
  1012. version = ((*buf)>>4) & 15;
  1013. page_state = ((*buf++) >> 2) & 3;
  1014. if (ctx->version == version) {
  1015. return;
  1016. }
  1017. ctx->time_out = timeout;
  1018. ctx->version = version;
  1019. av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  1020. if(ctx->compute_edt == 1)
  1021. save_subtitle_set(avctx, sub, got_output);
  1022. if (page_state == 1 || page_state == 2) {
  1023. delete_regions(ctx);
  1024. delete_objects(ctx);
  1025. delete_cluts(ctx);
  1026. }
  1027. tmp_display_list = ctx->display_list;
  1028. ctx->display_list = NULL;
  1029. while (buf + 5 < buf_end) {
  1030. region_id = *buf++;
  1031. buf += 1;
  1032. display = tmp_display_list;
  1033. tmp_ptr = &tmp_display_list;
  1034. while (display && display->region_id != region_id) {
  1035. tmp_ptr = &display->next;
  1036. display = display->next;
  1037. }
  1038. if (!display)
  1039. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  1040. display->region_id = region_id;
  1041. display->x_pos = AV_RB16(buf);
  1042. buf += 2;
  1043. display->y_pos = AV_RB16(buf);
  1044. buf += 2;
  1045. *tmp_ptr = display->next;
  1046. display->next = ctx->display_list;
  1047. ctx->display_list = display;
  1048. av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  1049. }
  1050. while (tmp_display_list) {
  1051. display = tmp_display_list;
  1052. tmp_display_list = display->next;
  1053. av_freep(&display);
  1054. }
  1055. }
  1056. #ifdef DEBUG
  1057. static void save_display_set(DVBSubContext *ctx)
  1058. {
  1059. DVBSubRegion *region;
  1060. DVBSubRegionDisplay *display;
  1061. DVBSubCLUT *clut;
  1062. uint32_t *clut_table;
  1063. int x_pos, y_pos, width, height;
  1064. int x, y, y_off, x_off;
  1065. uint32_t *pbuf;
  1066. char filename[32];
  1067. static int fileno_index = 0;
  1068. x_pos = -1;
  1069. y_pos = -1;
  1070. width = 0;
  1071. height = 0;
  1072. for (display = ctx->display_list; display; display = display->next) {
  1073. region = get_region(ctx, display->region_id);
  1074. if (x_pos == -1) {
  1075. x_pos = display->x_pos;
  1076. y_pos = display->y_pos;
  1077. width = region->width;
  1078. height = region->height;
  1079. } else {
  1080. if (display->x_pos < x_pos) {
  1081. width += (x_pos - display->x_pos);
  1082. x_pos = display->x_pos;
  1083. }
  1084. if (display->y_pos < y_pos) {
  1085. height += (y_pos - display->y_pos);
  1086. y_pos = display->y_pos;
  1087. }
  1088. if (display->x_pos + region->width > x_pos + width) {
  1089. width = display->x_pos + region->width - x_pos;
  1090. }
  1091. if (display->y_pos + region->height > y_pos + height) {
  1092. height = display->y_pos + region->height - y_pos;
  1093. }
  1094. }
  1095. }
  1096. if (x_pos >= 0) {
  1097. pbuf = av_malloc(width * height * 4);
  1098. for (display = ctx->display_list; display; display = display->next) {
  1099. region = get_region(ctx, display->region_id);
  1100. x_off = display->x_pos - x_pos;
  1101. y_off = display->y_pos - y_pos;
  1102. clut = get_clut(ctx, region->clut);
  1103. if (!clut)
  1104. clut = &default_clut;
  1105. switch (region->depth) {
  1106. case 2:
  1107. clut_table = clut->clut4;
  1108. break;
  1109. case 8:
  1110. clut_table = clut->clut256;
  1111. break;
  1112. case 4:
  1113. default:
  1114. clut_table = clut->clut16;
  1115. break;
  1116. }
  1117. for (y = 0; y < region->height; y++) {
  1118. for (x = 0; x < region->width; x++) {
  1119. pbuf[((y + y_off) * width) + x_off + x] =
  1120. clut_table[region->pbuf[y * region->width + x]];
  1121. }
  1122. }
  1123. }
  1124. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  1125. png_save2(filename, pbuf, width, height);
  1126. av_freep(&pbuf);
  1127. }
  1128. fileno_index++;
  1129. }
  1130. #endif
  1131. static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
  1132. const uint8_t *buf,
  1133. int buf_size)
  1134. {
  1135. DVBSubContext *ctx = avctx->priv_data;
  1136. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1137. int dds_version, info_byte;
  1138. if (buf_size < 5)
  1139. return;
  1140. info_byte = bytestream_get_byte(&buf);
  1141. dds_version = info_byte >> 4;
  1142. if (display_def && display_def->version == dds_version)
  1143. return; // already have this display definition version
  1144. if (!display_def) {
  1145. display_def = av_mallocz(sizeof(*display_def));
  1146. ctx->display_definition = display_def;
  1147. }
  1148. if (!display_def)
  1149. return;
  1150. display_def->version = dds_version;
  1151. display_def->x = 0;
  1152. display_def->y = 0;
  1153. display_def->width = bytestream_get_be16(&buf) + 1;
  1154. display_def->height = bytestream_get_be16(&buf) + 1;
  1155. if (!avctx->width || !avctx->height) {
  1156. avctx->width = display_def->width;
  1157. avctx->height = display_def->height;
  1158. }
  1159. if (buf_size < 13)
  1160. return;
  1161. if (info_byte & 1<<3) { // display_window_flag
  1162. display_def->x = bytestream_get_be16(&buf);
  1163. display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
  1164. display_def->y = bytestream_get_be16(&buf);
  1165. display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
  1166. }
  1167. }
  1168. static void dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1169. int buf_size, AVSubtitle *sub,int *got_output)
  1170. {
  1171. DVBSubContext *ctx = avctx->priv_data;
  1172. if(ctx->compute_edt == 0)
  1173. save_subtitle_set(avctx, sub, got_output);
  1174. #ifdef DEBUG
  1175. save_display_set(ctx);
  1176. #endif
  1177. }
  1178. static int dvbsub_decode(AVCodecContext *avctx,
  1179. void *data, int *data_size,
  1180. AVPacket *avpkt)
  1181. {
  1182. const uint8_t *buf = avpkt->data;
  1183. int buf_size = avpkt->size;
  1184. DVBSubContext *ctx = avctx->priv_data;
  1185. AVSubtitle *sub = data;
  1186. const uint8_t *p, *p_end;
  1187. int segment_type;
  1188. int page_id;
  1189. int segment_length;
  1190. int i;
  1191. int ret = 0;
  1192. int got_segment = 0;
  1193. av_dlog(avctx, "DVB sub packet:\n");
  1194. for (i=0; i < buf_size; i++) {
  1195. av_dlog(avctx, "%02x ", buf[i]);
  1196. if (i % 16 == 15)
  1197. av_dlog(avctx, "\n");
  1198. }
  1199. if (i % 16)
  1200. av_dlog(avctx, "\n");
  1201. if (buf_size <= 6 || *buf != 0x0f) {
  1202. av_dlog(avctx, "incomplete or broken packet");
  1203. return -1;
  1204. }
  1205. p = buf;
  1206. p_end = buf + buf_size;
  1207. while (p_end - p >= 6 && *p == 0x0f) {
  1208. p += 1;
  1209. segment_type = *p++;
  1210. page_id = AV_RB16(p);
  1211. p += 2;
  1212. segment_length = AV_RB16(p);
  1213. p += 2;
  1214. if (avctx->debug & FF_DEBUG_STARTCODE) {
  1215. av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
  1216. }
  1217. if (p_end - p < segment_length) {
  1218. av_dlog(avctx, "incomplete or broken packet");
  1219. ret = -1;
  1220. goto end;
  1221. }
  1222. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
  1223. ctx->composition_id == -1 || ctx->ancillary_id == -1) {
  1224. switch (segment_type) {
  1225. case DVBSUB_PAGE_SEGMENT:
  1226. dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
  1227. got_segment |= 1;
  1228. break;
  1229. case DVBSUB_REGION_SEGMENT:
  1230. dvbsub_parse_region_segment(avctx, p, segment_length);
  1231. got_segment |= 2;
  1232. break;
  1233. case DVBSUB_CLUT_SEGMENT:
  1234. ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
  1235. if (ret < 0) goto end;
  1236. got_segment |= 4;
  1237. break;
  1238. case DVBSUB_OBJECT_SEGMENT:
  1239. dvbsub_parse_object_segment(avctx, p, segment_length);
  1240. got_segment |= 8;
  1241. break;
  1242. case DVBSUB_DISPLAYDEFINITION_SEGMENT:
  1243. dvbsub_parse_display_definition_segment(avctx, p, segment_length);
  1244. break;
  1245. case DVBSUB_DISPLAY_SEGMENT:
  1246. dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
  1247. got_segment |= 16;
  1248. break;
  1249. default:
  1250. av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1251. segment_type, page_id, segment_length);
  1252. break;
  1253. }
  1254. }
  1255. p += segment_length;
  1256. }
  1257. // Some streams do not send a display segment but if we have all the other
  1258. // segments then we need no further data.
  1259. if (got_segment == 15) {
  1260. av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
  1261. dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
  1262. }
  1263. end:
  1264. if(ret < 0) {
  1265. *data_size = 0;
  1266. avsubtitle_free(sub);
  1267. return ret;
  1268. } else {
  1269. if(ctx->compute_edt == 1 )
  1270. FFSWAP(int64_t, ctx->prev_start, sub->pts);
  1271. }
  1272. return p - buf;
  1273. }
  1274. #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
  1275. static const AVOption options[] = {
  1276. {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DS},
  1277. {NULL}
  1278. };
  1279. static const AVClass dvbsubdec_class = {
  1280. .class_name = "DVB Sub Decoder",
  1281. .item_name = av_default_item_name,
  1282. .option = options,
  1283. .version = LIBAVUTIL_VERSION_INT,
  1284. };
  1285. AVCodec ff_dvbsub_decoder = {
  1286. .name = "dvbsub",
  1287. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1288. .type = AVMEDIA_TYPE_SUBTITLE,
  1289. .id = AV_CODEC_ID_DVB_SUBTITLE,
  1290. .priv_data_size = sizeof(DVBSubContext),
  1291. .init = dvbsub_init_decoder,
  1292. .close = dvbsub_close_decoder,
  1293. .decode = dvbsub_decode,
  1294. .priv_class = &dvbsubdec_class,
  1295. };