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.

1478 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. } else {
  578. bits = *(*srcbuf)++;
  579. if (non_mod == 1 && bits == 1)
  580. pixels_read += run_length;
  581. }
  582. if (map_table)
  583. bits = map_table[0];
  584. else
  585. bits = 0;
  586. while (run_length-- > 0 && pixels_read < dbuf_len) {
  587. *destbuf++ = bits;
  588. pixels_read++;
  589. }
  590. }
  591. }
  592. if (*(*srcbuf)++)
  593. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  594. return pixels_read;
  595. }
  596. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  597. const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  598. {
  599. DVBSubContext *ctx = avctx->priv_data;
  600. DVBSubRegion *region = get_region(ctx, display->region_id);
  601. const uint8_t *buf_end = buf + buf_size;
  602. uint8_t *pbuf;
  603. int x_pos, y_pos;
  604. int i;
  605. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  606. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  607. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  608. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  609. uint8_t *map_table;
  610. av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
  611. top_bottom ? "bottom" : "top");
  612. for (i = 0; i < buf_size; i++) {
  613. if (i % 16 == 0)
  614. av_dlog(avctx, "0x%8p: ", buf+i);
  615. av_dlog(avctx, "%02x ", buf[i]);
  616. if (i % 16 == 15)
  617. av_dlog(avctx, "\n");
  618. }
  619. if (i % 16)
  620. av_dlog(avctx, "\n");
  621. if (region == 0)
  622. return;
  623. pbuf = region->pbuf;
  624. x_pos = display->x_pos;
  625. y_pos = display->y_pos;
  626. if ((y_pos & 1) != top_bottom)
  627. y_pos++;
  628. while (buf < buf_end) {
  629. if (x_pos > region->width || y_pos > region->height) {
  630. av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
  631. return;
  632. }
  633. switch (*buf++) {
  634. case 0x10:
  635. if (region->depth == 8)
  636. map_table = map2to8;
  637. else if (region->depth == 4)
  638. map_table = map2to4;
  639. else
  640. map_table = NULL;
  641. x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
  642. region->width - x_pos, &buf, buf_end - buf,
  643. non_mod, map_table);
  644. break;
  645. case 0x11:
  646. if (region->depth < 4) {
  647. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  648. return;
  649. }
  650. if (region->depth == 8)
  651. map_table = map4to8;
  652. else
  653. map_table = NULL;
  654. x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
  655. region->width - x_pos, &buf, buf_end - buf,
  656. non_mod, map_table);
  657. break;
  658. case 0x12:
  659. if (region->depth < 8) {
  660. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  661. return;
  662. }
  663. x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
  664. region->width - x_pos, &buf, buf_end - buf,
  665. non_mod, NULL);
  666. break;
  667. case 0x20:
  668. map2to4[0] = (*buf) >> 4;
  669. map2to4[1] = (*buf++) & 0xf;
  670. map2to4[2] = (*buf) >> 4;
  671. map2to4[3] = (*buf++) & 0xf;
  672. break;
  673. case 0x21:
  674. for (i = 0; i < 4; i++)
  675. map2to8[i] = *buf++;
  676. break;
  677. case 0x22:
  678. for (i = 0; i < 16; i++)
  679. map4to8[i] = *buf++;
  680. break;
  681. case 0xf0:
  682. x_pos = display->x_pos;
  683. y_pos += 2;
  684. break;
  685. default:
  686. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  687. }
  688. }
  689. }
  690. static void dvbsub_parse_object_segment(AVCodecContext *avctx,
  691. const uint8_t *buf, int buf_size)
  692. {
  693. DVBSubContext *ctx = avctx->priv_data;
  694. const uint8_t *buf_end = buf + buf_size;
  695. const uint8_t *block;
  696. int object_id;
  697. DVBSubObject *object;
  698. DVBSubObjectDisplay *display;
  699. int top_field_len, bottom_field_len;
  700. int coding_method, non_modifying_color;
  701. object_id = AV_RB16(buf);
  702. buf += 2;
  703. object = get_object(ctx, object_id);
  704. if (!object)
  705. return;
  706. coding_method = ((*buf) >> 2) & 3;
  707. non_modifying_color = ((*buf++) >> 1) & 1;
  708. if (coding_method == 0) {
  709. top_field_len = AV_RB16(buf);
  710. buf += 2;
  711. bottom_field_len = AV_RB16(buf);
  712. buf += 2;
  713. if (buf + top_field_len + bottom_field_len > buf_end) {
  714. av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
  715. return;
  716. }
  717. for (display = object->display_list; display; display = display->object_list_next) {
  718. block = buf;
  719. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  720. non_modifying_color);
  721. if (bottom_field_len > 0)
  722. block = buf + top_field_len;
  723. else
  724. bottom_field_len = top_field_len;
  725. dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
  726. non_modifying_color);
  727. }
  728. /* } else if (coding_method == 1) {*/
  729. } else {
  730. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  731. }
  732. }
  733. static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
  734. const uint8_t *buf, int buf_size)
  735. {
  736. DVBSubContext *ctx = avctx->priv_data;
  737. const uint8_t *buf_end = buf + buf_size;
  738. int i, clut_id;
  739. DVBSubCLUT *clut;
  740. int entry_id, depth , full_range;
  741. int y, cr, cb, alpha;
  742. int r, g, b, r_add, g_add, b_add;
  743. av_dlog(avctx, "DVB clut packet:\n");
  744. for (i=0; i < buf_size; i++) {
  745. av_dlog(avctx, "%02x ", buf[i]);
  746. if (i % 16 == 15)
  747. av_dlog(avctx, "\n");
  748. }
  749. if (i % 16)
  750. av_dlog(avctx, "\n");
  751. clut_id = *buf++;
  752. buf += 1;
  753. clut = get_clut(ctx, clut_id);
  754. if (!clut) {
  755. clut = av_malloc(sizeof(DVBSubCLUT));
  756. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  757. clut->id = clut_id;
  758. clut->next = ctx->clut_list;
  759. ctx->clut_list = clut;
  760. }
  761. while (buf + 4 < buf_end) {
  762. entry_id = *buf++;
  763. depth = (*buf) & 0xe0;
  764. if (depth == 0) {
  765. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  766. return;
  767. }
  768. full_range = (*buf++) & 1;
  769. if (full_range) {
  770. y = *buf++;
  771. cr = *buf++;
  772. cb = *buf++;
  773. alpha = *buf++;
  774. } else {
  775. y = buf[0] & 0xfc;
  776. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  777. cb = (buf[1] << 2) & 0xf0;
  778. alpha = (buf[1] << 6) & 0xc0;
  779. buf += 2;
  780. }
  781. if (y == 0)
  782. alpha = 0xff;
  783. YUV_TO_RGB1_CCIR(cb, cr);
  784. YUV_TO_RGB2_CCIR(r, g, b, y);
  785. av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  786. if (depth & 0x80)
  787. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  788. if (depth & 0x40)
  789. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  790. if (depth & 0x20)
  791. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  792. }
  793. }
  794. static void dvbsub_parse_region_segment(AVCodecContext *avctx,
  795. const uint8_t *buf, int buf_size)
  796. {
  797. DVBSubContext *ctx = avctx->priv_data;
  798. const uint8_t *buf_end = buf + buf_size;
  799. int region_id, object_id;
  800. DVBSubRegion *region;
  801. DVBSubObject *object;
  802. DVBSubObjectDisplay *display;
  803. int fill;
  804. if (buf_size < 10)
  805. return;
  806. region_id = *buf++;
  807. region = get_region(ctx, region_id);
  808. if (!region) {
  809. region = av_mallocz(sizeof(DVBSubRegion));
  810. region->id = region_id;
  811. region->next = ctx->region_list;
  812. ctx->region_list = region;
  813. }
  814. fill = ((*buf++) >> 3) & 1;
  815. region->width = AV_RB16(buf);
  816. buf += 2;
  817. region->height = AV_RB16(buf);
  818. buf += 2;
  819. if (region->width * region->height != region->buf_size) {
  820. av_free(region->pbuf);
  821. region->buf_size = region->width * region->height;
  822. region->pbuf = av_malloc(region->buf_size);
  823. fill = 1;
  824. }
  825. region->depth = 1 << (((*buf++) >> 2) & 7);
  826. if(region->depth<2 || region->depth>8){
  827. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  828. region->depth= 4;
  829. }
  830. region->clut = *buf++;
  831. if (region->depth == 8)
  832. region->bgcolor = *buf++;
  833. else {
  834. buf += 1;
  835. if (region->depth == 4)
  836. region->bgcolor = (((*buf++) >> 4) & 15);
  837. else
  838. region->bgcolor = (((*buf++) >> 2) & 3);
  839. }
  840. av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  841. if (fill) {
  842. memset(region->pbuf, region->bgcolor, region->buf_size);
  843. av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
  844. }
  845. delete_region_display_list(ctx, region);
  846. while (buf + 5 < buf_end) {
  847. object_id = AV_RB16(buf);
  848. buf += 2;
  849. object = get_object(ctx, object_id);
  850. if (!object) {
  851. object = av_mallocz(sizeof(DVBSubObject));
  852. object->id = object_id;
  853. object->next = ctx->object_list;
  854. ctx->object_list = object;
  855. }
  856. object->type = (*buf) >> 6;
  857. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  858. display->object_id = object_id;
  859. display->region_id = region_id;
  860. display->x_pos = AV_RB16(buf) & 0xfff;
  861. buf += 2;
  862. display->y_pos = AV_RB16(buf) & 0xfff;
  863. buf += 2;
  864. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  865. display->fgcolor = *buf++;
  866. display->bgcolor = *buf++;
  867. }
  868. display->region_list_next = region->display_list;
  869. region->display_list = display;
  870. display->object_list_next = object->display_list;
  871. object->display_list = display;
  872. }
  873. }
  874. static void dvbsub_parse_page_segment(AVCodecContext *avctx,
  875. const uint8_t *buf, int buf_size)
  876. {
  877. DVBSubContext *ctx = avctx->priv_data;
  878. DVBSubRegionDisplay *display;
  879. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  880. const uint8_t *buf_end = buf + buf_size;
  881. int region_id;
  882. int page_state;
  883. if (buf_size < 1)
  884. return;
  885. ctx->time_out = *buf++;
  886. page_state = ((*buf++) >> 2) & 3;
  887. av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  888. if (page_state == 2) {
  889. delete_state(ctx);
  890. }
  891. tmp_display_list = ctx->display_list;
  892. ctx->display_list = NULL;
  893. ctx->display_list_size = 0;
  894. while (buf + 5 < buf_end) {
  895. region_id = *buf++;
  896. buf += 1;
  897. display = tmp_display_list;
  898. tmp_ptr = &tmp_display_list;
  899. while (display && display->region_id != region_id) {
  900. tmp_ptr = &display->next;
  901. display = display->next;
  902. }
  903. if (!display)
  904. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  905. display->region_id = region_id;
  906. display->x_pos = AV_RB16(buf);
  907. buf += 2;
  908. display->y_pos = AV_RB16(buf);
  909. buf += 2;
  910. *tmp_ptr = display->next;
  911. display->next = ctx->display_list;
  912. ctx->display_list = display;
  913. ctx->display_list_size++;
  914. av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  915. }
  916. while (tmp_display_list) {
  917. display = tmp_display_list;
  918. tmp_display_list = display->next;
  919. av_free(display);
  920. }
  921. }
  922. #ifdef DEBUG
  923. static void save_display_set(DVBSubContext *ctx)
  924. {
  925. DVBSubRegion *region;
  926. DVBSubRegionDisplay *display;
  927. DVBSubCLUT *clut;
  928. uint32_t *clut_table;
  929. int x_pos, y_pos, width, height;
  930. int x, y, y_off, x_off;
  931. uint32_t *pbuf;
  932. char filename[32];
  933. static int fileno_index = 0;
  934. x_pos = -1;
  935. y_pos = -1;
  936. width = 0;
  937. height = 0;
  938. for (display = ctx->display_list; display; display = display->next) {
  939. region = get_region(ctx, display->region_id);
  940. if (x_pos == -1) {
  941. x_pos = display->x_pos;
  942. y_pos = display->y_pos;
  943. width = region->width;
  944. height = region->height;
  945. } else {
  946. if (display->x_pos < x_pos) {
  947. width += (x_pos - display->x_pos);
  948. x_pos = display->x_pos;
  949. }
  950. if (display->y_pos < y_pos) {
  951. height += (y_pos - display->y_pos);
  952. y_pos = display->y_pos;
  953. }
  954. if (display->x_pos + region->width > x_pos + width) {
  955. width = display->x_pos + region->width - x_pos;
  956. }
  957. if (display->y_pos + region->height > y_pos + height) {
  958. height = display->y_pos + region->height - y_pos;
  959. }
  960. }
  961. }
  962. if (x_pos >= 0) {
  963. pbuf = av_malloc(width * height * 4);
  964. for (display = ctx->display_list; display; display = display->next) {
  965. region = get_region(ctx, display->region_id);
  966. x_off = display->x_pos - x_pos;
  967. y_off = display->y_pos - y_pos;
  968. clut = get_clut(ctx, region->clut);
  969. if (clut == 0)
  970. clut = &default_clut;
  971. switch (region->depth) {
  972. case 2:
  973. clut_table = clut->clut4;
  974. break;
  975. case 8:
  976. clut_table = clut->clut256;
  977. break;
  978. case 4:
  979. default:
  980. clut_table = clut->clut16;
  981. break;
  982. }
  983. for (y = 0; y < region->height; y++) {
  984. for (x = 0; x < region->width; x++) {
  985. pbuf[((y + y_off) * width) + x_off + x] =
  986. clut_table[region->pbuf[y * region->width + x]];
  987. }
  988. }
  989. }
  990. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  991. png_save2(filename, pbuf, width, height);
  992. av_free(pbuf);
  993. }
  994. fileno_index++;
  995. }
  996. #endif
  997. static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
  998. const uint8_t *buf,
  999. int buf_size)
  1000. {
  1001. DVBSubContext *ctx = avctx->priv_data;
  1002. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1003. int dds_version, info_byte;
  1004. if (buf_size < 5)
  1005. return;
  1006. info_byte = bytestream_get_byte(&buf);
  1007. dds_version = info_byte >> 4;
  1008. if (display_def && display_def->version == dds_version)
  1009. return; // already have this display definition version
  1010. if (!display_def) {
  1011. display_def = av_mallocz(sizeof(*display_def));
  1012. ctx->display_definition = display_def;
  1013. }
  1014. if (!display_def)
  1015. return;
  1016. display_def->version = dds_version;
  1017. display_def->x = 0;
  1018. display_def->y = 0;
  1019. display_def->width = bytestream_get_be16(&buf) + 1;
  1020. display_def->height = bytestream_get_be16(&buf) + 1;
  1021. if (buf_size < 13)
  1022. return;
  1023. if (info_byte & 1<<3) { // display_window_flag
  1024. display_def->x = bytestream_get_be16(&buf);
  1025. display_def->y = bytestream_get_be16(&buf);
  1026. display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
  1027. display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
  1028. }
  1029. }
  1030. static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1031. int buf_size, AVSubtitle *sub)
  1032. {
  1033. DVBSubContext *ctx = avctx->priv_data;
  1034. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1035. DVBSubRegion *region;
  1036. DVBSubRegionDisplay *display;
  1037. AVSubtitleRect *rect;
  1038. DVBSubCLUT *clut;
  1039. uint32_t *clut_table;
  1040. int i;
  1041. int offset_x=0, offset_y=0;
  1042. sub->rects = NULL;
  1043. sub->start_display_time = 0;
  1044. sub->end_display_time = ctx->time_out * 1000;
  1045. sub->format = 0;
  1046. if (display_def) {
  1047. offset_x = display_def->x;
  1048. offset_y = display_def->y;
  1049. }
  1050. sub->num_rects = ctx->display_list_size;
  1051. if (sub->num_rects <= 0)
  1052. return AVERROR_INVALIDDATA;
  1053. sub->rects = av_mallocz_array(sub->num_rects * sub->num_rects,
  1054. sizeof(*sub->rects));
  1055. if (!sub->rects)
  1056. return AVERROR(ENOMEM);
  1057. i = 0;
  1058. for (display = ctx->display_list; display; display = display->next) {
  1059. region = get_region(ctx, display->region_id);
  1060. rect = sub->rects[i];
  1061. if (!region)
  1062. continue;
  1063. rect->x = display->x_pos + offset_x;
  1064. rect->y = display->y_pos + offset_y;
  1065. rect->w = region->width;
  1066. rect->h = region->height;
  1067. rect->nb_colors = 16;
  1068. rect->type = SUBTITLE_BITMAP;
  1069. rect->pict.linesize[0] = region->width;
  1070. clut = get_clut(ctx, region->clut);
  1071. if (!clut)
  1072. clut = &default_clut;
  1073. switch (region->depth) {
  1074. case 2:
  1075. clut_table = clut->clut4;
  1076. break;
  1077. case 8:
  1078. clut_table = clut->clut256;
  1079. break;
  1080. case 4:
  1081. default:
  1082. clut_table = clut->clut16;
  1083. break;
  1084. }
  1085. rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  1086. if (!rect->pict.data[1]) {
  1087. av_free(sub->rects);
  1088. return AVERROR(ENOMEM);
  1089. }
  1090. memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
  1091. rect->pict.data[0] = av_malloc(region->buf_size);
  1092. if (!rect->pict.data[0]) {
  1093. av_free(rect->pict.data[1]);
  1094. av_free(sub->rects);
  1095. return AVERROR(ENOMEM);
  1096. }
  1097. memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
  1098. i++;
  1099. }
  1100. sub->num_rects = i;
  1101. #ifdef DEBUG
  1102. save_display_set(ctx);
  1103. #endif
  1104. return 1;
  1105. }
  1106. static int dvbsub_decode(AVCodecContext *avctx,
  1107. void *data, int *data_size,
  1108. AVPacket *avpkt)
  1109. {
  1110. const uint8_t *buf = avpkt->data;
  1111. int buf_size = avpkt->size;
  1112. DVBSubContext *ctx = avctx->priv_data;
  1113. AVSubtitle *sub = data;
  1114. const uint8_t *p, *p_end;
  1115. int segment_type;
  1116. int page_id;
  1117. int segment_length;
  1118. int i;
  1119. av_dlog(avctx, "DVB sub packet:\n");
  1120. for (i=0; i < buf_size; i++) {
  1121. av_dlog(avctx, "%02x ", buf[i]);
  1122. if (i % 16 == 15)
  1123. av_dlog(avctx, "\n");
  1124. }
  1125. if (i % 16)
  1126. av_dlog(avctx, "\n");
  1127. if (buf_size <= 6 || *buf != 0x0f) {
  1128. av_dlog(avctx, "incomplete or broken packet");
  1129. return -1;
  1130. }
  1131. p = buf;
  1132. p_end = buf + buf_size;
  1133. while (p_end - p >= 6 && *p == 0x0f) {
  1134. p += 1;
  1135. segment_type = *p++;
  1136. page_id = AV_RB16(p);
  1137. p += 2;
  1138. segment_length = AV_RB16(p);
  1139. p += 2;
  1140. if (p_end - p < segment_length) {
  1141. av_dlog(avctx, "incomplete or broken packet");
  1142. return -1;
  1143. }
  1144. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
  1145. ctx->composition_id == -1 || ctx->ancillary_id == -1) {
  1146. switch (segment_type) {
  1147. case DVBSUB_PAGE_SEGMENT:
  1148. dvbsub_parse_page_segment(avctx, p, segment_length);
  1149. break;
  1150. case DVBSUB_REGION_SEGMENT:
  1151. dvbsub_parse_region_segment(avctx, p, segment_length);
  1152. break;
  1153. case DVBSUB_CLUT_SEGMENT:
  1154. dvbsub_parse_clut_segment(avctx, p, segment_length);
  1155. break;
  1156. case DVBSUB_OBJECT_SEGMENT:
  1157. dvbsub_parse_object_segment(avctx, p, segment_length);
  1158. break;
  1159. case DVBSUB_DISPLAYDEFINITION_SEGMENT:
  1160. dvbsub_parse_display_definition_segment(avctx, p, segment_length);
  1161. break;
  1162. case DVBSUB_DISPLAY_SEGMENT:
  1163. *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
  1164. break;
  1165. default:
  1166. av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1167. segment_type, page_id, segment_length);
  1168. break;
  1169. }
  1170. }
  1171. p += segment_length;
  1172. }
  1173. return p - buf;
  1174. }
  1175. AVCodec ff_dvbsub_decoder = {
  1176. .name = "dvbsub",
  1177. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1178. .type = AVMEDIA_TYPE_SUBTITLE,
  1179. .id = AV_CODEC_ID_DVB_SUBTITLE,
  1180. .priv_data_size = sizeof(DVBSubContext),
  1181. .init = dvbsub_init_decoder,
  1182. .close = dvbsub_close_decoder,
  1183. .decode = dvbsub_decode,
  1184. };