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.

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