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.

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