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.

1757 lines
49KB

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