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.

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