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.

1705 lines
48KB

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