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.

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