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.

1527 lines
41KB

  1. /*
  2. * DVB subtitle decoding for ffmpeg
  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) (((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)
  375. {
  376. GetBitContext gb;
  377. int bits;
  378. int run_length;
  379. int pixels_read = 0;
  380. init_get_bits(&gb, *srcbuf, buf_size << 3);
  381. while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
  382. bits = get_bits(&gb, 2);
  383. if (bits) {
  384. if (non_mod != 1 || bits != 1) {
  385. if (map_table)
  386. *destbuf++ = map_table[bits];
  387. else
  388. *destbuf++ = bits;
  389. }
  390. pixels_read++;
  391. } else {
  392. bits = get_bits1(&gb);
  393. if (bits == 1) {
  394. run_length = get_bits(&gb, 3) + 3;
  395. bits = get_bits(&gb, 2);
  396. if (non_mod == 1 && bits == 1)
  397. pixels_read += run_length;
  398. else {
  399. if (map_table)
  400. bits = map_table[bits];
  401. while (run_length-- > 0 && pixels_read < dbuf_len) {
  402. *destbuf++ = bits;
  403. pixels_read++;
  404. }
  405. }
  406. } else {
  407. bits = get_bits1(&gb);
  408. if (bits == 0) {
  409. bits = get_bits(&gb, 2);
  410. if (bits == 2) {
  411. run_length = get_bits(&gb, 4) + 12;
  412. bits = get_bits(&gb, 2);
  413. if (non_mod == 1 && bits == 1)
  414. pixels_read += run_length;
  415. else {
  416. if (map_table)
  417. bits = map_table[bits];
  418. while (run_length-- > 0 && pixels_read < dbuf_len) {
  419. *destbuf++ = bits;
  420. pixels_read++;
  421. }
  422. }
  423. } else if (bits == 3) {
  424. run_length = get_bits(&gb, 8) + 29;
  425. bits = get_bits(&gb, 2);
  426. if (non_mod == 1 && bits == 1)
  427. pixels_read += run_length;
  428. else {
  429. if (map_table)
  430. bits = map_table[bits];
  431. while (run_length-- > 0 && pixels_read < dbuf_len) {
  432. *destbuf++ = bits;
  433. pixels_read++;
  434. }
  435. }
  436. } else if (bits == 1) {
  437. if (map_table)
  438. bits = map_table[0];
  439. else
  440. bits = 0;
  441. run_length = 2;
  442. while (run_length-- > 0 && pixels_read < dbuf_len) {
  443. *destbuf++ = bits;
  444. pixels_read++;
  445. }
  446. } else {
  447. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  448. return pixels_read;
  449. }
  450. } else {
  451. if (map_table)
  452. bits = map_table[0];
  453. else
  454. bits = 0;
  455. *destbuf++ = bits;
  456. pixels_read++;
  457. }
  458. }
  459. }
  460. }
  461. if (get_bits(&gb, 6))
  462. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  463. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  464. return pixels_read;
  465. }
  466. static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
  467. const uint8_t **srcbuf, int buf_size,
  468. int non_mod, uint8_t *map_table)
  469. {
  470. GetBitContext gb;
  471. int bits;
  472. int run_length;
  473. int pixels_read = 0;
  474. init_get_bits(&gb, *srcbuf, buf_size << 3);
  475. while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
  476. bits = get_bits(&gb, 4);
  477. if (bits) {
  478. if (non_mod != 1 || bits != 1) {
  479. if (map_table)
  480. *destbuf++ = map_table[bits];
  481. else
  482. *destbuf++ = bits;
  483. }
  484. pixels_read++;
  485. } else {
  486. bits = get_bits1(&gb);
  487. if (bits == 0) {
  488. run_length = get_bits(&gb, 3);
  489. if (run_length == 0) {
  490. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  491. return pixels_read;
  492. }
  493. run_length += 2;
  494. if (map_table)
  495. bits = map_table[0];
  496. else
  497. bits = 0;
  498. while (run_length-- > 0 && pixels_read < dbuf_len) {
  499. *destbuf++ = bits;
  500. pixels_read++;
  501. }
  502. } else {
  503. bits = get_bits1(&gb);
  504. if (bits == 0) {
  505. run_length = get_bits(&gb, 2) + 4;
  506. bits = get_bits(&gb, 4);
  507. if (non_mod == 1 && bits == 1)
  508. pixels_read += run_length;
  509. else {
  510. if (map_table)
  511. bits = map_table[bits];
  512. while (run_length-- > 0 && pixels_read < dbuf_len) {
  513. *destbuf++ = bits;
  514. pixels_read++;
  515. }
  516. }
  517. } else {
  518. bits = get_bits(&gb, 2);
  519. if (bits == 2) {
  520. run_length = get_bits(&gb, 4) + 9;
  521. bits = get_bits(&gb, 4);
  522. if (non_mod == 1 && bits == 1)
  523. pixels_read += run_length;
  524. else {
  525. if (map_table)
  526. bits = map_table[bits];
  527. while (run_length-- > 0 && pixels_read < dbuf_len) {
  528. *destbuf++ = bits;
  529. pixels_read++;
  530. }
  531. }
  532. } else if (bits == 3) {
  533. run_length = get_bits(&gb, 8) + 25;
  534. bits = get_bits(&gb, 4);
  535. if (non_mod == 1 && bits == 1)
  536. pixels_read += run_length;
  537. else {
  538. if (map_table)
  539. bits = map_table[bits];
  540. while (run_length-- > 0 && pixels_read < dbuf_len) {
  541. *destbuf++ = bits;
  542. pixels_read++;
  543. }
  544. }
  545. } else if (bits == 1) {
  546. if (map_table)
  547. bits = map_table[0];
  548. else
  549. bits = 0;
  550. run_length = 2;
  551. while (run_length-- > 0 && pixels_read < dbuf_len) {
  552. *destbuf++ = bits;
  553. pixels_read++;
  554. }
  555. } else {
  556. if (map_table)
  557. bits = map_table[0];
  558. else
  559. bits = 0;
  560. *destbuf++ = bits;
  561. pixels_read ++;
  562. }
  563. }
  564. }
  565. }
  566. }
  567. if (get_bits(&gb, 8))
  568. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  569. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  570. return pixels_read;
  571. }
  572. static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
  573. const uint8_t **srcbuf, int buf_size,
  574. int non_mod, uint8_t *map_table)
  575. {
  576. const uint8_t *sbuf_end = (*srcbuf) + buf_size;
  577. int bits;
  578. int run_length;
  579. int pixels_read = 0;
  580. while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
  581. bits = *(*srcbuf)++;
  582. if (bits) {
  583. if (non_mod != 1 || bits != 1) {
  584. if (map_table)
  585. *destbuf++ = map_table[bits];
  586. else
  587. *destbuf++ = bits;
  588. }
  589. pixels_read++;
  590. } else {
  591. bits = *(*srcbuf)++;
  592. run_length = bits & 0x7f;
  593. if ((bits & 0x80) == 0) {
  594. if (run_length == 0) {
  595. return pixels_read;
  596. }
  597. if (map_table)
  598. bits = map_table[0];
  599. else
  600. bits = 0;
  601. while (run_length-- > 0 && pixels_read < dbuf_len) {
  602. *destbuf++ = bits;
  603. pixels_read++;
  604. }
  605. } else {
  606. bits = *(*srcbuf)++;
  607. if (non_mod == 1 && bits == 1)
  608. pixels_read += run_length;
  609. if (map_table)
  610. bits = map_table[bits];
  611. else while (run_length-- > 0 && pixels_read < dbuf_len) {
  612. *destbuf++ = bits;
  613. pixels_read++;
  614. }
  615. }
  616. }
  617. }
  618. if (*(*srcbuf)++)
  619. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  620. return pixels_read;
  621. }
  622. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  623. const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  624. {
  625. DVBSubContext *ctx = avctx->priv_data;
  626. DVBSubRegion *region = get_region(ctx, display->region_id);
  627. const uint8_t *buf_end = buf + buf_size;
  628. uint8_t *pbuf;
  629. int x_pos, y_pos;
  630. int i;
  631. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  632. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  633. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  634. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  635. uint8_t *map_table;
  636. #if 0
  637. av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
  638. top_bottom ? "bottom" : "top");
  639. for (i = 0; i < buf_size; i++) {
  640. if (i % 16 == 0)
  641. av_dlog(avctx, "0x%8p: ", buf+i);
  642. av_dlog(avctx, "%02x ", buf[i]);
  643. if (i % 16 == 15)
  644. av_dlog(avctx, "\n");
  645. }
  646. if (i % 16)
  647. av_dlog(avctx, "\n");
  648. #endif
  649. if (region == 0)
  650. return;
  651. pbuf = region->pbuf;
  652. region->dirty = 1;
  653. x_pos = display->x_pos;
  654. y_pos = display->y_pos;
  655. y_pos += top_bottom;
  656. while (buf < buf_end) {
  657. if (x_pos >= region->width || y_pos >= region->height) {
  658. av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
  659. return;
  660. }
  661. switch (*buf++) {
  662. case 0x10:
  663. if (region->depth == 8)
  664. map_table = map2to8;
  665. else if (region->depth == 4)
  666. map_table = map2to4;
  667. else
  668. map_table = NULL;
  669. x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
  670. region->width - x_pos, &buf, buf_end - buf,
  671. non_mod, map_table);
  672. break;
  673. case 0x11:
  674. if (region->depth < 4) {
  675. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  676. return;
  677. }
  678. if (region->depth == 8)
  679. map_table = map4to8;
  680. else
  681. map_table = NULL;
  682. x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
  683. region->width - x_pos, &buf, buf_end - buf,
  684. non_mod, map_table);
  685. break;
  686. case 0x12:
  687. if (region->depth < 8) {
  688. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  689. return;
  690. }
  691. x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
  692. region->width - x_pos, &buf, buf_end - buf,
  693. non_mod, NULL);
  694. break;
  695. case 0x20:
  696. map2to4[0] = (*buf) >> 4;
  697. map2to4[1] = (*buf++) & 0xf;
  698. map2to4[2] = (*buf) >> 4;
  699. map2to4[3] = (*buf++) & 0xf;
  700. break;
  701. case 0x21:
  702. for (i = 0; i < 4; i++)
  703. map2to8[i] = *buf++;
  704. break;
  705. case 0x22:
  706. for (i = 0; i < 16; i++)
  707. map4to8[i] = *buf++;
  708. break;
  709. case 0xf0:
  710. x_pos = display->x_pos;
  711. y_pos += 2;
  712. break;
  713. default:
  714. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  715. }
  716. }
  717. }
  718. static void dvbsub_parse_object_segment(AVCodecContext *avctx,
  719. const uint8_t *buf, int buf_size)
  720. {
  721. DVBSubContext *ctx = avctx->priv_data;
  722. const uint8_t *buf_end = buf + buf_size;
  723. int object_id;
  724. DVBSubObject *object;
  725. DVBSubObjectDisplay *display;
  726. int top_field_len, bottom_field_len;
  727. int coding_method, non_modifying_color;
  728. object_id = AV_RB16(buf);
  729. buf += 2;
  730. object = get_object(ctx, object_id);
  731. if (!object)
  732. return;
  733. coding_method = ((*buf) >> 2) & 3;
  734. non_modifying_color = ((*buf++) >> 1) & 1;
  735. if (coding_method == 0) {
  736. top_field_len = AV_RB16(buf);
  737. buf += 2;
  738. bottom_field_len = AV_RB16(buf);
  739. buf += 2;
  740. if (buf + top_field_len + bottom_field_len > buf_end) {
  741. av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
  742. return;
  743. }
  744. for (display = object->display_list; display; display = display->object_list_next) {
  745. const uint8_t *block = buf;
  746. int bfl = bottom_field_len;
  747. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  748. non_modifying_color);
  749. if (bottom_field_len > 0)
  750. block = buf + top_field_len;
  751. else
  752. bfl = top_field_len;
  753. dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
  754. non_modifying_color);
  755. }
  756. /* } else if (coding_method == 1) {*/
  757. } else {
  758. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  759. }
  760. }
  761. static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
  762. const uint8_t *buf, int buf_size)
  763. {
  764. DVBSubContext *ctx = avctx->priv_data;
  765. const uint8_t *buf_end = buf + buf_size;
  766. int i, clut_id;
  767. int version;
  768. DVBSubCLUT *clut;
  769. int entry_id, depth , full_range;
  770. int y, cr, cb, alpha;
  771. int r, g, b, r_add, g_add, b_add;
  772. av_dlog(avctx, "DVB clut packet:\n");
  773. for (i=0; i < buf_size; i++) {
  774. av_dlog(avctx, "%02x ", buf[i]);
  775. if (i % 16 == 15)
  776. av_dlog(avctx, "\n");
  777. }
  778. if (i % 16)
  779. av_dlog(avctx, "\n");
  780. clut_id = *buf++;
  781. version = ((*buf)>>4)&15;
  782. buf += 1;
  783. clut = get_clut(ctx, clut_id);
  784. if (!clut) {
  785. clut = av_malloc(sizeof(DVBSubCLUT));
  786. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  787. clut->id = clut_id;
  788. clut->version = -1;
  789. clut->next = ctx->clut_list;
  790. ctx->clut_list = clut;
  791. }
  792. if (clut->version != version) {
  793. clut->version = version;
  794. while (buf + 4 < buf_end) {
  795. entry_id = *buf++;
  796. depth = (*buf) & 0xe0;
  797. if (depth == 0) {
  798. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  799. return;
  800. }
  801. full_range = (*buf++) & 1;
  802. if (full_range) {
  803. y = *buf++;
  804. cr = *buf++;
  805. cb = *buf++;
  806. alpha = *buf++;
  807. } else {
  808. y = buf[0] & 0xfc;
  809. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  810. cb = (buf[1] << 2) & 0xf0;
  811. alpha = (buf[1] << 6) & 0xc0;
  812. buf += 2;
  813. }
  814. if (y == 0)
  815. alpha = 0xff;
  816. YUV_TO_RGB1_CCIR(cb, cr);
  817. YUV_TO_RGB2_CCIR(r, g, b, y);
  818. av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  819. if (depth & 0x80)
  820. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  821. if (depth & 0x40)
  822. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  823. if (depth & 0x20)
  824. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  825. }
  826. }
  827. }
  828. static void dvbsub_parse_region_segment(AVCodecContext *avctx,
  829. const uint8_t *buf, int buf_size)
  830. {
  831. DVBSubContext *ctx = avctx->priv_data;
  832. const uint8_t *buf_end = buf + buf_size;
  833. int region_id, object_id;
  834. int version;
  835. DVBSubRegion *region;
  836. DVBSubObject *object;
  837. DVBSubObjectDisplay *display;
  838. int fill;
  839. if (buf_size < 10)
  840. return;
  841. region_id = *buf++;
  842. region = get_region(ctx, region_id);
  843. if (!region) {
  844. region = av_mallocz(sizeof(DVBSubRegion));
  845. region->id = region_id;
  846. region->version = -1;
  847. region->next = ctx->region_list;
  848. ctx->region_list = region;
  849. }
  850. version = ((*buf)>>4) & 15;
  851. fill = ((*buf++) >> 3) & 1;
  852. region->width = AV_RB16(buf);
  853. buf += 2;
  854. region->height = AV_RB16(buf);
  855. buf += 2;
  856. if (region->width * region->height != region->buf_size) {
  857. av_free(region->pbuf);
  858. region->buf_size = region->width * region->height;
  859. region->pbuf = av_malloc(region->buf_size);
  860. fill = 1;
  861. region->dirty = 0;
  862. }
  863. region->depth = 1 << (((*buf++) >> 2) & 7);
  864. if(region->depth<2 || region->depth>8){
  865. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  866. region->depth= 4;
  867. }
  868. region->clut = *buf++;
  869. if (region->depth == 8) {
  870. region->bgcolor = *buf++;
  871. buf += 1;
  872. } else {
  873. buf += 1;
  874. if (region->depth == 4)
  875. region->bgcolor = (((*buf++) >> 4) & 15);
  876. else
  877. region->bgcolor = (((*buf++) >> 2) & 3);
  878. }
  879. av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  880. if (fill) {
  881. memset(region->pbuf, region->bgcolor, region->buf_size);
  882. av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
  883. }
  884. delete_region_display_list(ctx, region);
  885. while (buf + 5 < buf_end) {
  886. object_id = AV_RB16(buf);
  887. buf += 2;
  888. object = get_object(ctx, object_id);
  889. if (!object) {
  890. object = av_mallocz(sizeof(DVBSubObject));
  891. object->id = object_id;
  892. object->next = ctx->object_list;
  893. ctx->object_list = object;
  894. }
  895. object->type = (*buf) >> 6;
  896. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  897. display->object_id = object_id;
  898. display->region_id = region_id;
  899. display->x_pos = AV_RB16(buf) & 0xfff;
  900. buf += 2;
  901. display->y_pos = AV_RB16(buf) & 0xfff;
  902. buf += 2;
  903. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  904. display->fgcolor = *buf++;
  905. display->bgcolor = *buf++;
  906. }
  907. display->region_list_next = region->display_list;
  908. region->display_list = display;
  909. display->object_list_next = object->display_list;
  910. object->display_list = display;
  911. }
  912. }
  913. static void dvbsub_parse_page_segment(AVCodecContext *avctx,
  914. const uint8_t *buf, int buf_size)
  915. {
  916. DVBSubContext *ctx = avctx->priv_data;
  917. DVBSubRegionDisplay *display;
  918. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  919. const uint8_t *buf_end = buf + buf_size;
  920. int region_id;
  921. int page_state;
  922. int timeout;
  923. int version;
  924. if (buf_size < 1)
  925. return;
  926. timeout = *buf++;
  927. version = ((*buf)>>4) & 15;
  928. page_state = ((*buf++) >> 2) & 3;
  929. if (ctx->version != version) {
  930. ctx->time_out = timeout;
  931. ctx->version = version;
  932. av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  933. if (page_state == 1 || page_state == 2) {
  934. delete_regions(ctx);
  935. delete_objects(ctx);
  936. delete_cluts(ctx);
  937. }
  938. tmp_display_list = ctx->display_list;
  939. ctx->display_list = NULL;
  940. ctx->display_list_size = 0;
  941. while (buf + 5 < buf_end) {
  942. region_id = *buf++;
  943. buf += 1;
  944. display = tmp_display_list;
  945. tmp_ptr = &tmp_display_list;
  946. while (display && display->region_id != region_id) {
  947. tmp_ptr = &display->next;
  948. display = display->next;
  949. }
  950. if (!display)
  951. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  952. display->region_id = region_id;
  953. display->x_pos = AV_RB16(buf);
  954. buf += 2;
  955. display->y_pos = AV_RB16(buf);
  956. buf += 2;
  957. *tmp_ptr = display->next;
  958. display->next = ctx->display_list;
  959. ctx->display_list = display;
  960. ctx->display_list_size++;
  961. av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  962. }
  963. while (tmp_display_list) {
  964. display = tmp_display_list;
  965. tmp_display_list = display->next;
  966. av_free(display);
  967. }
  968. }
  969. }
  970. #ifdef DEBUG
  971. static void save_display_set(DVBSubContext *ctx)
  972. {
  973. DVBSubRegion *region;
  974. DVBSubRegionDisplay *display;
  975. DVBSubCLUT *clut;
  976. uint32_t *clut_table;
  977. int x_pos, y_pos, width, height;
  978. int x, y, y_off, x_off;
  979. uint32_t *pbuf;
  980. char filename[32];
  981. static int fileno_index = 0;
  982. x_pos = -1;
  983. y_pos = -1;
  984. width = 0;
  985. height = 0;
  986. for (display = ctx->display_list; display; display = display->next) {
  987. region = get_region(ctx, display->region_id);
  988. if (x_pos == -1) {
  989. x_pos = display->x_pos;
  990. y_pos = display->y_pos;
  991. width = region->width;
  992. height = region->height;
  993. } else {
  994. if (display->x_pos < x_pos) {
  995. width += (x_pos - display->x_pos);
  996. x_pos = display->x_pos;
  997. }
  998. if (display->y_pos < y_pos) {
  999. height += (y_pos - display->y_pos);
  1000. y_pos = display->y_pos;
  1001. }
  1002. if (display->x_pos + region->width > x_pos + width) {
  1003. width = display->x_pos + region->width - x_pos;
  1004. }
  1005. if (display->y_pos + region->height > y_pos + height) {
  1006. height = display->y_pos + region->height - y_pos;
  1007. }
  1008. }
  1009. }
  1010. if (x_pos >= 0) {
  1011. pbuf = av_malloc(width * height * 4);
  1012. for (display = ctx->display_list; display; display = display->next) {
  1013. region = get_region(ctx, display->region_id);
  1014. x_off = display->x_pos - x_pos;
  1015. y_off = display->y_pos - y_pos;
  1016. clut = get_clut(ctx, region->clut);
  1017. if (clut == 0)
  1018. clut = &default_clut;
  1019. switch (region->depth) {
  1020. case 2:
  1021. clut_table = clut->clut4;
  1022. break;
  1023. case 8:
  1024. clut_table = clut->clut256;
  1025. break;
  1026. case 4:
  1027. default:
  1028. clut_table = clut->clut16;
  1029. break;
  1030. }
  1031. for (y = 0; y < region->height; y++) {
  1032. for (x = 0; x < region->width; x++) {
  1033. pbuf[((y + y_off) * width) + x_off + x] =
  1034. clut_table[region->pbuf[y * region->width + x]];
  1035. }
  1036. }
  1037. }
  1038. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  1039. png_save2(filename, pbuf, width, height);
  1040. av_free(pbuf);
  1041. }
  1042. fileno_index++;
  1043. }
  1044. #endif
  1045. static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
  1046. const uint8_t *buf,
  1047. int buf_size)
  1048. {
  1049. DVBSubContext *ctx = avctx->priv_data;
  1050. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1051. int dds_version, info_byte;
  1052. if (buf_size < 5)
  1053. return;
  1054. info_byte = bytestream_get_byte(&buf);
  1055. dds_version = info_byte >> 4;
  1056. if (display_def && display_def->version == dds_version)
  1057. return; // already have this display definition version
  1058. if (!display_def) {
  1059. display_def = av_mallocz(sizeof(*display_def));
  1060. ctx->display_definition = display_def;
  1061. }
  1062. if (!display_def)
  1063. return;
  1064. display_def->version = dds_version;
  1065. display_def->x = 0;
  1066. display_def->y = 0;
  1067. display_def->width = bytestream_get_be16(&buf) + 1;
  1068. display_def->height = bytestream_get_be16(&buf) + 1;
  1069. if (buf_size < 13)
  1070. return;
  1071. if (info_byte & 1<<3) { // display_window_flag
  1072. display_def->x = bytestream_get_be16(&buf);
  1073. display_def->y = bytestream_get_be16(&buf);
  1074. display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
  1075. display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
  1076. }
  1077. }
  1078. static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1079. int buf_size, AVSubtitle *sub)
  1080. {
  1081. DVBSubContext *ctx = avctx->priv_data;
  1082. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1083. DVBSubRegion *region;
  1084. DVBSubRegionDisplay *display;
  1085. AVSubtitleRect *rect;
  1086. DVBSubCLUT *clut;
  1087. uint32_t *clut_table;
  1088. int i;
  1089. int offset_x=0, offset_y=0;
  1090. sub->end_display_time = ctx->time_out * 1000;
  1091. if (display_def) {
  1092. offset_x = display_def->x;
  1093. offset_y = display_def->y;
  1094. }
  1095. sub->num_rects = ctx->display_list_size;
  1096. if (sub->num_rects > 0){
  1097. sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
  1098. for(i=0; i<sub->num_rects; i++)
  1099. sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
  1100. i = 0;
  1101. for (display = ctx->display_list; display; display = display->next) {
  1102. region = get_region(ctx, display->region_id);
  1103. if (!region)
  1104. continue;
  1105. if (!region->dirty)
  1106. continue;
  1107. rect = sub->rects[i];
  1108. rect->x = display->x_pos + offset_x;
  1109. rect->y = display->y_pos + offset_y;
  1110. rect->w = region->width;
  1111. rect->h = region->height;
  1112. rect->nb_colors = (1 << region->depth);
  1113. rect->type = SUBTITLE_BITMAP;
  1114. rect->pict.linesize[0] = region->width;
  1115. clut = get_clut(ctx, region->clut);
  1116. if (!clut)
  1117. clut = &default_clut;
  1118. switch (region->depth) {
  1119. case 2:
  1120. clut_table = clut->clut4;
  1121. break;
  1122. case 8:
  1123. clut_table = clut->clut256;
  1124. break;
  1125. case 4:
  1126. default:
  1127. clut_table = clut->clut16;
  1128. break;
  1129. }
  1130. rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
  1131. memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
  1132. rect->pict.data[0] = av_malloc(region->buf_size);
  1133. memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
  1134. i++;
  1135. }
  1136. sub->num_rects = i;
  1137. }
  1138. #ifdef DEBUG
  1139. save_display_set(ctx);
  1140. #endif
  1141. return 1;
  1142. }
  1143. static int dvbsub_decode(AVCodecContext *avctx,
  1144. void *data, int *data_size,
  1145. AVPacket *avpkt)
  1146. {
  1147. const uint8_t *buf = avpkt->data;
  1148. int buf_size = avpkt->size;
  1149. DVBSubContext *ctx = avctx->priv_data;
  1150. AVSubtitle *sub = data;
  1151. const uint8_t *p, *p_end;
  1152. int segment_type;
  1153. int page_id;
  1154. int segment_length;
  1155. int i;
  1156. av_dlog(avctx, "DVB sub packet:\n");
  1157. for (i=0; i < buf_size; i++) {
  1158. av_dlog(avctx, "%02x ", buf[i]);
  1159. if (i % 16 == 15)
  1160. av_dlog(avctx, "\n");
  1161. }
  1162. if (i % 16)
  1163. av_dlog(avctx, "\n");
  1164. if (buf_size <= 6 || *buf != 0x0f) {
  1165. av_dlog(avctx, "incomplete or broken packet");
  1166. return -1;
  1167. }
  1168. p = buf;
  1169. p_end = buf + buf_size;
  1170. while (p_end - p >= 6 && *p == 0x0f) {
  1171. p += 1;
  1172. segment_type = *p++;
  1173. page_id = AV_RB16(p);
  1174. p += 2;
  1175. segment_length = AV_RB16(p);
  1176. p += 2;
  1177. if (p_end - p < segment_length) {
  1178. av_dlog(avctx, "incomplete or broken packet");
  1179. return -1;
  1180. }
  1181. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
  1182. ctx->composition_id == -1 || ctx->ancillary_id == -1) {
  1183. switch (segment_type) {
  1184. case DVBSUB_PAGE_SEGMENT:
  1185. dvbsub_parse_page_segment(avctx, p, segment_length);
  1186. break;
  1187. case DVBSUB_REGION_SEGMENT:
  1188. dvbsub_parse_region_segment(avctx, p, segment_length);
  1189. break;
  1190. case DVBSUB_CLUT_SEGMENT:
  1191. dvbsub_parse_clut_segment(avctx, p, segment_length);
  1192. break;
  1193. case DVBSUB_OBJECT_SEGMENT:
  1194. dvbsub_parse_object_segment(avctx, p, segment_length);
  1195. break;
  1196. case DVBSUB_DISPLAYDEFINITION_SEGMENT:
  1197. dvbsub_parse_display_definition_segment(avctx, p, segment_length);
  1198. break;
  1199. case DVBSUB_DISPLAY_SEGMENT:
  1200. *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
  1201. break;
  1202. default:
  1203. av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1204. segment_type, page_id, segment_length);
  1205. break;
  1206. }
  1207. }
  1208. p += segment_length;
  1209. }
  1210. return p - buf;
  1211. }
  1212. AVCodec ff_dvbsub_decoder = {
  1213. .name = "dvbsub",
  1214. .type = AVMEDIA_TYPE_SUBTITLE,
  1215. .id = CODEC_ID_DVB_SUBTITLE,
  1216. .priv_data_size = sizeof(DVBSubContext),
  1217. .init = dvbsub_init_decoder,
  1218. .close = dvbsub_close_decoder,
  1219. .decode = dvbsub_decode,
  1220. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1221. };