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.

1462 lines
40KB

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