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.

1744 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(AVPicture *frame, 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) frame->data[0][(x) + (y)*frame->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[ frame->data[0][(x) + (y)*frame->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 = frame->data[0][x + y*frame->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(frame->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->pict.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->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  757. if (!rect->pict.data[1]) {
  758. ret = AVERROR(ENOMEM);
  759. goto fail;
  760. }
  761. memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
  762. rect->pict.data[0] = av_malloc(region->buf_size);
  763. if (!rect->pict.data[0]) {
  764. ret = AVERROR(ENOMEM);
  765. goto fail;
  766. }
  767. memcpy(rect->pict.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->pict, rect->w, rect->h);
  770. i++;
  771. }
  772. }
  773. return 0;
  774. fail:
  775. if (sub->rects) {
  776. for(i=0; i<sub->num_rects; i++) {
  777. rect = sub->rects[i];
  778. if (rect) {
  779. av_freep(&rect->pict.data[0]);
  780. av_freep(&rect->pict.data[1]);
  781. }
  782. av_freep(&sub->rects[i]);
  783. }
  784. av_freep(&sub->rects);
  785. }
  786. sub->num_rects = 0;
  787. return ret;
  788. }
  789. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  790. const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  791. {
  792. DVBSubContext *ctx = avctx->priv_data;
  793. DVBSubRegion *region = get_region(ctx, display->region_id);
  794. const uint8_t *buf_end = buf + buf_size;
  795. uint8_t *pbuf;
  796. int x_pos, y_pos;
  797. int i;
  798. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  799. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  800. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  801. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  802. uint8_t *map_table;
  803. #if 0
  804. ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
  805. top_bottom ? "bottom" : "top");
  806. for (i = 0; i < buf_size; i++) {
  807. if (i % 16 == 0)
  808. ff_dlog(avctx, "0x%8p: ", buf+i);
  809. ff_dlog(avctx, "%02x ", buf[i]);
  810. if (i % 16 == 15)
  811. ff_dlog(avctx, "\n");
  812. }
  813. if (i % 16)
  814. ff_dlog(avctx, "\n");
  815. #endif
  816. if (!region)
  817. return;
  818. pbuf = region->pbuf;
  819. region->dirty = 1;
  820. x_pos = display->x_pos;
  821. y_pos = display->y_pos;
  822. y_pos += top_bottom;
  823. while (buf < buf_end) {
  824. if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
  825. av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
  826. return;
  827. }
  828. switch (*buf++) {
  829. case 0x10:
  830. if (region->depth == 8)
  831. map_table = map2to8;
  832. else if (region->depth == 4)
  833. map_table = map2to4;
  834. else
  835. map_table = NULL;
  836. x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
  837. region->width, &buf, buf_end - buf,
  838. non_mod, map_table, x_pos);
  839. break;
  840. case 0x11:
  841. if (region->depth < 4) {
  842. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  843. return;
  844. }
  845. if (region->depth == 8)
  846. map_table = map4to8;
  847. else
  848. map_table = NULL;
  849. x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
  850. region->width, &buf, buf_end - buf,
  851. non_mod, map_table, x_pos);
  852. break;
  853. case 0x12:
  854. if (region->depth < 8) {
  855. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  856. return;
  857. }
  858. x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
  859. region->width, &buf, buf_end - buf,
  860. non_mod, NULL, x_pos);
  861. break;
  862. case 0x20:
  863. map2to4[0] = (*buf) >> 4;
  864. map2to4[1] = (*buf++) & 0xf;
  865. map2to4[2] = (*buf) >> 4;
  866. map2to4[3] = (*buf++) & 0xf;
  867. break;
  868. case 0x21:
  869. for (i = 0; i < 4; i++)
  870. map2to8[i] = *buf++;
  871. break;
  872. case 0x22:
  873. for (i = 0; i < 16; i++)
  874. map4to8[i] = *buf++;
  875. break;
  876. case 0xf0:
  877. x_pos = display->x_pos;
  878. y_pos += 2;
  879. break;
  880. default:
  881. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  882. }
  883. }
  884. }
  885. static int dvbsub_parse_object_segment(AVCodecContext *avctx,
  886. const uint8_t *buf, int buf_size)
  887. {
  888. DVBSubContext *ctx = avctx->priv_data;
  889. const uint8_t *buf_end = buf + buf_size;
  890. int object_id;
  891. DVBSubObject *object;
  892. DVBSubObjectDisplay *display;
  893. int top_field_len, bottom_field_len;
  894. int coding_method, non_modifying_color;
  895. object_id = AV_RB16(buf);
  896. buf += 2;
  897. object = get_object(ctx, object_id);
  898. if (!object)
  899. return AVERROR_INVALIDDATA;
  900. coding_method = ((*buf) >> 2) & 3;
  901. non_modifying_color = ((*buf++) >> 1) & 1;
  902. if (coding_method == 0) {
  903. top_field_len = AV_RB16(buf);
  904. buf += 2;
  905. bottom_field_len = AV_RB16(buf);
  906. buf += 2;
  907. if (buf + top_field_len + bottom_field_len > buf_end) {
  908. av_log(avctx, AV_LOG_ERROR, "Field data size %d+%d too large\n", top_field_len, bottom_field_len);
  909. return AVERROR_INVALIDDATA;
  910. }
  911. for (display = object->display_list; display; display = display->object_list_next) {
  912. const uint8_t *block = buf;
  913. int bfl = bottom_field_len;
  914. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  915. non_modifying_color);
  916. if (bottom_field_len > 0)
  917. block = buf + top_field_len;
  918. else
  919. bfl = top_field_len;
  920. dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
  921. non_modifying_color);
  922. }
  923. /* } else if (coding_method == 1) {*/
  924. } else {
  925. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  926. }
  927. return 0;
  928. }
  929. static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
  930. const uint8_t *buf, int buf_size)
  931. {
  932. DVBSubContext *ctx = avctx->priv_data;
  933. const uint8_t *buf_end = buf + buf_size;
  934. int i, clut_id;
  935. int version;
  936. DVBSubCLUT *clut;
  937. int entry_id, depth , full_range;
  938. int y, cr, cb, alpha;
  939. int r, g, b, r_add, g_add, b_add;
  940. ff_dlog(avctx, "DVB clut packet:\n");
  941. for (i=0; i < buf_size; i++) {
  942. ff_dlog(avctx, "%02x ", buf[i]);
  943. if (i % 16 == 15)
  944. ff_dlog(avctx, "\n");
  945. }
  946. if (i % 16)
  947. ff_dlog(avctx, "\n");
  948. clut_id = *buf++;
  949. version = ((*buf)>>4)&15;
  950. buf += 1;
  951. clut = get_clut(ctx, clut_id);
  952. if (!clut) {
  953. clut = av_malloc(sizeof(DVBSubCLUT));
  954. if (!clut)
  955. return AVERROR(ENOMEM);
  956. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  957. clut->id = clut_id;
  958. clut->version = -1;
  959. clut->next = ctx->clut_list;
  960. ctx->clut_list = clut;
  961. }
  962. if (clut->version != version) {
  963. clut->version = version;
  964. while (buf + 4 < buf_end) {
  965. entry_id = *buf++;
  966. depth = (*buf) & 0xe0;
  967. if (depth == 0) {
  968. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  969. }
  970. full_range = (*buf++) & 1;
  971. if (full_range) {
  972. y = *buf++;
  973. cr = *buf++;
  974. cb = *buf++;
  975. alpha = *buf++;
  976. } else {
  977. y = buf[0] & 0xfc;
  978. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  979. cb = (buf[1] << 2) & 0xf0;
  980. alpha = (buf[1] << 6) & 0xc0;
  981. buf += 2;
  982. }
  983. if (y == 0)
  984. alpha = 0xff;
  985. YUV_TO_RGB1_CCIR(cb, cr);
  986. YUV_TO_RGB2_CCIR(r, g, b, y);
  987. ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  988. if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
  989. ff_dlog(avctx, "More than one bit level marked: %x\n", depth);
  990. if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
  991. return AVERROR_INVALIDDATA;
  992. }
  993. if (depth & 0x80)
  994. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  995. else if (depth & 0x40)
  996. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  997. else if (depth & 0x20)
  998. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  999. }
  1000. }
  1001. return 0;
  1002. }
  1003. static int dvbsub_parse_region_segment(AVCodecContext *avctx,
  1004. const uint8_t *buf, int buf_size)
  1005. {
  1006. DVBSubContext *ctx = avctx->priv_data;
  1007. const uint8_t *buf_end = buf + buf_size;
  1008. int region_id, object_id;
  1009. int av_unused version;
  1010. DVBSubRegion *region;
  1011. DVBSubObject *object;
  1012. DVBSubObjectDisplay *display;
  1013. int fill;
  1014. if (buf_size < 10)
  1015. return AVERROR_INVALIDDATA;
  1016. region_id = *buf++;
  1017. region = get_region(ctx, region_id);
  1018. if (!region) {
  1019. region = av_mallocz(sizeof(DVBSubRegion));
  1020. if (!region)
  1021. return AVERROR(ENOMEM);
  1022. region->id = region_id;
  1023. region->version = -1;
  1024. region->next = ctx->region_list;
  1025. ctx->region_list = region;
  1026. }
  1027. version = ((*buf)>>4) & 15;
  1028. fill = ((*buf++) >> 3) & 1;
  1029. region->width = AV_RB16(buf);
  1030. buf += 2;
  1031. region->height = AV_RB16(buf);
  1032. buf += 2;
  1033. if (region->width * region->height != region->buf_size) {
  1034. av_free(region->pbuf);
  1035. region->buf_size = region->width * region->height;
  1036. region->pbuf = av_malloc(region->buf_size);
  1037. if (!region->pbuf) {
  1038. region->buf_size =
  1039. region->width =
  1040. region->height = 0;
  1041. return AVERROR(ENOMEM);
  1042. }
  1043. fill = 1;
  1044. region->dirty = 0;
  1045. }
  1046. region->depth = 1 << (((*buf++) >> 2) & 7);
  1047. if(region->depth<2 || region->depth>8){
  1048. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  1049. region->depth= 4;
  1050. }
  1051. region->clut = *buf++;
  1052. if (region->depth == 8) {
  1053. region->bgcolor = *buf++;
  1054. buf += 1;
  1055. } else {
  1056. buf += 1;
  1057. if (region->depth == 4)
  1058. region->bgcolor = (((*buf++) >> 4) & 15);
  1059. else
  1060. region->bgcolor = (((*buf++) >> 2) & 3);
  1061. }
  1062. ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  1063. if (fill) {
  1064. memset(region->pbuf, region->bgcolor, region->buf_size);
  1065. ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
  1066. }
  1067. delete_region_display_list(ctx, region);
  1068. while (buf + 5 < buf_end) {
  1069. object_id = AV_RB16(buf);
  1070. buf += 2;
  1071. object = get_object(ctx, object_id);
  1072. if (!object) {
  1073. object = av_mallocz(sizeof(DVBSubObject));
  1074. if (!object)
  1075. return AVERROR(ENOMEM);
  1076. object->id = object_id;
  1077. object->next = ctx->object_list;
  1078. ctx->object_list = object;
  1079. }
  1080. object->type = (*buf) >> 6;
  1081. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  1082. if (!display)
  1083. return AVERROR(ENOMEM);
  1084. display->object_id = object_id;
  1085. display->region_id = region_id;
  1086. display->x_pos = AV_RB16(buf) & 0xfff;
  1087. buf += 2;
  1088. display->y_pos = AV_RB16(buf) & 0xfff;
  1089. buf += 2;
  1090. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  1091. display->fgcolor = *buf++;
  1092. display->bgcolor = *buf++;
  1093. }
  1094. display->region_list_next = region->display_list;
  1095. region->display_list = display;
  1096. display->object_list_next = object->display_list;
  1097. object->display_list = display;
  1098. }
  1099. return 0;
  1100. }
  1101. static int dvbsub_parse_page_segment(AVCodecContext *avctx,
  1102. const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
  1103. {
  1104. DVBSubContext *ctx = avctx->priv_data;
  1105. DVBSubRegionDisplay *display;
  1106. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  1107. const uint8_t *buf_end = buf + buf_size;
  1108. int region_id;
  1109. int page_state;
  1110. int timeout;
  1111. int version;
  1112. if (buf_size < 1)
  1113. return AVERROR_INVALIDDATA;
  1114. timeout = *buf++;
  1115. version = ((*buf)>>4) & 15;
  1116. page_state = ((*buf++) >> 2) & 3;
  1117. if (ctx->version == version) {
  1118. return 0;
  1119. }
  1120. ctx->time_out = timeout;
  1121. ctx->version = version;
  1122. ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  1123. if(ctx->compute_edt == 1)
  1124. save_subtitle_set(avctx, sub, got_output);
  1125. if (page_state == 1 || page_state == 2) {
  1126. delete_regions(ctx);
  1127. delete_objects(ctx);
  1128. delete_cluts(ctx);
  1129. }
  1130. tmp_display_list = ctx->display_list;
  1131. ctx->display_list = NULL;
  1132. while (buf + 5 < buf_end) {
  1133. region_id = *buf++;
  1134. buf += 1;
  1135. display = tmp_display_list;
  1136. tmp_ptr = &tmp_display_list;
  1137. while (display && display->region_id != region_id) {
  1138. tmp_ptr = &display->next;
  1139. display = display->next;
  1140. }
  1141. if (!display) {
  1142. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  1143. if (!display)
  1144. return AVERROR(ENOMEM);
  1145. }
  1146. display->region_id = region_id;
  1147. display->x_pos = AV_RB16(buf);
  1148. buf += 2;
  1149. display->y_pos = AV_RB16(buf);
  1150. buf += 2;
  1151. *tmp_ptr = display->next;
  1152. display->next = ctx->display_list;
  1153. ctx->display_list = display;
  1154. ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  1155. }
  1156. while (tmp_display_list) {
  1157. display = tmp_display_list;
  1158. tmp_display_list = display->next;
  1159. av_freep(&display);
  1160. }
  1161. return 0;
  1162. }
  1163. #ifdef DEBUG
  1164. static void save_display_set(DVBSubContext *ctx)
  1165. {
  1166. DVBSubRegion *region;
  1167. DVBSubRegionDisplay *display;
  1168. DVBSubCLUT *clut;
  1169. uint32_t *clut_table;
  1170. int x_pos, y_pos, width, height;
  1171. int x, y, y_off, x_off;
  1172. uint32_t *pbuf;
  1173. char filename[32];
  1174. static int fileno_index = 0;
  1175. x_pos = -1;
  1176. y_pos = -1;
  1177. width = 0;
  1178. height = 0;
  1179. for (display = ctx->display_list; display; display = display->next) {
  1180. region = get_region(ctx, display->region_id);
  1181. if (!region)
  1182. return;
  1183. if (x_pos == -1) {
  1184. x_pos = display->x_pos;
  1185. y_pos = display->y_pos;
  1186. width = region->width;
  1187. height = region->height;
  1188. } else {
  1189. if (display->x_pos < x_pos) {
  1190. width += (x_pos - display->x_pos);
  1191. x_pos = display->x_pos;
  1192. }
  1193. if (display->y_pos < y_pos) {
  1194. height += (y_pos - display->y_pos);
  1195. y_pos = display->y_pos;
  1196. }
  1197. if (display->x_pos + region->width > x_pos + width) {
  1198. width = display->x_pos + region->width - x_pos;
  1199. }
  1200. if (display->y_pos + region->height > y_pos + height) {
  1201. height = display->y_pos + region->height - y_pos;
  1202. }
  1203. }
  1204. }
  1205. if (x_pos >= 0) {
  1206. pbuf = av_malloc(width * height * 4);
  1207. if (!pbuf)
  1208. return;
  1209. for (display = ctx->display_list; display; display = display->next) {
  1210. region = get_region(ctx, display->region_id);
  1211. if (!region)
  1212. return;
  1213. x_off = display->x_pos - x_pos;
  1214. y_off = display->y_pos - y_pos;
  1215. clut = get_clut(ctx, region->clut);
  1216. if (!clut)
  1217. clut = &default_clut;
  1218. switch (region->depth) {
  1219. case 2:
  1220. clut_table = clut->clut4;
  1221. break;
  1222. case 8:
  1223. clut_table = clut->clut256;
  1224. break;
  1225. case 4:
  1226. default:
  1227. clut_table = clut->clut16;
  1228. break;
  1229. }
  1230. for (y = 0; y < region->height; y++) {
  1231. for (x = 0; x < region->width; x++) {
  1232. pbuf[((y + y_off) * width) + x_off + x] =
  1233. clut_table[region->pbuf[y * region->width + x]];
  1234. }
  1235. }
  1236. }
  1237. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  1238. png_save2(filename, pbuf, width, height);
  1239. av_freep(&pbuf);
  1240. }
  1241. fileno_index++;
  1242. }
  1243. #endif
  1244. static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
  1245. const uint8_t *buf,
  1246. int buf_size)
  1247. {
  1248. DVBSubContext *ctx = avctx->priv_data;
  1249. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1250. int dds_version, info_byte;
  1251. if (buf_size < 5)
  1252. return AVERROR_INVALIDDATA;
  1253. info_byte = bytestream_get_byte(&buf);
  1254. dds_version = info_byte >> 4;
  1255. if (display_def && display_def->version == dds_version)
  1256. return 0; // already have this display definition version
  1257. if (!display_def) {
  1258. display_def = av_mallocz(sizeof(*display_def));
  1259. if (!display_def)
  1260. return AVERROR(ENOMEM);
  1261. ctx->display_definition = display_def;
  1262. }
  1263. display_def->version = dds_version;
  1264. display_def->x = 0;
  1265. display_def->y = 0;
  1266. display_def->width = bytestream_get_be16(&buf) + 1;
  1267. display_def->height = bytestream_get_be16(&buf) + 1;
  1268. if (!avctx->width || !avctx->height) {
  1269. avctx->width = display_def->width;
  1270. avctx->height = display_def->height;
  1271. }
  1272. if (info_byte & 1<<3) { // display_window_flag
  1273. if (buf_size < 13)
  1274. return AVERROR_INVALIDDATA;
  1275. display_def->x = bytestream_get_be16(&buf);
  1276. display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
  1277. display_def->y = bytestream_get_be16(&buf);
  1278. display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
  1279. }
  1280. return 0;
  1281. }
  1282. static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1283. int buf_size, AVSubtitle *sub,int *got_output)
  1284. {
  1285. DVBSubContext *ctx = avctx->priv_data;
  1286. if(ctx->compute_edt == 0)
  1287. save_subtitle_set(avctx, sub, got_output);
  1288. #ifdef DEBUG
  1289. save_display_set(ctx);
  1290. #endif
  1291. return 0;
  1292. }
  1293. static int dvbsub_decode(AVCodecContext *avctx,
  1294. void *data, int *data_size,
  1295. AVPacket *avpkt)
  1296. {
  1297. const uint8_t *buf = avpkt->data;
  1298. int buf_size = avpkt->size;
  1299. DVBSubContext *ctx = avctx->priv_data;
  1300. AVSubtitle *sub = data;
  1301. const uint8_t *p, *p_end;
  1302. int segment_type;
  1303. int page_id;
  1304. int segment_length;
  1305. int i;
  1306. int ret = 0;
  1307. int got_segment = 0;
  1308. int got_dds = 0;
  1309. ff_dlog(avctx, "DVB sub packet:\n");
  1310. for (i=0; i < buf_size; i++) {
  1311. ff_dlog(avctx, "%02x ", buf[i]);
  1312. if (i % 16 == 15)
  1313. ff_dlog(avctx, "\n");
  1314. }
  1315. if (i % 16)
  1316. ff_dlog(avctx, "\n");
  1317. if (buf_size <= 6 || *buf != 0x0f) {
  1318. ff_dlog(avctx, "incomplete or broken packet");
  1319. return AVERROR_INVALIDDATA;
  1320. }
  1321. p = buf;
  1322. p_end = buf + buf_size;
  1323. while (p_end - p >= 6 && *p == 0x0f) {
  1324. p += 1;
  1325. segment_type = *p++;
  1326. page_id = AV_RB16(p);
  1327. p += 2;
  1328. segment_length = AV_RB16(p);
  1329. p += 2;
  1330. if (avctx->debug & FF_DEBUG_STARTCODE) {
  1331. av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
  1332. }
  1333. if (p_end - p < segment_length) {
  1334. ff_dlog(avctx, "incomplete or broken packet");
  1335. ret = -1;
  1336. goto end;
  1337. }
  1338. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
  1339. ctx->composition_id == -1 || ctx->ancillary_id == -1) {
  1340. int ret = 0;
  1341. switch (segment_type) {
  1342. case DVBSUB_PAGE_SEGMENT:
  1343. ret = dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
  1344. got_segment |= 1;
  1345. break;
  1346. case DVBSUB_REGION_SEGMENT:
  1347. ret = dvbsub_parse_region_segment(avctx, p, segment_length);
  1348. got_segment |= 2;
  1349. break;
  1350. case DVBSUB_CLUT_SEGMENT:
  1351. ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
  1352. if (ret < 0) goto end;
  1353. got_segment |= 4;
  1354. break;
  1355. case DVBSUB_OBJECT_SEGMENT:
  1356. ret = dvbsub_parse_object_segment(avctx, p, segment_length);
  1357. got_segment |= 8;
  1358. break;
  1359. case DVBSUB_DISPLAYDEFINITION_SEGMENT:
  1360. ret = dvbsub_parse_display_definition_segment(avctx, p,
  1361. segment_length);
  1362. got_dds = 1;
  1363. break;
  1364. case DVBSUB_DISPLAY_SEGMENT:
  1365. ret = dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
  1366. if (got_segment == 15 && !got_dds && !avctx->width && !avctx->height) {
  1367. // Default from ETSI EN 300 743 V1.3.1 (7.2.1)
  1368. avctx->width = 720;
  1369. avctx->height = 576;
  1370. }
  1371. got_segment |= 16;
  1372. break;
  1373. default:
  1374. ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1375. segment_type, page_id, segment_length);
  1376. break;
  1377. }
  1378. if (ret < 0)
  1379. goto end;
  1380. }
  1381. p += segment_length;
  1382. }
  1383. // Some streams do not send a display segment but if we have all the other
  1384. // segments then we need no further data.
  1385. if (got_segment == 15) {
  1386. av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
  1387. dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
  1388. }
  1389. end:
  1390. if(ret < 0) {
  1391. *data_size = 0;
  1392. avsubtitle_free(sub);
  1393. return ret;
  1394. } else {
  1395. if(ctx->compute_edt == 1 )
  1396. FFSWAP(int64_t, ctx->prev_start, sub->pts);
  1397. }
  1398. return p - buf;
  1399. }
  1400. #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
  1401. static const AVOption options[] = {
  1402. {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DS},
  1403. {"compute_clut", "compute clut when not available(-1) or always(1) or never(0)", offsetof(DVBSubContext, compute_clut), FF_OPT_TYPE_INT, {.i64 = -1}, -1, 1, DS},
  1404. {"dvb_substream", "", offsetof(DVBSubContext, substream), FF_OPT_TYPE_INT, {.i64 = -1}, -1, 63, DS},
  1405. {NULL}
  1406. };
  1407. static const AVClass dvbsubdec_class = {
  1408. .class_name = "DVB Sub Decoder",
  1409. .item_name = av_default_item_name,
  1410. .option = options,
  1411. .version = LIBAVUTIL_VERSION_INT,
  1412. };
  1413. AVCodec ff_dvbsub_decoder = {
  1414. .name = "dvbsub",
  1415. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1416. .type = AVMEDIA_TYPE_SUBTITLE,
  1417. .id = AV_CODEC_ID_DVB_SUBTITLE,
  1418. .priv_data_size = sizeof(DVBSubContext),
  1419. .init = dvbsub_init_decoder,
  1420. .close = dvbsub_close_decoder,
  1421. .decode = dvbsub_decode,
  1422. .priv_class = &dvbsubdec_class,
  1423. };