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.

1647 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. av_assert0(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(AVCodecContext *avctx,
  375. uint8_t *destbuf, int dbuf_len,
  376. const uint8_t **srcbuf, int buf_size,
  377. int non_mod, uint8_t *map_table, int x_pos)
  378. {
  379. GetBitContext gb;
  380. int bits;
  381. int run_length;
  382. int pixels_read = x_pos;
  383. init_get_bits(&gb, *srcbuf, buf_size << 3);
  384. destbuf += x_pos;
  385. while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
  386. bits = get_bits(&gb, 2);
  387. if (bits) {
  388. if (non_mod != 1 || bits != 1) {
  389. if (map_table)
  390. *destbuf++ = map_table[bits];
  391. else
  392. *destbuf++ = bits;
  393. }
  394. pixels_read++;
  395. } else {
  396. bits = get_bits1(&gb);
  397. if (bits == 1) {
  398. run_length = get_bits(&gb, 3) + 3;
  399. bits = get_bits(&gb, 2);
  400. if (non_mod == 1 && bits == 1)
  401. pixels_read += run_length;
  402. else {
  403. if (map_table)
  404. bits = map_table[bits];
  405. while (run_length-- > 0 && pixels_read < dbuf_len) {
  406. *destbuf++ = bits;
  407. pixels_read++;
  408. }
  409. }
  410. } else {
  411. bits = get_bits1(&gb);
  412. if (bits == 0) {
  413. bits = get_bits(&gb, 2);
  414. if (bits == 2) {
  415. run_length = get_bits(&gb, 4) + 12;
  416. bits = get_bits(&gb, 2);
  417. if (non_mod == 1 && bits == 1)
  418. pixels_read += run_length;
  419. else {
  420. if (map_table)
  421. bits = map_table[bits];
  422. while (run_length-- > 0 && pixels_read < dbuf_len) {
  423. *destbuf++ = bits;
  424. pixels_read++;
  425. }
  426. }
  427. } else if (bits == 3) {
  428. run_length = get_bits(&gb, 8) + 29;
  429. bits = get_bits(&gb, 2);
  430. if (non_mod == 1 && bits == 1)
  431. pixels_read += run_length;
  432. else {
  433. if (map_table)
  434. bits = map_table[bits];
  435. while (run_length-- > 0 && pixels_read < dbuf_len) {
  436. *destbuf++ = bits;
  437. pixels_read++;
  438. }
  439. }
  440. } else if (bits == 1) {
  441. if (map_table)
  442. bits = map_table[0];
  443. else
  444. bits = 0;
  445. run_length = 2;
  446. while (run_length-- > 0 && pixels_read < dbuf_len) {
  447. *destbuf++ = bits;
  448. pixels_read++;
  449. }
  450. } else {
  451. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  452. return pixels_read;
  453. }
  454. } else {
  455. if (map_table)
  456. bits = map_table[0];
  457. else
  458. bits = 0;
  459. *destbuf++ = bits;
  460. pixels_read++;
  461. }
  462. }
  463. }
  464. }
  465. if (get_bits(&gb, 6))
  466. av_log(avctx, AV_LOG_ERROR, "line overflow\n");
  467. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  468. return pixels_read;
  469. }
  470. static int dvbsub_read_4bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len,
  471. const uint8_t **srcbuf, int buf_size,
  472. int non_mod, uint8_t *map_table, int x_pos)
  473. {
  474. GetBitContext gb;
  475. int bits;
  476. int run_length;
  477. int pixels_read = x_pos;
  478. init_get_bits(&gb, *srcbuf, buf_size << 3);
  479. destbuf += x_pos;
  480. while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
  481. bits = get_bits(&gb, 4);
  482. if (bits) {
  483. if (non_mod != 1 || bits != 1) {
  484. if (map_table)
  485. *destbuf++ = map_table[bits];
  486. else
  487. *destbuf++ = bits;
  488. }
  489. pixels_read++;
  490. } else {
  491. bits = get_bits1(&gb);
  492. if (bits == 0) {
  493. run_length = get_bits(&gb, 3);
  494. if (run_length == 0) {
  495. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  496. return pixels_read;
  497. }
  498. run_length += 2;
  499. if (map_table)
  500. bits = map_table[0];
  501. else
  502. bits = 0;
  503. while (run_length-- > 0 && pixels_read < dbuf_len) {
  504. *destbuf++ = bits;
  505. pixels_read++;
  506. }
  507. } else {
  508. bits = get_bits1(&gb);
  509. if (bits == 0) {
  510. run_length = get_bits(&gb, 2) + 4;
  511. bits = get_bits(&gb, 4);
  512. if (non_mod == 1 && bits == 1)
  513. pixels_read += run_length;
  514. else {
  515. if (map_table)
  516. bits = map_table[bits];
  517. while (run_length-- > 0 && pixels_read < dbuf_len) {
  518. *destbuf++ = bits;
  519. pixels_read++;
  520. }
  521. }
  522. } else {
  523. bits = get_bits(&gb, 2);
  524. if (bits == 2) {
  525. run_length = get_bits(&gb, 4) + 9;
  526. bits = get_bits(&gb, 4);
  527. if (non_mod == 1 && bits == 1)
  528. pixels_read += run_length;
  529. else {
  530. if (map_table)
  531. bits = map_table[bits];
  532. while (run_length-- > 0 && pixels_read < dbuf_len) {
  533. *destbuf++ = bits;
  534. pixels_read++;
  535. }
  536. }
  537. } else if (bits == 3) {
  538. run_length = get_bits(&gb, 8) + 25;
  539. bits = get_bits(&gb, 4);
  540. if (non_mod == 1 && bits == 1)
  541. pixels_read += run_length;
  542. else {
  543. if (map_table)
  544. bits = map_table[bits];
  545. while (run_length-- > 0 && pixels_read < dbuf_len) {
  546. *destbuf++ = bits;
  547. pixels_read++;
  548. }
  549. }
  550. } else if (bits == 1) {
  551. if (map_table)
  552. bits = map_table[0];
  553. else
  554. bits = 0;
  555. run_length = 2;
  556. while (run_length-- > 0 && pixels_read < dbuf_len) {
  557. *destbuf++ = bits;
  558. pixels_read++;
  559. }
  560. } else {
  561. if (map_table)
  562. bits = map_table[0];
  563. else
  564. bits = 0;
  565. *destbuf++ = bits;
  566. pixels_read ++;
  567. }
  568. }
  569. }
  570. }
  571. }
  572. if (get_bits(&gb, 8))
  573. av_log(avctx, AV_LOG_ERROR, "line overflow\n");
  574. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  575. return pixels_read;
  576. }
  577. static int dvbsub_read_8bit_string(AVCodecContext *avctx,
  578. uint8_t *destbuf, int dbuf_len,
  579. const uint8_t **srcbuf, int buf_size,
  580. int non_mod, uint8_t *map_table, int x_pos)
  581. {
  582. const uint8_t *sbuf_end = (*srcbuf) + buf_size;
  583. int bits;
  584. int run_length;
  585. int pixels_read = x_pos;
  586. destbuf += x_pos;
  587. while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
  588. bits = *(*srcbuf)++;
  589. if (bits) {
  590. if (non_mod != 1 || bits != 1) {
  591. if (map_table)
  592. *destbuf++ = map_table[bits];
  593. else
  594. *destbuf++ = bits;
  595. }
  596. pixels_read++;
  597. } else {
  598. bits = *(*srcbuf)++;
  599. run_length = bits & 0x7f;
  600. if ((bits & 0x80) == 0) {
  601. if (run_length == 0) {
  602. return pixels_read;
  603. }
  604. bits = 0;
  605. } else {
  606. bits = *(*srcbuf)++;
  607. }
  608. if (non_mod == 1 && bits == 1)
  609. pixels_read += run_length;
  610. else {
  611. if (map_table)
  612. bits = map_table[bits];
  613. while (run_length-- > 0 && pixels_read < dbuf_len) {
  614. *destbuf++ = bits;
  615. pixels_read++;
  616. }
  617. }
  618. }
  619. }
  620. if (*(*srcbuf)++)
  621. av_log(avctx, AV_LOG_ERROR, "line overflow\n");
  622. return pixels_read;
  623. }
  624. static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
  625. {
  626. DVBSubContext *ctx = avctx->priv_data;
  627. DVBSubRegionDisplay *display;
  628. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  629. DVBSubRegion *region;
  630. AVSubtitleRect *rect;
  631. DVBSubCLUT *clut;
  632. uint32_t *clut_table;
  633. int i;
  634. int offset_x=0, offset_y=0;
  635. int ret = 0;
  636. if (display_def) {
  637. offset_x = display_def->x;
  638. offset_y = display_def->y;
  639. }
  640. /* Not touching AVSubtitles again*/
  641. if(sub->num_rects) {
  642. avpriv_request_sample(ctx, "Different Version of Segment asked Twice\n");
  643. return AVERROR_PATCHWELCOME;
  644. }
  645. for (display = ctx->display_list; display; display = display->next) {
  646. region = get_region(ctx, display->region_id);
  647. if (region && region->dirty)
  648. sub->num_rects++;
  649. }
  650. if(ctx->compute_edt == 0) {
  651. sub->end_display_time = ctx->time_out * 1000;
  652. *got_output = 1;
  653. } else if (ctx->prev_start != AV_NOPTS_VALUE) {
  654. sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
  655. *got_output = 1;
  656. }
  657. if (sub->num_rects > 0) {
  658. sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
  659. if (!sub->rects) {
  660. ret = AVERROR(ENOMEM);
  661. goto fail;
  662. }
  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. ret = AVERROR(ENOMEM);
  698. goto fail;
  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. ret = AVERROR(ENOMEM);
  704. goto fail;
  705. }
  706. memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
  707. i++;
  708. }
  709. }
  710. return 0;
  711. fail:
  712. if (sub->rects) {
  713. for(i=0; i<sub->num_rects; i++) {
  714. rect = sub->rects[i];
  715. if (rect) {
  716. av_freep(&rect->pict.data[0]);
  717. av_freep(&rect->pict.data[1]);
  718. }
  719. av_freep(&sub->rects[i]);
  720. }
  721. av_freep(&sub->rects);
  722. }
  723. sub->num_rects = 0;
  724. return ret;
  725. }
  726. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  727. const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  728. {
  729. DVBSubContext *ctx = avctx->priv_data;
  730. DVBSubRegion *region = get_region(ctx, display->region_id);
  731. const uint8_t *buf_end = buf + buf_size;
  732. uint8_t *pbuf;
  733. int x_pos, y_pos;
  734. int i;
  735. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  736. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  737. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  738. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  739. uint8_t *map_table;
  740. #if 0
  741. av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
  742. top_bottom ? "bottom" : "top");
  743. for (i = 0; i < buf_size; i++) {
  744. if (i % 16 == 0)
  745. av_dlog(avctx, "0x%8p: ", buf+i);
  746. av_dlog(avctx, "%02x ", buf[i]);
  747. if (i % 16 == 15)
  748. av_dlog(avctx, "\n");
  749. }
  750. if (i % 16)
  751. av_dlog(avctx, "\n");
  752. #endif
  753. if (!region)
  754. return;
  755. pbuf = region->pbuf;
  756. region->dirty = 1;
  757. x_pos = display->x_pos;
  758. y_pos = display->y_pos;
  759. y_pos += top_bottom;
  760. while (buf < buf_end) {
  761. if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
  762. av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
  763. return;
  764. }
  765. switch (*buf++) {
  766. case 0x10:
  767. if (region->depth == 8)
  768. map_table = map2to8;
  769. else if (region->depth == 4)
  770. map_table = map2to4;
  771. else
  772. map_table = NULL;
  773. x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
  774. region->width, &buf, buf_end - buf,
  775. non_mod, map_table, x_pos);
  776. break;
  777. case 0x11:
  778. if (region->depth < 4) {
  779. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  780. return;
  781. }
  782. if (region->depth == 8)
  783. map_table = map4to8;
  784. else
  785. map_table = NULL;
  786. x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
  787. region->width, &buf, buf_end - buf,
  788. non_mod, map_table, x_pos);
  789. break;
  790. case 0x12:
  791. if (region->depth < 8) {
  792. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  793. return;
  794. }
  795. x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
  796. region->width, &buf, buf_end - buf,
  797. non_mod, NULL, x_pos);
  798. break;
  799. case 0x20:
  800. map2to4[0] = (*buf) >> 4;
  801. map2to4[1] = (*buf++) & 0xf;
  802. map2to4[2] = (*buf) >> 4;
  803. map2to4[3] = (*buf++) & 0xf;
  804. break;
  805. case 0x21:
  806. for (i = 0; i < 4; i++)
  807. map2to8[i] = *buf++;
  808. break;
  809. case 0x22:
  810. for (i = 0; i < 16; i++)
  811. map4to8[i] = *buf++;
  812. break;
  813. case 0xf0:
  814. x_pos = display->x_pos;
  815. y_pos += 2;
  816. break;
  817. default:
  818. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  819. }
  820. }
  821. }
  822. static void dvbsub_parse_object_segment(AVCodecContext *avctx,
  823. const uint8_t *buf, int buf_size)
  824. {
  825. DVBSubContext *ctx = avctx->priv_data;
  826. const uint8_t *buf_end = buf + buf_size;
  827. int object_id;
  828. DVBSubObject *object;
  829. DVBSubObjectDisplay *display;
  830. int top_field_len, bottom_field_len;
  831. int coding_method, non_modifying_color;
  832. object_id = AV_RB16(buf);
  833. buf += 2;
  834. object = get_object(ctx, object_id);
  835. if (!object)
  836. return;
  837. coding_method = ((*buf) >> 2) & 3;
  838. non_modifying_color = ((*buf++) >> 1) & 1;
  839. if (coding_method == 0) {
  840. top_field_len = AV_RB16(buf);
  841. buf += 2;
  842. bottom_field_len = AV_RB16(buf);
  843. buf += 2;
  844. if (buf + top_field_len + bottom_field_len > buf_end) {
  845. av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
  846. return;
  847. }
  848. for (display = object->display_list; display; display = display->object_list_next) {
  849. const uint8_t *block = buf;
  850. int bfl = bottom_field_len;
  851. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  852. non_modifying_color);
  853. if (bottom_field_len > 0)
  854. block = buf + top_field_len;
  855. else
  856. bfl = top_field_len;
  857. dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
  858. non_modifying_color);
  859. }
  860. /* } else if (coding_method == 1) {*/
  861. } else {
  862. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  863. }
  864. }
  865. static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
  866. const uint8_t *buf, int buf_size)
  867. {
  868. DVBSubContext *ctx = avctx->priv_data;
  869. const uint8_t *buf_end = buf + buf_size;
  870. int i, clut_id;
  871. int version;
  872. DVBSubCLUT *clut;
  873. int entry_id, depth , full_range;
  874. int y, cr, cb, alpha;
  875. int r, g, b, r_add, g_add, b_add;
  876. av_dlog(avctx, "DVB clut packet:\n");
  877. for (i=0; i < buf_size; i++) {
  878. av_dlog(avctx, "%02x ", buf[i]);
  879. if (i % 16 == 15)
  880. av_dlog(avctx, "\n");
  881. }
  882. if (i % 16)
  883. av_dlog(avctx, "\n");
  884. clut_id = *buf++;
  885. version = ((*buf)>>4)&15;
  886. buf += 1;
  887. clut = get_clut(ctx, clut_id);
  888. if (!clut) {
  889. clut = av_malloc(sizeof(DVBSubCLUT));
  890. if (!clut)
  891. return AVERROR(ENOMEM);
  892. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  893. clut->id = clut_id;
  894. clut->version = -1;
  895. clut->next = ctx->clut_list;
  896. ctx->clut_list = clut;
  897. }
  898. if (clut->version != version) {
  899. clut->version = version;
  900. while (buf + 4 < buf_end) {
  901. entry_id = *buf++;
  902. depth = (*buf) & 0xe0;
  903. if (depth == 0) {
  904. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  905. return 0;
  906. }
  907. full_range = (*buf++) & 1;
  908. if (full_range) {
  909. y = *buf++;
  910. cr = *buf++;
  911. cb = *buf++;
  912. alpha = *buf++;
  913. } else {
  914. y = buf[0] & 0xfc;
  915. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  916. cb = (buf[1] << 2) & 0xf0;
  917. alpha = (buf[1] << 6) & 0xc0;
  918. buf += 2;
  919. }
  920. if (y == 0)
  921. alpha = 0xff;
  922. YUV_TO_RGB1_CCIR(cb, cr);
  923. YUV_TO_RGB2_CCIR(r, g, b, y);
  924. av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  925. if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
  926. av_dlog(avctx, "More than one bit level marked: %x\n", depth);
  927. if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
  928. return AVERROR_INVALIDDATA;
  929. }
  930. if (depth & 0x80)
  931. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  932. else if (depth & 0x40)
  933. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  934. else if (depth & 0x20)
  935. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  936. }
  937. }
  938. return 0;
  939. }
  940. static int dvbsub_parse_region_segment(AVCodecContext *avctx,
  941. const uint8_t *buf, int buf_size)
  942. {
  943. DVBSubContext *ctx = avctx->priv_data;
  944. const uint8_t *buf_end = buf + buf_size;
  945. int region_id, object_id;
  946. int av_unused version;
  947. DVBSubRegion *region;
  948. DVBSubObject *object;
  949. DVBSubObjectDisplay *display;
  950. int fill;
  951. if (buf_size < 10)
  952. return AVERROR_INVALIDDATA;
  953. region_id = *buf++;
  954. region = get_region(ctx, region_id);
  955. if (!region) {
  956. region = av_mallocz(sizeof(DVBSubRegion));
  957. if (!region)
  958. return AVERROR(ENOMEM);
  959. region->id = region_id;
  960. region->version = -1;
  961. region->next = ctx->region_list;
  962. ctx->region_list = region;
  963. }
  964. version = ((*buf)>>4) & 15;
  965. fill = ((*buf++) >> 3) & 1;
  966. region->width = AV_RB16(buf);
  967. buf += 2;
  968. region->height = AV_RB16(buf);
  969. buf += 2;
  970. if (region->width * region->height != region->buf_size) {
  971. av_free(region->pbuf);
  972. region->buf_size = region->width * region->height;
  973. region->pbuf = av_malloc(region->buf_size);
  974. fill = 1;
  975. region->dirty = 0;
  976. }
  977. region->depth = 1 << (((*buf++) >> 2) & 7);
  978. if(region->depth<2 || region->depth>8){
  979. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  980. region->depth= 4;
  981. }
  982. region->clut = *buf++;
  983. if (region->depth == 8) {
  984. region->bgcolor = *buf++;
  985. buf += 1;
  986. } else {
  987. buf += 1;
  988. if (region->depth == 4)
  989. region->bgcolor = (((*buf++) >> 4) & 15);
  990. else
  991. region->bgcolor = (((*buf++) >> 2) & 3);
  992. }
  993. av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  994. if (fill) {
  995. memset(region->pbuf, region->bgcolor, region->buf_size);
  996. av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
  997. }
  998. delete_region_display_list(ctx, region);
  999. while (buf + 5 < buf_end) {
  1000. object_id = AV_RB16(buf);
  1001. buf += 2;
  1002. object = get_object(ctx, object_id);
  1003. if (!object) {
  1004. object = av_mallocz(sizeof(DVBSubObject));
  1005. object->id = object_id;
  1006. object->next = ctx->object_list;
  1007. ctx->object_list = object;
  1008. }
  1009. object->type = (*buf) >> 6;
  1010. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  1011. display->object_id = object_id;
  1012. display->region_id = region_id;
  1013. display->x_pos = AV_RB16(buf) & 0xfff;
  1014. buf += 2;
  1015. display->y_pos = AV_RB16(buf) & 0xfff;
  1016. buf += 2;
  1017. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  1018. display->fgcolor = *buf++;
  1019. display->bgcolor = *buf++;
  1020. }
  1021. display->region_list_next = region->display_list;
  1022. region->display_list = display;
  1023. display->object_list_next = object->display_list;
  1024. object->display_list = display;
  1025. }
  1026. return 0;
  1027. }
  1028. static int dvbsub_parse_page_segment(AVCodecContext *avctx,
  1029. const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
  1030. {
  1031. DVBSubContext *ctx = avctx->priv_data;
  1032. DVBSubRegionDisplay *display;
  1033. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  1034. const uint8_t *buf_end = buf + buf_size;
  1035. int region_id;
  1036. int page_state;
  1037. int timeout;
  1038. int version;
  1039. if (buf_size < 1)
  1040. return AVERROR_INVALIDDATA;
  1041. timeout = *buf++;
  1042. version = ((*buf)>>4) & 15;
  1043. page_state = ((*buf++) >> 2) & 3;
  1044. if (ctx->version == version) {
  1045. return 0;
  1046. }
  1047. ctx->time_out = timeout;
  1048. ctx->version = version;
  1049. av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  1050. if(ctx->compute_edt == 1)
  1051. save_subtitle_set(avctx, sub, got_output);
  1052. if (page_state == 1 || page_state == 2) {
  1053. delete_regions(ctx);
  1054. delete_objects(ctx);
  1055. delete_cluts(ctx);
  1056. }
  1057. tmp_display_list = ctx->display_list;
  1058. ctx->display_list = NULL;
  1059. while (buf + 5 < buf_end) {
  1060. region_id = *buf++;
  1061. buf += 1;
  1062. display = tmp_display_list;
  1063. tmp_ptr = &tmp_display_list;
  1064. while (display && display->region_id != region_id) {
  1065. tmp_ptr = &display->next;
  1066. display = display->next;
  1067. }
  1068. if (!display)
  1069. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  1070. display->region_id = region_id;
  1071. display->x_pos = AV_RB16(buf);
  1072. buf += 2;
  1073. display->y_pos = AV_RB16(buf);
  1074. buf += 2;
  1075. *tmp_ptr = display->next;
  1076. display->next = ctx->display_list;
  1077. ctx->display_list = display;
  1078. av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  1079. }
  1080. while (tmp_display_list) {
  1081. display = tmp_display_list;
  1082. tmp_display_list = display->next;
  1083. av_freep(&display);
  1084. }
  1085. return 0;
  1086. }
  1087. #ifdef DEBUG
  1088. static void save_display_set(DVBSubContext *ctx)
  1089. {
  1090. DVBSubRegion *region;
  1091. DVBSubRegionDisplay *display;
  1092. DVBSubCLUT *clut;
  1093. uint32_t *clut_table;
  1094. int x_pos, y_pos, width, height;
  1095. int x, y, y_off, x_off;
  1096. uint32_t *pbuf;
  1097. char filename[32];
  1098. static int fileno_index = 0;
  1099. x_pos = -1;
  1100. y_pos = -1;
  1101. width = 0;
  1102. height = 0;
  1103. for (display = ctx->display_list; display; display = display->next) {
  1104. region = get_region(ctx, display->region_id);
  1105. if (!region)
  1106. return;
  1107. if (x_pos == -1) {
  1108. x_pos = display->x_pos;
  1109. y_pos = display->y_pos;
  1110. width = region->width;
  1111. height = region->height;
  1112. } else {
  1113. if (display->x_pos < x_pos) {
  1114. width += (x_pos - display->x_pos);
  1115. x_pos = display->x_pos;
  1116. }
  1117. if (display->y_pos < y_pos) {
  1118. height += (y_pos - display->y_pos);
  1119. y_pos = display->y_pos;
  1120. }
  1121. if (display->x_pos + region->width > x_pos + width) {
  1122. width = display->x_pos + region->width - x_pos;
  1123. }
  1124. if (display->y_pos + region->height > y_pos + height) {
  1125. height = display->y_pos + region->height - y_pos;
  1126. }
  1127. }
  1128. }
  1129. if (x_pos >= 0) {
  1130. pbuf = av_malloc(width * height * 4);
  1131. for (display = ctx->display_list; display; display = display->next) {
  1132. region = get_region(ctx, display->region_id);
  1133. if (!region)
  1134. return;
  1135. x_off = display->x_pos - x_pos;
  1136. y_off = display->y_pos - y_pos;
  1137. clut = get_clut(ctx, region->clut);
  1138. if (!clut)
  1139. clut = &default_clut;
  1140. switch (region->depth) {
  1141. case 2:
  1142. clut_table = clut->clut4;
  1143. break;
  1144. case 8:
  1145. clut_table = clut->clut256;
  1146. break;
  1147. case 4:
  1148. default:
  1149. clut_table = clut->clut16;
  1150. break;
  1151. }
  1152. for (y = 0; y < region->height; y++) {
  1153. for (x = 0; x < region->width; x++) {
  1154. pbuf[((y + y_off) * width) + x_off + x] =
  1155. clut_table[region->pbuf[y * region->width + x]];
  1156. }
  1157. }
  1158. }
  1159. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  1160. png_save2(filename, pbuf, width, height);
  1161. av_freep(&pbuf);
  1162. }
  1163. fileno_index++;
  1164. }
  1165. #endif
  1166. static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
  1167. const uint8_t *buf,
  1168. int buf_size)
  1169. {
  1170. DVBSubContext *ctx = avctx->priv_data;
  1171. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1172. int dds_version, info_byte;
  1173. if (buf_size < 5)
  1174. return AVERROR_INVALIDDATA;
  1175. info_byte = bytestream_get_byte(&buf);
  1176. dds_version = info_byte >> 4;
  1177. if (display_def && display_def->version == dds_version)
  1178. return 0; // already have this display definition version
  1179. if (!display_def) {
  1180. display_def = av_mallocz(sizeof(*display_def));
  1181. ctx->display_definition = display_def;
  1182. }
  1183. if (!display_def)
  1184. return AVERROR(ENOMEM);
  1185. display_def->version = dds_version;
  1186. display_def->x = 0;
  1187. display_def->y = 0;
  1188. display_def->width = bytestream_get_be16(&buf) + 1;
  1189. display_def->height = bytestream_get_be16(&buf) + 1;
  1190. if (!avctx->width || !avctx->height) {
  1191. avctx->width = display_def->width;
  1192. avctx->height = display_def->height;
  1193. }
  1194. if (buf_size < 13)
  1195. return 0;
  1196. if (info_byte & 1<<3) { // display_window_flag
  1197. display_def->x = bytestream_get_be16(&buf);
  1198. display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
  1199. display_def->y = bytestream_get_be16(&buf);
  1200. display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
  1201. }
  1202. return 0;
  1203. }
  1204. static void dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1205. int buf_size, AVSubtitle *sub,int *got_output)
  1206. {
  1207. DVBSubContext *ctx = avctx->priv_data;
  1208. if(ctx->compute_edt == 0)
  1209. save_subtitle_set(avctx, sub, got_output);
  1210. #ifdef DEBUG
  1211. save_display_set(ctx);
  1212. #endif
  1213. }
  1214. static int dvbsub_decode(AVCodecContext *avctx,
  1215. void *data, int *data_size,
  1216. AVPacket *avpkt)
  1217. {
  1218. const uint8_t *buf = avpkt->data;
  1219. int buf_size = avpkt->size;
  1220. DVBSubContext *ctx = avctx->priv_data;
  1221. AVSubtitle *sub = data;
  1222. const uint8_t *p, *p_end;
  1223. int segment_type;
  1224. int page_id;
  1225. int segment_length;
  1226. int i;
  1227. int ret = 0;
  1228. int got_segment = 0;
  1229. av_dlog(avctx, "DVB sub packet:\n");
  1230. for (i=0; i < buf_size; i++) {
  1231. av_dlog(avctx, "%02x ", buf[i]);
  1232. if (i % 16 == 15)
  1233. av_dlog(avctx, "\n");
  1234. }
  1235. if (i % 16)
  1236. av_dlog(avctx, "\n");
  1237. if (buf_size <= 6 || *buf != 0x0f) {
  1238. av_dlog(avctx, "incomplete or broken packet");
  1239. return -1;
  1240. }
  1241. p = buf;
  1242. p_end = buf + buf_size;
  1243. while (p_end - p >= 6 && *p == 0x0f) {
  1244. p += 1;
  1245. segment_type = *p++;
  1246. page_id = AV_RB16(p);
  1247. p += 2;
  1248. segment_length = AV_RB16(p);
  1249. p += 2;
  1250. if (avctx->debug & FF_DEBUG_STARTCODE) {
  1251. av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
  1252. }
  1253. if (p_end - p < segment_length) {
  1254. av_dlog(avctx, "incomplete or broken packet");
  1255. ret = -1;
  1256. goto end;
  1257. }
  1258. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
  1259. ctx->composition_id == -1 || ctx->ancillary_id == -1) {
  1260. switch (segment_type) {
  1261. case DVBSUB_PAGE_SEGMENT:
  1262. dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
  1263. got_segment |= 1;
  1264. break;
  1265. case DVBSUB_REGION_SEGMENT:
  1266. dvbsub_parse_region_segment(avctx, p, segment_length);
  1267. got_segment |= 2;
  1268. break;
  1269. case DVBSUB_CLUT_SEGMENT:
  1270. ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
  1271. if (ret < 0) goto end;
  1272. got_segment |= 4;
  1273. break;
  1274. case DVBSUB_OBJECT_SEGMENT:
  1275. dvbsub_parse_object_segment(avctx, p, segment_length);
  1276. got_segment |= 8;
  1277. break;
  1278. case DVBSUB_DISPLAYDEFINITION_SEGMENT:
  1279. dvbsub_parse_display_definition_segment(avctx, p, segment_length);
  1280. break;
  1281. case DVBSUB_DISPLAY_SEGMENT:
  1282. dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
  1283. got_segment |= 16;
  1284. break;
  1285. default:
  1286. av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1287. segment_type, page_id, segment_length);
  1288. break;
  1289. }
  1290. }
  1291. p += segment_length;
  1292. }
  1293. // Some streams do not send a display segment but if we have all the other
  1294. // segments then we need no further data.
  1295. if (got_segment == 15) {
  1296. av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
  1297. dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
  1298. }
  1299. end:
  1300. if(ret < 0) {
  1301. *data_size = 0;
  1302. avsubtitle_free(sub);
  1303. return ret;
  1304. } else {
  1305. if(ctx->compute_edt == 1 )
  1306. FFSWAP(int64_t, ctx->prev_start, sub->pts);
  1307. }
  1308. return p - buf;
  1309. }
  1310. #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
  1311. static const AVOption options[] = {
  1312. {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DS},
  1313. {NULL}
  1314. };
  1315. static const AVClass dvbsubdec_class = {
  1316. .class_name = "DVB Sub Decoder",
  1317. .item_name = av_default_item_name,
  1318. .option = options,
  1319. .version = LIBAVUTIL_VERSION_INT,
  1320. };
  1321. AVCodec ff_dvbsub_decoder = {
  1322. .name = "dvbsub",
  1323. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1324. .type = AVMEDIA_TYPE_SUBTITLE,
  1325. .id = AV_CODEC_ID_DVB_SUBTITLE,
  1326. .priv_data_size = sizeof(DVBSubContext),
  1327. .init = dvbsub_init_decoder,
  1328. .close = dvbsub_close_decoder,
  1329. .decode = dvbsub_decode,
  1330. .priv_class = &dvbsubdec_class,
  1331. };