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.

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