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.

1494 lines
40KB

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