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.

1558 lines
42KB

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