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.

1711 lines
48KB

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