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.

1756 lines
49KB

  1. /*
  2. * DVB subtitle decoding
  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 "get_bits.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. #include "libavutil/colorspace.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/opt.h"
  28. #define DVBSUB_PAGE_SEGMENT 0x10
  29. #define DVBSUB_REGION_SEGMENT 0x11
  30. #define DVBSUB_CLUT_SEGMENT 0x12
  31. #define DVBSUB_OBJECT_SEGMENT 0x13
  32. #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
  33. #define DVBSUB_DISPLAY_SEGMENT 0x80
  34. #define cm (ff_crop_tab + MAX_NEG_CROP)
  35. #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
  36. typedef struct DVBSubCLUT {
  37. int id;
  38. int version;
  39. uint32_t clut4[4];
  40. uint32_t clut16[16];
  41. uint32_t clut256[256];
  42. struct DVBSubCLUT *next;
  43. } DVBSubCLUT;
  44. static DVBSubCLUT default_clut;
  45. typedef struct DVBSubObjectDisplay {
  46. int object_id;
  47. int region_id;
  48. int x_pos;
  49. int y_pos;
  50. int fgcolor;
  51. int bgcolor;
  52. struct DVBSubObjectDisplay *region_list_next;
  53. struct DVBSubObjectDisplay *object_list_next;
  54. } DVBSubObjectDisplay;
  55. typedef struct DVBSubObject {
  56. int id;
  57. int version;
  58. int type;
  59. DVBSubObjectDisplay *display_list;
  60. struct DVBSubObject *next;
  61. } DVBSubObject;
  62. typedef struct DVBSubRegionDisplay {
  63. int region_id;
  64. int x_pos;
  65. int y_pos;
  66. struct DVBSubRegionDisplay *next;
  67. } DVBSubRegionDisplay;
  68. typedef struct DVBSubRegion {
  69. int id;
  70. int version;
  71. int width;
  72. int height;
  73. int depth;
  74. int clut;
  75. int bgcolor;
  76. uint8_t computed_clut[4*256];
  77. int has_computed_clut;
  78. uint8_t *pbuf;
  79. int buf_size;
  80. int dirty;
  81. DVBSubObjectDisplay *display_list;
  82. struct DVBSubRegion *next;
  83. } DVBSubRegion;
  84. typedef struct DVBSubDisplayDefinition {
  85. int version;
  86. int x;
  87. int y;
  88. int width;
  89. int height;
  90. } DVBSubDisplayDefinition;
  91. typedef struct DVBSubContext {
  92. AVClass *class;
  93. int composition_id;
  94. int ancillary_id;
  95. int version;
  96. int time_out;
  97. int compute_edt; /**< if 1 end display time calculated using pts
  98. if 0 (Default) calculated using time out */
  99. int compute_clut;
  100. int clut_count2[257][256];
  101. int substream;
  102. int64_t prev_start;
  103. DVBSubRegion *region_list;
  104. DVBSubCLUT *clut_list;
  105. DVBSubObject *object_list;
  106. DVBSubRegionDisplay *display_list;
  107. DVBSubDisplayDefinition *display_definition;
  108. } DVBSubContext;
  109. static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
  110. {
  111. DVBSubObject *ptr = ctx->object_list;
  112. while (ptr && ptr->id != object_id) {
  113. ptr = ptr->next;
  114. }
  115. return ptr;
  116. }
  117. static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
  118. {
  119. DVBSubCLUT *ptr = ctx->clut_list;
  120. while (ptr && ptr->id != clut_id) {
  121. ptr = ptr->next;
  122. }
  123. return ptr;
  124. }
  125. static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
  126. {
  127. DVBSubRegion *ptr = ctx->region_list;
  128. while (ptr && ptr->id != region_id) {
  129. ptr = ptr->next;
  130. }
  131. return ptr;
  132. }
  133. static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
  134. {
  135. DVBSubObject *object, *obj2, **obj2_ptr;
  136. DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
  137. while (region->display_list) {
  138. display = region->display_list;
  139. object = get_object(ctx, display->object_id);
  140. if (object) {
  141. obj_disp_ptr = &object->display_list;
  142. obj_disp = *obj_disp_ptr;
  143. while (obj_disp && obj_disp != display) {
  144. obj_disp_ptr = &obj_disp->object_list_next;
  145. obj_disp = *obj_disp_ptr;
  146. }
  147. if (obj_disp) {
  148. *obj_disp_ptr = obj_disp->object_list_next;
  149. if (!object->display_list) {
  150. obj2_ptr = &ctx->object_list;
  151. obj2 = *obj2_ptr;
  152. while (obj2 != object) {
  153. av_assert0(obj2);
  154. obj2_ptr = &obj2->next;
  155. obj2 = *obj2_ptr;
  156. }
  157. *obj2_ptr = obj2->next;
  158. av_freep(&obj2);
  159. }
  160. }
  161. }
  162. region->display_list = display->region_list_next;
  163. av_freep(&display);
  164. }
  165. }
  166. static void delete_cluts(DVBSubContext *ctx)
  167. {
  168. while (ctx->clut_list) {
  169. DVBSubCLUT *clut = ctx->clut_list;
  170. ctx->clut_list = clut->next;
  171. av_freep(&clut);
  172. }
  173. }
  174. static void delete_objects(DVBSubContext *ctx)
  175. {
  176. while (ctx->object_list) {
  177. DVBSubObject *object = ctx->object_list;
  178. ctx->object_list = object->next;
  179. av_freep(&object);
  180. }
  181. }
  182. static void delete_regions(DVBSubContext *ctx)
  183. {
  184. while (ctx->region_list) {
  185. DVBSubRegion *region = ctx->region_list;
  186. ctx->region_list = region->next;
  187. delete_region_display_list(ctx, region);
  188. av_freep(&region->pbuf);
  189. av_freep(&region);
  190. }
  191. }
  192. static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
  193. {
  194. int i, r, g, b, a = 0;
  195. DVBSubContext *ctx = avctx->priv_data;
  196. if (ctx->substream < 0) {
  197. ctx->composition_id = -1;
  198. ctx->ancillary_id = -1;
  199. } else if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
  200. av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
  201. ctx->composition_id = -1;
  202. ctx->ancillary_id = -1;
  203. } else {
  204. if (avctx->extradata_size > 5*ctx->substream + 2) {
  205. ctx->composition_id = AV_RB16(avctx->extradata + 5*ctx->substream);
  206. ctx->ancillary_id = AV_RB16(avctx->extradata + 5*ctx->substream + 2);
  207. } else {
  208. av_log(avctx, AV_LOG_WARNING, "Selected DVB subtitles sub-stream %d is not available\n", ctx->substream);
  209. ctx->composition_id = AV_RB16(avctx->extradata);
  210. ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
  211. }
  212. }
  213. ctx->version = -1;
  214. ctx->prev_start = AV_NOPTS_VALUE;
  215. default_clut.id = -1;
  216. default_clut.next = NULL;
  217. default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
  218. default_clut.clut4[1] = RGBA(255, 255, 255, 255);
  219. default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
  220. default_clut.clut4[3] = RGBA(127, 127, 127, 255);
  221. default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
  222. for (i = 1; i < 16; i++) {
  223. if (i < 8) {
  224. r = (i & 1) ? 255 : 0;
  225. g = (i & 2) ? 255 : 0;
  226. b = (i & 4) ? 255 : 0;
  227. } else {
  228. r = (i & 1) ? 127 : 0;
  229. g = (i & 2) ? 127 : 0;
  230. b = (i & 4) ? 127 : 0;
  231. }
  232. default_clut.clut16[i] = RGBA(r, g, b, 255);
  233. }
  234. default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
  235. for (i = 1; i < 256; i++) {
  236. if (i < 8) {
  237. r = (i & 1) ? 255 : 0;
  238. g = (i & 2) ? 255 : 0;
  239. b = (i & 4) ? 255 : 0;
  240. a = 63;
  241. } else {
  242. switch (i & 0x88) {
  243. case 0x00:
  244. r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
  245. g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
  246. b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
  247. a = 255;
  248. break;
  249. case 0x08:
  250. r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
  251. g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
  252. b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
  253. a = 127;
  254. break;
  255. case 0x80:
  256. r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
  257. g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
  258. b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
  259. a = 255;
  260. break;
  261. case 0x88:
  262. r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
  263. g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
  264. b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
  265. a = 255;
  266. break;
  267. }
  268. }
  269. default_clut.clut256[i] = RGBA(r, g, b, a);
  270. }
  271. return 0;
  272. }
  273. static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
  274. {
  275. DVBSubContext *ctx = avctx->priv_data;
  276. DVBSubRegionDisplay *display;
  277. delete_regions(ctx);
  278. delete_objects(ctx);
  279. delete_cluts(ctx);
  280. av_freep(&ctx->display_definition);
  281. while (ctx->display_list) {
  282. display = ctx->display_list;
  283. ctx->display_list = display->next;
  284. av_freep(&display);
  285. }
  286. return 0;
  287. }
  288. static int dvbsub_read_2bit_string(AVCodecContext *avctx,
  289. uint8_t *destbuf, int dbuf_len,
  290. const uint8_t **srcbuf, int buf_size,
  291. int non_mod, uint8_t *map_table, int x_pos)
  292. {
  293. GetBitContext gb;
  294. int bits;
  295. int run_length;
  296. int pixels_read = x_pos;
  297. init_get_bits(&gb, *srcbuf, buf_size << 3);
  298. destbuf += x_pos;
  299. while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
  300. bits = get_bits(&gb, 2);
  301. if (bits) {
  302. if (non_mod != 1 || bits != 1) {
  303. if (map_table)
  304. *destbuf++ = map_table[bits];
  305. else
  306. *destbuf++ = bits;
  307. }
  308. pixels_read++;
  309. } else {
  310. bits = get_bits1(&gb);
  311. if (bits == 1) {
  312. run_length = get_bits(&gb, 3) + 3;
  313. bits = get_bits(&gb, 2);
  314. if (non_mod == 1 && bits == 1)
  315. pixels_read += run_length;
  316. else {
  317. if (map_table)
  318. bits = map_table[bits];
  319. while (run_length-- > 0 && pixels_read < dbuf_len) {
  320. *destbuf++ = bits;
  321. pixels_read++;
  322. }
  323. }
  324. } else {
  325. bits = get_bits1(&gb);
  326. if (bits == 0) {
  327. bits = get_bits(&gb, 2);
  328. if (bits == 2) {
  329. run_length = get_bits(&gb, 4) + 12;
  330. bits = get_bits(&gb, 2);
  331. if (non_mod == 1 && bits == 1)
  332. pixels_read += run_length;
  333. else {
  334. if (map_table)
  335. bits = map_table[bits];
  336. while (run_length-- > 0 && pixels_read < dbuf_len) {
  337. *destbuf++ = bits;
  338. pixels_read++;
  339. }
  340. }
  341. } else if (bits == 3) {
  342. run_length = get_bits(&gb, 8) + 29;
  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 == 1) {
  355. if (map_table)
  356. bits = map_table[0];
  357. else
  358. bits = 0;
  359. run_length = 2;
  360. while (run_length-- > 0 && pixels_read < dbuf_len) {
  361. *destbuf++ = bits;
  362. pixels_read++;
  363. }
  364. } else {
  365. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  366. return pixels_read;
  367. }
  368. } else {
  369. if (map_table)
  370. bits = map_table[0];
  371. else
  372. bits = 0;
  373. *destbuf++ = bits;
  374. pixels_read++;
  375. }
  376. }
  377. }
  378. }
  379. if (get_bits(&gb, 6))
  380. av_log(avctx, AV_LOG_ERROR, "line overflow\n");
  381. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  382. return pixels_read;
  383. }
  384. static int dvbsub_read_4bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len,
  385. const uint8_t **srcbuf, int buf_size,
  386. int non_mod, uint8_t *map_table, int x_pos)
  387. {
  388. GetBitContext gb;
  389. int bits;
  390. int run_length;
  391. int pixels_read = x_pos;
  392. init_get_bits(&gb, *srcbuf, buf_size << 3);
  393. destbuf += x_pos;
  394. while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
  395. bits = get_bits(&gb, 4);
  396. if (bits) {
  397. if (non_mod != 1 || bits != 1) {
  398. if (map_table)
  399. *destbuf++ = map_table[bits];
  400. else
  401. *destbuf++ = bits;
  402. }
  403. pixels_read++;
  404. } else {
  405. bits = get_bits1(&gb);
  406. if (bits == 0) {
  407. run_length = get_bits(&gb, 3);
  408. if (run_length == 0) {
  409. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  410. return pixels_read;
  411. }
  412. run_length += 2;
  413. if (map_table)
  414. bits = map_table[0];
  415. else
  416. bits = 0;
  417. while (run_length-- > 0 && pixels_read < dbuf_len) {
  418. *destbuf++ = bits;
  419. pixels_read++;
  420. }
  421. } else {
  422. bits = get_bits1(&gb);
  423. if (bits == 0) {
  424. run_length = get_bits(&gb, 2) + 4;
  425. bits = get_bits(&gb, 4);
  426. if (non_mod == 1 && bits == 1)
  427. pixels_read += run_length;
  428. else {
  429. if (map_table)
  430. bits = map_table[bits];
  431. while (run_length-- > 0 && pixels_read < dbuf_len) {
  432. *destbuf++ = bits;
  433. pixels_read++;
  434. }
  435. }
  436. } else {
  437. bits = get_bits(&gb, 2);
  438. if (bits == 2) {
  439. run_length = get_bits(&gb, 4) + 9;
  440. bits = get_bits(&gb, 4);
  441. if (non_mod == 1 && bits == 1)
  442. pixels_read += run_length;
  443. else {
  444. if (map_table)
  445. bits = map_table[bits];
  446. while (run_length-- > 0 && pixels_read < dbuf_len) {
  447. *destbuf++ = bits;
  448. pixels_read++;
  449. }
  450. }
  451. } else if (bits == 3) {
  452. run_length = get_bits(&gb, 8) + 25;
  453. bits = get_bits(&gb, 4);
  454. if (non_mod == 1 && bits == 1)
  455. pixels_read += run_length;
  456. else {
  457. if (map_table)
  458. bits = map_table[bits];
  459. while (run_length-- > 0 && pixels_read < dbuf_len) {
  460. *destbuf++ = bits;
  461. pixels_read++;
  462. }
  463. }
  464. } else if (bits == 1) {
  465. if (map_table)
  466. bits = map_table[0];
  467. else
  468. bits = 0;
  469. run_length = 2;
  470. while (run_length-- > 0 && pixels_read < dbuf_len) {
  471. *destbuf++ = bits;
  472. pixels_read++;
  473. }
  474. } else {
  475. if (map_table)
  476. bits = map_table[0];
  477. else
  478. bits = 0;
  479. *destbuf++ = bits;
  480. pixels_read ++;
  481. }
  482. }
  483. }
  484. }
  485. }
  486. if (get_bits(&gb, 8))
  487. av_log(avctx, AV_LOG_ERROR, "line overflow\n");
  488. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  489. return pixels_read;
  490. }
  491. static int dvbsub_read_8bit_string(AVCodecContext *avctx,
  492. uint8_t *destbuf, int dbuf_len,
  493. const uint8_t **srcbuf, int buf_size,
  494. int non_mod, uint8_t *map_table, int x_pos)
  495. {
  496. const uint8_t *sbuf_end = (*srcbuf) + buf_size;
  497. int bits;
  498. int run_length;
  499. int pixels_read = x_pos;
  500. destbuf += x_pos;
  501. while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
  502. bits = *(*srcbuf)++;
  503. if (bits) {
  504. if (non_mod != 1 || bits != 1) {
  505. if (map_table)
  506. *destbuf++ = map_table[bits];
  507. else
  508. *destbuf++ = bits;
  509. }
  510. pixels_read++;
  511. } else {
  512. bits = *(*srcbuf)++;
  513. run_length = bits & 0x7f;
  514. if ((bits & 0x80) == 0) {
  515. if (run_length == 0) {
  516. return pixels_read;
  517. }
  518. bits = 0;
  519. } else {
  520. bits = *(*srcbuf)++;
  521. }
  522. if (non_mod == 1 && bits == 1)
  523. pixels_read += run_length;
  524. else {
  525. if (map_table)
  526. bits = map_table[bits];
  527. while (run_length-- > 0 && pixels_read < dbuf_len) {
  528. *destbuf++ = bits;
  529. pixels_read++;
  530. }
  531. }
  532. }
  533. }
  534. if (*(*srcbuf)++)
  535. av_log(avctx, AV_LOG_ERROR, "line overflow\n");
  536. return pixels_read;
  537. }
  538. static void compute_default_clut(DVBSubContext *ctx, uint8_t *clut, AVSubtitleRect *rect, int w, int h)
  539. {
  540. uint8_t list[256] = {0};
  541. uint8_t list_inv[256];
  542. int counttab[256] = {0};
  543. int (*counttab2)[256] = ctx->clut_count2;
  544. int count, i, x, y;
  545. ptrdiff_t stride = rect->linesize[0];
  546. memset(ctx->clut_count2, 0 , sizeof(ctx->clut_count2));
  547. #define V(x,y) rect->data[0][(x) + (y)*stride]
  548. for (y = 0; y<h; y++) {
  549. for (x = 0; x<w; x++) {
  550. int v = V(x,y) + 1;
  551. int vl = x ? V(x-1,y) + 1 : 0;
  552. int vr = x+1<w ? V(x+1,y) + 1 : 0;
  553. int vt = y ? V(x,y-1) + 1 : 0;
  554. int vb = y+1<h ? V(x,y+1) + 1 : 0;
  555. counttab[v-1] += !!((v!=vl) + (v!=vr) + (v!=vt) + (v!=vb));
  556. counttab2[vl][v-1] ++;
  557. counttab2[vr][v-1] ++;
  558. counttab2[vt][v-1] ++;
  559. counttab2[vb][v-1] ++;
  560. }
  561. }
  562. #define L(x,y) list[d[(x) + (y)*stride]]
  563. for (i = 0; i<256; i++) {
  564. counttab2[i+1][i] = 0;
  565. }
  566. for (i = 0; i<256; i++) {
  567. int bestscore = 0;
  568. int bestv = 0;
  569. for (x = 0; x < 256; x++) {
  570. int scorev = 0;
  571. if (list[x])
  572. continue;
  573. scorev += counttab2[0][x];
  574. for (y = 0; y < 256; y++) {
  575. scorev += list[y] * counttab2[y+1][x];
  576. }
  577. if (scorev) {
  578. int score = 1024LL*scorev / counttab[x];
  579. if (score > bestscore) {
  580. bestscore = score;
  581. bestv = x;
  582. }
  583. }
  584. }
  585. if (!bestscore)
  586. break;
  587. list [ bestv ] = 1;
  588. list_inv[ i ] = bestv;
  589. }
  590. count = FFMAX(i - 1, 1);
  591. for (i--; i>=0; i--) {
  592. int v = i*255/count;
  593. AV_WN32(clut + 4*list_inv[i], RGBA(v/2,v,v/2,v));
  594. }
  595. }
  596. static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
  597. {
  598. DVBSubContext *ctx = avctx->priv_data;
  599. DVBSubRegionDisplay *display;
  600. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  601. DVBSubRegion *region;
  602. AVSubtitleRect *rect;
  603. DVBSubCLUT *clut;
  604. uint32_t *clut_table;
  605. int i;
  606. int offset_x=0, offset_y=0;
  607. int ret = 0;
  608. if (display_def) {
  609. offset_x = display_def->x;
  610. offset_y = display_def->y;
  611. }
  612. /* Not touching AVSubtitles again*/
  613. if(sub->num_rects) {
  614. avpriv_request_sample(ctx, "Different Version of Segment asked Twice");
  615. return AVERROR_PATCHWELCOME;
  616. }
  617. for (display = ctx->display_list; display; display = display->next) {
  618. region = get_region(ctx, display->region_id);
  619. if (region && region->dirty)
  620. sub->num_rects++;
  621. }
  622. if(ctx->compute_edt == 0) {
  623. sub->end_display_time = ctx->time_out * 1000;
  624. *got_output = 1;
  625. } else if (ctx->prev_start != AV_NOPTS_VALUE) {
  626. sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
  627. *got_output = 1;
  628. }
  629. if (sub->num_rects > 0) {
  630. sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
  631. if (!sub->rects) {
  632. ret = AVERROR(ENOMEM);
  633. goto fail;
  634. }
  635. for (i = 0; i < sub->num_rects; i++) {
  636. sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
  637. if (!sub->rects[i]) {
  638. ret = AVERROR(ENOMEM);
  639. goto fail;
  640. }
  641. }
  642. i = 0;
  643. for (display = ctx->display_list; display; display = display->next) {
  644. region = get_region(ctx, display->region_id);
  645. if (!region)
  646. continue;
  647. if (!region->dirty)
  648. continue;
  649. rect = sub->rects[i];
  650. rect->x = display->x_pos + offset_x;
  651. rect->y = display->y_pos + offset_y;
  652. rect->w = region->width;
  653. rect->h = region->height;
  654. rect->nb_colors = (1 << region->depth);
  655. rect->type = SUBTITLE_BITMAP;
  656. rect->linesize[0] = region->width;
  657. clut = get_clut(ctx, region->clut);
  658. if (!clut)
  659. clut = &default_clut;
  660. switch (region->depth) {
  661. case 2:
  662. clut_table = clut->clut4;
  663. break;
  664. case 8:
  665. clut_table = clut->clut256;
  666. break;
  667. case 4:
  668. default:
  669. clut_table = clut->clut16;
  670. break;
  671. }
  672. rect->data[1] = av_mallocz(AVPALETTE_SIZE);
  673. if (!rect->data[1]) {
  674. ret = AVERROR(ENOMEM);
  675. goto fail;
  676. }
  677. memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
  678. rect->data[0] = av_malloc(region->buf_size);
  679. if (!rect->data[0]) {
  680. ret = AVERROR(ENOMEM);
  681. goto fail;
  682. }
  683. memcpy(rect->data[0], region->pbuf, region->buf_size);
  684. if ((clut == &default_clut && ctx->compute_clut == -1) || ctx->compute_clut == 1) {
  685. if (!region->has_computed_clut) {
  686. compute_default_clut(ctx, region->computed_clut, rect, rect->w, rect->h);
  687. region->has_computed_clut = 1;
  688. }
  689. memcpy(rect->data[1], region->computed_clut, sizeof(region->computed_clut));
  690. }
  691. #if FF_API_AVPICTURE
  692. FF_DISABLE_DEPRECATION_WARNINGS
  693. {
  694. int j;
  695. for (j = 0; j < 4; j++) {
  696. rect->pict.data[j] = rect->data[j];
  697. rect->pict.linesize[j] = rect->linesize[j];
  698. }
  699. }
  700. FF_ENABLE_DEPRECATION_WARNINGS
  701. #endif
  702. i++;
  703. }
  704. }
  705. return 0;
  706. fail:
  707. if (sub->rects) {
  708. for(i=0; i<sub->num_rects; i++) {
  709. rect = sub->rects[i];
  710. if (rect) {
  711. av_freep(&rect->data[0]);
  712. av_freep(&rect->data[1]);
  713. }
  714. av_freep(&sub->rects[i]);
  715. }
  716. av_freep(&sub->rects);
  717. }
  718. sub->num_rects = 0;
  719. return ret;
  720. }
  721. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  722. const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  723. {
  724. DVBSubContext *ctx = avctx->priv_data;
  725. DVBSubRegion *region = get_region(ctx, display->region_id);
  726. const uint8_t *buf_end = buf + buf_size;
  727. uint8_t *pbuf;
  728. int x_pos, y_pos;
  729. int i;
  730. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  731. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  732. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  733. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  734. uint8_t *map_table;
  735. #if 0
  736. ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
  737. top_bottom ? "bottom" : "top");
  738. for (i = 0; i < buf_size; i++) {
  739. if (i % 16 == 0)
  740. ff_dlog(avctx, "0x%8p: ", buf+i);
  741. ff_dlog(avctx, "%02x ", buf[i]);
  742. if (i % 16 == 15)
  743. ff_dlog(avctx, "\n");
  744. }
  745. if (i % 16)
  746. ff_dlog(avctx, "\n");
  747. #endif
  748. if (!region)
  749. return;
  750. pbuf = region->pbuf;
  751. region->dirty = 1;
  752. x_pos = display->x_pos;
  753. y_pos = display->y_pos;
  754. y_pos += top_bottom;
  755. while (buf < buf_end) {
  756. if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
  757. av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
  758. return;
  759. }
  760. switch (*buf++) {
  761. case 0x10:
  762. if (region->depth == 8)
  763. map_table = map2to8;
  764. else if (region->depth == 4)
  765. map_table = map2to4;
  766. else
  767. map_table = NULL;
  768. x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
  769. region->width, &buf, buf_end - buf,
  770. non_mod, map_table, x_pos);
  771. break;
  772. case 0x11:
  773. if (region->depth < 4) {
  774. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  775. return;
  776. }
  777. if (region->depth == 8)
  778. map_table = map4to8;
  779. else
  780. map_table = NULL;
  781. x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
  782. region->width, &buf, buf_end - buf,
  783. non_mod, map_table, x_pos);
  784. break;
  785. case 0x12:
  786. if (region->depth < 8) {
  787. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  788. return;
  789. }
  790. x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
  791. region->width, &buf, buf_end - buf,
  792. non_mod, NULL, x_pos);
  793. break;
  794. case 0x20:
  795. map2to4[0] = (*buf) >> 4;
  796. map2to4[1] = (*buf++) & 0xf;
  797. map2to4[2] = (*buf) >> 4;
  798. map2to4[3] = (*buf++) & 0xf;
  799. break;
  800. case 0x21:
  801. for (i = 0; i < 4; i++)
  802. map2to8[i] = *buf++;
  803. break;
  804. case 0x22:
  805. for (i = 0; i < 16; i++)
  806. map4to8[i] = *buf++;
  807. break;
  808. case 0xf0:
  809. x_pos = display->x_pos;
  810. y_pos += 2;
  811. break;
  812. default:
  813. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  814. }
  815. }
  816. region->has_computed_clut = 0;
  817. }
  818. static int dvbsub_parse_object_segment(AVCodecContext *avctx,
  819. const uint8_t *buf, int buf_size)
  820. {
  821. DVBSubContext *ctx = avctx->priv_data;
  822. const uint8_t *buf_end = buf + buf_size;
  823. int object_id;
  824. DVBSubObject *object;
  825. DVBSubObjectDisplay *display;
  826. int top_field_len, bottom_field_len;
  827. int coding_method, non_modifying_color;
  828. object_id = AV_RB16(buf);
  829. buf += 2;
  830. object = get_object(ctx, object_id);
  831. if (!object)
  832. return AVERROR_INVALIDDATA;
  833. coding_method = ((*buf) >> 2) & 3;
  834. non_modifying_color = ((*buf++) >> 1) & 1;
  835. if (coding_method == 0) {
  836. top_field_len = AV_RB16(buf);
  837. buf += 2;
  838. bottom_field_len = AV_RB16(buf);
  839. buf += 2;
  840. if (buf + top_field_len + bottom_field_len > buf_end) {
  841. av_log(avctx, AV_LOG_ERROR, "Field data size %d+%d too large\n", top_field_len, bottom_field_len);
  842. return AVERROR_INVALIDDATA;
  843. }
  844. for (display = object->display_list; display; display = display->object_list_next) {
  845. const uint8_t *block = buf;
  846. int bfl = bottom_field_len;
  847. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  848. non_modifying_color);
  849. if (bottom_field_len > 0)
  850. block = buf + top_field_len;
  851. else
  852. bfl = top_field_len;
  853. dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
  854. non_modifying_color);
  855. }
  856. /* } else if (coding_method == 1) {*/
  857. } else {
  858. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  859. }
  860. return 0;
  861. }
  862. static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
  863. const uint8_t *buf, int buf_size)
  864. {
  865. DVBSubContext *ctx = avctx->priv_data;
  866. const uint8_t *buf_end = buf + buf_size;
  867. int i, clut_id;
  868. int version;
  869. DVBSubCLUT *clut;
  870. int entry_id, depth , full_range;
  871. int y, cr, cb, alpha;
  872. int r, g, b, r_add, g_add, b_add;
  873. ff_dlog(avctx, "DVB clut packet:\n");
  874. for (i=0; i < buf_size; i++) {
  875. ff_dlog(avctx, "%02x ", buf[i]);
  876. if (i % 16 == 15)
  877. ff_dlog(avctx, "\n");
  878. }
  879. if (i % 16)
  880. ff_dlog(avctx, "\n");
  881. clut_id = *buf++;
  882. version = ((*buf)>>4)&15;
  883. buf += 1;
  884. clut = get_clut(ctx, clut_id);
  885. if (!clut) {
  886. clut = av_malloc(sizeof(DVBSubCLUT));
  887. if (!clut)
  888. return AVERROR(ENOMEM);
  889. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  890. clut->id = clut_id;
  891. clut->version = -1;
  892. clut->next = ctx->clut_list;
  893. ctx->clut_list = clut;
  894. }
  895. if (clut->version != version) {
  896. clut->version = version;
  897. while (buf + 4 < buf_end) {
  898. entry_id = *buf++;
  899. depth = (*buf) & 0xe0;
  900. if (depth == 0) {
  901. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  902. }
  903. full_range = (*buf++) & 1;
  904. if (full_range) {
  905. y = *buf++;
  906. cr = *buf++;
  907. cb = *buf++;
  908. alpha = *buf++;
  909. } else {
  910. y = buf[0] & 0xfc;
  911. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  912. cb = (buf[1] << 2) & 0xf0;
  913. alpha = (buf[1] << 6) & 0xc0;
  914. buf += 2;
  915. }
  916. if (y == 0)
  917. alpha = 0xff;
  918. YUV_TO_RGB1_CCIR(cb, cr);
  919. YUV_TO_RGB2_CCIR(r, g, b, y);
  920. ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  921. if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
  922. ff_dlog(avctx, "More than one bit level marked: %x\n", depth);
  923. if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
  924. return AVERROR_INVALIDDATA;
  925. }
  926. if (depth & 0x80 && entry_id < 4)
  927. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  928. else if (depth & 0x40 && entry_id < 16)
  929. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  930. else if (depth & 0x20)
  931. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  932. }
  933. }
  934. return 0;
  935. }
  936. static int dvbsub_parse_region_segment(AVCodecContext *avctx,
  937. const uint8_t *buf, int buf_size)
  938. {
  939. DVBSubContext *ctx = avctx->priv_data;
  940. const uint8_t *buf_end = buf + buf_size;
  941. int region_id, object_id;
  942. int av_unused version;
  943. DVBSubRegion *region;
  944. DVBSubObject *object;
  945. DVBSubObjectDisplay *display;
  946. int fill;
  947. int ret;
  948. if (buf_size < 10)
  949. return AVERROR_INVALIDDATA;
  950. region_id = *buf++;
  951. region = get_region(ctx, region_id);
  952. if (!region) {
  953. region = av_mallocz(sizeof(DVBSubRegion));
  954. if (!region)
  955. return AVERROR(ENOMEM);
  956. region->id = region_id;
  957. region->version = -1;
  958. region->next = ctx->region_list;
  959. ctx->region_list = region;
  960. }
  961. version = ((*buf)>>4) & 15;
  962. fill = ((*buf++) >> 3) & 1;
  963. region->width = AV_RB16(buf);
  964. buf += 2;
  965. region->height = AV_RB16(buf);
  966. buf += 2;
  967. ret = av_image_check_size2(region->width, region->height, avctx->max_pixels, AV_PIX_FMT_PAL8, 0, avctx);
  968. if (ret >= 0 && region->width * region->height * 2 > 320 * 1024 * 8) {
  969. ret = AVERROR_INVALIDDATA;
  970. av_log(avctx, AV_LOG_ERROR, "Pixel buffer memory constraint violated\n");
  971. }
  972. if (ret < 0) {
  973. region->width= region->height= 0;
  974. return ret;
  975. }
  976. if (region->width * region->height != region->buf_size) {
  977. av_free(region->pbuf);
  978. region->buf_size = region->width * region->height;
  979. region->pbuf = av_malloc(region->buf_size);
  980. if (!region->pbuf) {
  981. region->buf_size =
  982. region->width =
  983. region->height = 0;
  984. return AVERROR(ENOMEM);
  985. }
  986. fill = 1;
  987. region->dirty = 0;
  988. }
  989. region->depth = 1 << (((*buf++) >> 2) & 7);
  990. if(region->depth<2 || region->depth>8){
  991. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  992. region->depth= 4;
  993. }
  994. region->clut = *buf++;
  995. if (region->depth == 8) {
  996. region->bgcolor = *buf++;
  997. buf += 1;
  998. } else {
  999. buf += 1;
  1000. if (region->depth == 4)
  1001. region->bgcolor = (((*buf++) >> 4) & 15);
  1002. else
  1003. region->bgcolor = (((*buf++) >> 2) & 3);
  1004. }
  1005. ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  1006. if (fill) {
  1007. memset(region->pbuf, region->bgcolor, region->buf_size);
  1008. ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
  1009. }
  1010. delete_region_display_list(ctx, region);
  1011. while (buf + 5 < buf_end) {
  1012. object_id = AV_RB16(buf);
  1013. buf += 2;
  1014. object = get_object(ctx, object_id);
  1015. if (!object) {
  1016. object = av_mallocz(sizeof(DVBSubObject));
  1017. if (!object)
  1018. return AVERROR(ENOMEM);
  1019. object->id = object_id;
  1020. object->next = ctx->object_list;
  1021. ctx->object_list = object;
  1022. }
  1023. object->type = (*buf) >> 6;
  1024. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  1025. if (!display)
  1026. return AVERROR(ENOMEM);
  1027. display->object_id = object_id;
  1028. display->region_id = region_id;
  1029. display->x_pos = AV_RB16(buf) & 0xfff;
  1030. buf += 2;
  1031. display->y_pos = AV_RB16(buf) & 0xfff;
  1032. buf += 2;
  1033. if (display->x_pos >= region->width ||
  1034. display->y_pos >= region->height) {
  1035. av_log(avctx, AV_LOG_ERROR, "Object outside region\n");
  1036. av_free(display);
  1037. return AVERROR_INVALIDDATA;
  1038. }
  1039. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  1040. display->fgcolor = *buf++;
  1041. display->bgcolor = *buf++;
  1042. }
  1043. display->region_list_next = region->display_list;
  1044. region->display_list = display;
  1045. display->object_list_next = object->display_list;
  1046. object->display_list = display;
  1047. }
  1048. return 0;
  1049. }
  1050. static int dvbsub_parse_page_segment(AVCodecContext *avctx,
  1051. const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
  1052. {
  1053. DVBSubContext *ctx = avctx->priv_data;
  1054. DVBSubRegionDisplay *display;
  1055. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  1056. const uint8_t *buf_end = buf + buf_size;
  1057. int region_id;
  1058. int page_state;
  1059. int timeout;
  1060. int version;
  1061. if (buf_size < 1)
  1062. return AVERROR_INVALIDDATA;
  1063. timeout = *buf++;
  1064. version = ((*buf)>>4) & 15;
  1065. page_state = ((*buf++) >> 2) & 3;
  1066. if (ctx->version == version) {
  1067. return 0;
  1068. }
  1069. ctx->time_out = timeout;
  1070. ctx->version = version;
  1071. ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  1072. if(ctx->compute_edt == 1)
  1073. save_subtitle_set(avctx, sub, got_output);
  1074. if (page_state == 1 || page_state == 2) {
  1075. delete_regions(ctx);
  1076. delete_objects(ctx);
  1077. delete_cluts(ctx);
  1078. }
  1079. tmp_display_list = ctx->display_list;
  1080. ctx->display_list = NULL;
  1081. while (buf + 5 < buf_end) {
  1082. region_id = *buf++;
  1083. buf += 1;
  1084. display = ctx->display_list;
  1085. while (display && display->region_id != region_id) {
  1086. display = display->next;
  1087. }
  1088. if (display) {
  1089. av_log(avctx, AV_LOG_ERROR, "duplicate region\n");
  1090. break;
  1091. }
  1092. display = tmp_display_list;
  1093. tmp_ptr = &tmp_display_list;
  1094. while (display && display->region_id != region_id) {
  1095. tmp_ptr = &display->next;
  1096. display = display->next;
  1097. }
  1098. if (!display) {
  1099. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  1100. if (!display)
  1101. return AVERROR(ENOMEM);
  1102. }
  1103. display->region_id = region_id;
  1104. display->x_pos = AV_RB16(buf);
  1105. buf += 2;
  1106. display->y_pos = AV_RB16(buf);
  1107. buf += 2;
  1108. *tmp_ptr = display->next;
  1109. display->next = ctx->display_list;
  1110. ctx->display_list = display;
  1111. ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  1112. }
  1113. while (tmp_display_list) {
  1114. display = tmp_display_list;
  1115. tmp_display_list = display->next;
  1116. av_freep(&display);
  1117. }
  1118. return 0;
  1119. }
  1120. #ifdef DEBUG
  1121. static void png_save(DVBSubContext *ctx, const char *filename, uint32_t *bitmap, int w, int h)
  1122. {
  1123. int x, y, v;
  1124. FILE *f;
  1125. char fname[40], fname2[40];
  1126. char command[1024];
  1127. snprintf(fname, sizeof(fname), "%s.ppm", filename);
  1128. f = fopen(fname, "w");
  1129. if (!f) {
  1130. perror(fname);
  1131. return;
  1132. }
  1133. fprintf(f, "P6\n"
  1134. "%d %d\n"
  1135. "%d\n",
  1136. w, h, 255);
  1137. for(y = 0; y < h; y++) {
  1138. for(x = 0; x < w; x++) {
  1139. v = bitmap[y * w + x];
  1140. putc((v >> 16) & 0xff, f);
  1141. putc((v >> 8) & 0xff, f);
  1142. putc((v >> 0) & 0xff, f);
  1143. }
  1144. }
  1145. fclose(f);
  1146. snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
  1147. f = fopen(fname2, "w");
  1148. if (!f) {
  1149. perror(fname2);
  1150. return;
  1151. }
  1152. fprintf(f, "P5\n"
  1153. "%d %d\n"
  1154. "%d\n",
  1155. w, h, 255);
  1156. for(y = 0; y < h; y++) {
  1157. for(x = 0; x < w; x++) {
  1158. v = bitmap[y * w + x];
  1159. putc((v >> 24) & 0xff, f);
  1160. }
  1161. }
  1162. fclose(f);
  1163. snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
  1164. if (system(command) != 0) {
  1165. av_log(ctx, AV_LOG_ERROR, "Error running pnmtopng\n");
  1166. return;
  1167. }
  1168. snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
  1169. if (system(command) != 0) {
  1170. av_log(ctx, AV_LOG_ERROR, "Error removing %s and %s\n", fname, fname2);
  1171. return;
  1172. }
  1173. }
  1174. static int save_display_set(DVBSubContext *ctx)
  1175. {
  1176. DVBSubRegion *region;
  1177. DVBSubRegionDisplay *display;
  1178. DVBSubCLUT *clut;
  1179. uint32_t *clut_table;
  1180. int x_pos, y_pos, width, height;
  1181. int x, y, y_off, x_off;
  1182. uint32_t *pbuf;
  1183. char filename[32];
  1184. static int fileno_index = 0;
  1185. x_pos = -1;
  1186. y_pos = -1;
  1187. width = 0;
  1188. height = 0;
  1189. for (display = ctx->display_list; display; display = display->next) {
  1190. region = get_region(ctx, display->region_id);
  1191. if (!region)
  1192. return -1;
  1193. if (x_pos == -1) {
  1194. x_pos = display->x_pos;
  1195. y_pos = display->y_pos;
  1196. width = region->width;
  1197. height = region->height;
  1198. } else {
  1199. if (display->x_pos < x_pos) {
  1200. width += (x_pos - display->x_pos);
  1201. x_pos = display->x_pos;
  1202. }
  1203. if (display->y_pos < y_pos) {
  1204. height += (y_pos - display->y_pos);
  1205. y_pos = display->y_pos;
  1206. }
  1207. if (display->x_pos + region->width > x_pos + width) {
  1208. width = display->x_pos + region->width - x_pos;
  1209. }
  1210. if (display->y_pos + region->height > y_pos + height) {
  1211. height = display->y_pos + region->height - y_pos;
  1212. }
  1213. }
  1214. }
  1215. if (x_pos >= 0) {
  1216. pbuf = av_malloc(width * height * 4);
  1217. if (!pbuf)
  1218. return -1;
  1219. for (display = ctx->display_list; display; display = display->next) {
  1220. region = get_region(ctx, display->region_id);
  1221. if (!region)
  1222. return -1;
  1223. x_off = display->x_pos - x_pos;
  1224. y_off = display->y_pos - y_pos;
  1225. clut = get_clut(ctx, region->clut);
  1226. if (!clut)
  1227. clut = &default_clut;
  1228. switch (region->depth) {
  1229. case 2:
  1230. clut_table = clut->clut4;
  1231. break;
  1232. case 8:
  1233. clut_table = clut->clut256;
  1234. break;
  1235. case 4:
  1236. default:
  1237. clut_table = clut->clut16;
  1238. break;
  1239. }
  1240. for (y = 0; y < region->height; y++) {
  1241. for (x = 0; x < region->width; x++) {
  1242. pbuf[((y + y_off) * width) + x_off + x] =
  1243. clut_table[region->pbuf[y * region->width + x]];
  1244. }
  1245. }
  1246. }
  1247. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  1248. png_save(ctx, filename, pbuf, width, height);
  1249. av_freep(&pbuf);
  1250. }
  1251. fileno_index++;
  1252. return 0;
  1253. }
  1254. #endif /* DEBUG */
  1255. static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
  1256. const uint8_t *buf,
  1257. int buf_size)
  1258. {
  1259. DVBSubContext *ctx = avctx->priv_data;
  1260. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1261. int dds_version, info_byte;
  1262. if (buf_size < 5)
  1263. return AVERROR_INVALIDDATA;
  1264. info_byte = bytestream_get_byte(&buf);
  1265. dds_version = info_byte >> 4;
  1266. if (display_def && display_def->version == dds_version)
  1267. return 0; // already have this display definition version
  1268. if (!display_def) {
  1269. display_def = av_mallocz(sizeof(*display_def));
  1270. if (!display_def)
  1271. return AVERROR(ENOMEM);
  1272. ctx->display_definition = display_def;
  1273. }
  1274. display_def->version = dds_version;
  1275. display_def->x = 0;
  1276. display_def->y = 0;
  1277. display_def->width = bytestream_get_be16(&buf) + 1;
  1278. display_def->height = bytestream_get_be16(&buf) + 1;
  1279. if (!avctx->width || !avctx->height) {
  1280. avctx->width = display_def->width;
  1281. avctx->height = display_def->height;
  1282. }
  1283. if (info_byte & 1<<3) { // display_window_flag
  1284. if (buf_size < 13)
  1285. return AVERROR_INVALIDDATA;
  1286. display_def->x = bytestream_get_be16(&buf);
  1287. display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
  1288. display_def->y = bytestream_get_be16(&buf);
  1289. display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
  1290. }
  1291. return 0;
  1292. }
  1293. static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1294. int buf_size, AVSubtitle *sub,int *got_output)
  1295. {
  1296. DVBSubContext *ctx = avctx->priv_data;
  1297. if(ctx->compute_edt == 0)
  1298. save_subtitle_set(avctx, sub, got_output);
  1299. #ifdef DEBUG
  1300. save_display_set(ctx);
  1301. #endif
  1302. return 0;
  1303. }
  1304. static int dvbsub_decode(AVCodecContext *avctx,
  1305. void *data, int *data_size,
  1306. AVPacket *avpkt)
  1307. {
  1308. const uint8_t *buf = avpkt->data;
  1309. int buf_size = avpkt->size;
  1310. DVBSubContext *ctx = avctx->priv_data;
  1311. AVSubtitle *sub = data;
  1312. const uint8_t *p, *p_end;
  1313. int segment_type;
  1314. int page_id;
  1315. int segment_length;
  1316. int i;
  1317. int ret = 0;
  1318. int got_segment = 0;
  1319. int got_dds = 0;
  1320. ff_dlog(avctx, "DVB sub packet:\n");
  1321. for (i=0; i < buf_size; i++) {
  1322. ff_dlog(avctx, "%02x ", buf[i]);
  1323. if (i % 16 == 15)
  1324. ff_dlog(avctx, "\n");
  1325. }
  1326. if (i % 16)
  1327. ff_dlog(avctx, "\n");
  1328. if (buf_size <= 6 || *buf != 0x0f) {
  1329. ff_dlog(avctx, "incomplete or broken packet");
  1330. return AVERROR_INVALIDDATA;
  1331. }
  1332. p = buf;
  1333. p_end = buf + buf_size;
  1334. while (p_end - p >= 6 && *p == 0x0f) {
  1335. p += 1;
  1336. segment_type = *p++;
  1337. page_id = AV_RB16(p);
  1338. p += 2;
  1339. segment_length = AV_RB16(p);
  1340. p += 2;
  1341. if (avctx->debug & FF_DEBUG_STARTCODE) {
  1342. av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
  1343. }
  1344. if (p_end - p < segment_length) {
  1345. ff_dlog(avctx, "incomplete or broken packet");
  1346. ret = -1;
  1347. goto end;
  1348. }
  1349. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
  1350. ctx->composition_id == -1 || ctx->ancillary_id == -1) {
  1351. int ret = 0;
  1352. switch (segment_type) {
  1353. case DVBSUB_PAGE_SEGMENT:
  1354. ret = dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
  1355. got_segment |= 1;
  1356. break;
  1357. case DVBSUB_REGION_SEGMENT:
  1358. ret = dvbsub_parse_region_segment(avctx, p, segment_length);
  1359. got_segment |= 2;
  1360. break;
  1361. case DVBSUB_CLUT_SEGMENT:
  1362. ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
  1363. if (ret < 0) goto end;
  1364. got_segment |= 4;
  1365. break;
  1366. case DVBSUB_OBJECT_SEGMENT:
  1367. ret = dvbsub_parse_object_segment(avctx, p, segment_length);
  1368. got_segment |= 8;
  1369. break;
  1370. case DVBSUB_DISPLAYDEFINITION_SEGMENT:
  1371. ret = dvbsub_parse_display_definition_segment(avctx, p,
  1372. segment_length);
  1373. got_dds = 1;
  1374. break;
  1375. case DVBSUB_DISPLAY_SEGMENT:
  1376. ret = dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
  1377. if (got_segment == 15 && !got_dds && !avctx->width && !avctx->height) {
  1378. // Default from ETSI EN 300 743 V1.3.1 (7.2.1)
  1379. avctx->width = 720;
  1380. avctx->height = 576;
  1381. }
  1382. got_segment |= 16;
  1383. break;
  1384. default:
  1385. ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1386. segment_type, page_id, segment_length);
  1387. break;
  1388. }
  1389. if (ret < 0)
  1390. goto end;
  1391. }
  1392. p += segment_length;
  1393. }
  1394. // Some streams do not send a display segment but if we have all the other
  1395. // segments then we need no further data.
  1396. if (got_segment == 15) {
  1397. av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
  1398. dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
  1399. }
  1400. end:
  1401. if(ret < 0) {
  1402. *data_size = 0;
  1403. avsubtitle_free(sub);
  1404. return ret;
  1405. } else {
  1406. if(ctx->compute_edt == 1 )
  1407. FFSWAP(int64_t, ctx->prev_start, sub->pts);
  1408. }
  1409. return p - buf;
  1410. }
  1411. #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
  1412. static const AVOption options[] = {
  1413. {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DS},
  1414. {"compute_clut", "compute clut when not available(-1) or always(1) or never(0)", offsetof(DVBSubContext, compute_clut), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, DS},
  1415. {"dvb_substream", "", offsetof(DVBSubContext, substream), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, DS},
  1416. {NULL}
  1417. };
  1418. static const AVClass dvbsubdec_class = {
  1419. .class_name = "DVB Sub Decoder",
  1420. .item_name = av_default_item_name,
  1421. .option = options,
  1422. .version = LIBAVUTIL_VERSION_INT,
  1423. };
  1424. AVCodec ff_dvbsub_decoder = {
  1425. .name = "dvbsub",
  1426. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1427. .type = AVMEDIA_TYPE_SUBTITLE,
  1428. .id = AV_CODEC_ID_DVB_SUBTITLE,
  1429. .priv_data_size = sizeof(DVBSubContext),
  1430. .init = dvbsub_init_decoder,
  1431. .close = dvbsub_close_decoder,
  1432. .decode = dvbsub_decode,
  1433. .priv_class = &dvbsubdec_class,
  1434. };