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.

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