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