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.

1543 lines
41KB

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