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.

1476 lines
40KB

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