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.

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