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.

1504 lines
41KB

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