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.

1462 lines
39KB

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