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.

1476 lines
40KB

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