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.

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