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.

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