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.

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