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.

1493 lines
40KB

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