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.

1553 lines
42KB

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