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.

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