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.

1433 lines
38KB

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