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.

1520 lines
41KB

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