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.

1728 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 *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. ptrdiff_t stride = rect->linesize[0];
  542. #define V(x,y) rect->data[0][(x) + (y)*stride]
  543. for (y = 0; y<h; y++) {
  544. for (x = 0; x<w; x++) {
  545. int v = V(x,y) + 1;
  546. int vl = x ? V(x-1,y) + 1 : 0;
  547. int vr = x+1<w ? V(x+1,y) + 1 : 0;
  548. int vt = y ? V(x,y-1) + 1 : 0;
  549. int vb = y+1<h ? V(x,y+1) + 1 : 0;
  550. counttab[v-1] += !!((v!=vl) + (v!=vr) + (v!=vt) + (v!=vb));
  551. }
  552. }
  553. #define L(x,y) list[d[(x) + (y)*stride]]
  554. for (i = 0; i<256; i++) {
  555. int scoretab[256] = {0};
  556. int bestscore = 0;
  557. int bestv = 0;
  558. for (y = 0; y<h; y++) {
  559. for (x = 0; x<w; x++) {
  560. uint8_t *d = &rect->data[0][x + y*stride];
  561. int v = *d;
  562. int l_m = list[v];
  563. int l_l = x ? L(-1, 0) : 1;
  564. int l_r = x+1<w ? L( 1, 0) : 1;
  565. int l_t = y ? L( 0,-1) : 1;
  566. int l_b = y+1<h ? L( 0, 1) : 1;
  567. if (l_m)
  568. continue;
  569. scoretab[v] += l_l + l_r + l_t + l_b;
  570. }
  571. }
  572. for (x = 0; x < 256; x++) {
  573. if (scoretab[x]) {
  574. int score = 1024LL*scoretab[x] / counttab[x];
  575. if (score > bestscore) {
  576. bestscore = score;
  577. bestv = x;
  578. }
  579. }
  580. }
  581. if (!bestscore)
  582. break;
  583. list [ bestv ] = 1;
  584. list_inv[ i ] = bestv;
  585. }
  586. count = FFMAX(i - 1, 1);
  587. for (i--; i>=0; i--) {
  588. int v = i*255/count;
  589. AV_WN32(rect->data[1] + 4*list_inv[i], RGBA(v/2,v,v/2,v));
  590. }
  591. }
  592. static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
  593. {
  594. DVBSubContext *ctx = avctx->priv_data;
  595. DVBSubRegionDisplay *display;
  596. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  597. DVBSubRegion *region;
  598. AVSubtitleRect *rect;
  599. DVBSubCLUT *clut;
  600. uint32_t *clut_table;
  601. int i;
  602. int offset_x=0, offset_y=0;
  603. int ret = 0;
  604. if (display_def) {
  605. offset_x = display_def->x;
  606. offset_y = display_def->y;
  607. }
  608. /* Not touching AVSubtitles again*/
  609. if(sub->num_rects) {
  610. avpriv_request_sample(ctx, "Different Version of Segment asked Twice");
  611. return AVERROR_PATCHWELCOME;
  612. }
  613. for (display = ctx->display_list; display; display = display->next) {
  614. region = get_region(ctx, display->region_id);
  615. if (region && region->dirty)
  616. sub->num_rects++;
  617. }
  618. if(ctx->compute_edt == 0) {
  619. sub->end_display_time = ctx->time_out * 1000;
  620. *got_output = 1;
  621. } else if (ctx->prev_start != AV_NOPTS_VALUE) {
  622. sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
  623. *got_output = 1;
  624. }
  625. if (sub->num_rects > 0) {
  626. sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
  627. if (!sub->rects) {
  628. ret = AVERROR(ENOMEM);
  629. goto fail;
  630. }
  631. for(i=0; i<sub->num_rects; i++)
  632. sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
  633. i = 0;
  634. for (display = ctx->display_list; display; display = display->next) {
  635. region = get_region(ctx, display->region_id);
  636. if (!region)
  637. continue;
  638. if (!region->dirty)
  639. continue;
  640. rect = sub->rects[i];
  641. rect->x = display->x_pos + offset_x;
  642. rect->y = display->y_pos + offset_y;
  643. rect->w = region->width;
  644. rect->h = region->height;
  645. rect->nb_colors = (1 << region->depth);
  646. rect->type = SUBTITLE_BITMAP;
  647. rect->linesize[0] = region->width;
  648. clut = get_clut(ctx, region->clut);
  649. if (!clut)
  650. clut = &default_clut;
  651. switch (region->depth) {
  652. case 2:
  653. clut_table = clut->clut4;
  654. break;
  655. case 8:
  656. clut_table = clut->clut256;
  657. break;
  658. case 4:
  659. default:
  660. clut_table = clut->clut16;
  661. break;
  662. }
  663. rect->data[1] = av_mallocz(AVPALETTE_SIZE);
  664. if (!rect->data[1]) {
  665. ret = AVERROR(ENOMEM);
  666. goto fail;
  667. }
  668. memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
  669. rect->data[0] = av_malloc(region->buf_size);
  670. if (!rect->data[0]) {
  671. ret = AVERROR(ENOMEM);
  672. goto fail;
  673. }
  674. memcpy(rect->data[0], region->pbuf, region->buf_size);
  675. if ((clut == &default_clut && ctx->compute_clut == -1) || ctx->compute_clut == 1)
  676. compute_default_clut(rect, rect->w, rect->h);
  677. #if FF_API_AVPICTURE
  678. FF_DISABLE_DEPRECATION_WARNINGS
  679. {
  680. int j;
  681. for (j = 0; j < 4; j++) {
  682. rect->pict.data[j] = rect->data[j];
  683. rect->pict.linesize[j] = rect->linesize[j];
  684. }
  685. }
  686. FF_ENABLE_DEPRECATION_WARNINGS
  687. #endif
  688. i++;
  689. }
  690. }
  691. return 0;
  692. fail:
  693. if (sub->rects) {
  694. for(i=0; i<sub->num_rects; i++) {
  695. rect = sub->rects[i];
  696. if (rect) {
  697. av_freep(&rect->data[0]);
  698. av_freep(&rect->data[1]);
  699. }
  700. av_freep(&sub->rects[i]);
  701. }
  702. av_freep(&sub->rects);
  703. }
  704. sub->num_rects = 0;
  705. return ret;
  706. }
  707. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  708. const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  709. {
  710. DVBSubContext *ctx = avctx->priv_data;
  711. DVBSubRegion *region = get_region(ctx, display->region_id);
  712. const uint8_t *buf_end = buf + buf_size;
  713. uint8_t *pbuf;
  714. int x_pos, y_pos;
  715. int i;
  716. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  717. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  718. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  719. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  720. uint8_t *map_table;
  721. #if 0
  722. ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
  723. top_bottom ? "bottom" : "top");
  724. for (i = 0; i < buf_size; i++) {
  725. if (i % 16 == 0)
  726. ff_dlog(avctx, "0x%8p: ", buf+i);
  727. ff_dlog(avctx, "%02x ", buf[i]);
  728. if (i % 16 == 15)
  729. ff_dlog(avctx, "\n");
  730. }
  731. if (i % 16)
  732. ff_dlog(avctx, "\n");
  733. #endif
  734. if (!region)
  735. return;
  736. pbuf = region->pbuf;
  737. region->dirty = 1;
  738. x_pos = display->x_pos;
  739. y_pos = display->y_pos;
  740. y_pos += top_bottom;
  741. while (buf < buf_end) {
  742. if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
  743. av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
  744. return;
  745. }
  746. switch (*buf++) {
  747. case 0x10:
  748. if (region->depth == 8)
  749. map_table = map2to8;
  750. else if (region->depth == 4)
  751. map_table = map2to4;
  752. else
  753. map_table = NULL;
  754. x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
  755. region->width, &buf, buf_end - buf,
  756. non_mod, map_table, x_pos);
  757. break;
  758. case 0x11:
  759. if (region->depth < 4) {
  760. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  761. return;
  762. }
  763. if (region->depth == 8)
  764. map_table = map4to8;
  765. else
  766. map_table = NULL;
  767. x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
  768. region->width, &buf, buf_end - buf,
  769. non_mod, map_table, x_pos);
  770. break;
  771. case 0x12:
  772. if (region->depth < 8) {
  773. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  774. return;
  775. }
  776. x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
  777. region->width, &buf, buf_end - buf,
  778. non_mod, NULL, x_pos);
  779. break;
  780. case 0x20:
  781. map2to4[0] = (*buf) >> 4;
  782. map2to4[1] = (*buf++) & 0xf;
  783. map2to4[2] = (*buf) >> 4;
  784. map2to4[3] = (*buf++) & 0xf;
  785. break;
  786. case 0x21:
  787. for (i = 0; i < 4; i++)
  788. map2to8[i] = *buf++;
  789. break;
  790. case 0x22:
  791. for (i = 0; i < 16; i++)
  792. map4to8[i] = *buf++;
  793. break;
  794. case 0xf0:
  795. x_pos = display->x_pos;
  796. y_pos += 2;
  797. break;
  798. default:
  799. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  800. }
  801. }
  802. }
  803. static int dvbsub_parse_object_segment(AVCodecContext *avctx,
  804. const uint8_t *buf, int buf_size)
  805. {
  806. DVBSubContext *ctx = avctx->priv_data;
  807. const uint8_t *buf_end = buf + buf_size;
  808. int object_id;
  809. DVBSubObject *object;
  810. DVBSubObjectDisplay *display;
  811. int top_field_len, bottom_field_len;
  812. int coding_method, non_modifying_color;
  813. object_id = AV_RB16(buf);
  814. buf += 2;
  815. object = get_object(ctx, object_id);
  816. if (!object)
  817. return AVERROR_INVALIDDATA;
  818. coding_method = ((*buf) >> 2) & 3;
  819. non_modifying_color = ((*buf++) >> 1) & 1;
  820. if (coding_method == 0) {
  821. top_field_len = AV_RB16(buf);
  822. buf += 2;
  823. bottom_field_len = AV_RB16(buf);
  824. buf += 2;
  825. if (buf + top_field_len + bottom_field_len > buf_end) {
  826. av_log(avctx, AV_LOG_ERROR, "Field data size %d+%d too large\n", top_field_len, bottom_field_len);
  827. return AVERROR_INVALIDDATA;
  828. }
  829. for (display = object->display_list; display; display = display->object_list_next) {
  830. const uint8_t *block = buf;
  831. int bfl = bottom_field_len;
  832. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  833. non_modifying_color);
  834. if (bottom_field_len > 0)
  835. block = buf + top_field_len;
  836. else
  837. bfl = top_field_len;
  838. dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
  839. non_modifying_color);
  840. }
  841. /* } else if (coding_method == 1) {*/
  842. } else {
  843. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  844. }
  845. return 0;
  846. }
  847. static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
  848. const uint8_t *buf, int buf_size)
  849. {
  850. DVBSubContext *ctx = avctx->priv_data;
  851. const uint8_t *buf_end = buf + buf_size;
  852. int i, clut_id;
  853. int version;
  854. DVBSubCLUT *clut;
  855. int entry_id, depth , full_range;
  856. int y, cr, cb, alpha;
  857. int r, g, b, r_add, g_add, b_add;
  858. ff_dlog(avctx, "DVB clut packet:\n");
  859. for (i=0; i < buf_size; i++) {
  860. ff_dlog(avctx, "%02x ", buf[i]);
  861. if (i % 16 == 15)
  862. ff_dlog(avctx, "\n");
  863. }
  864. if (i % 16)
  865. ff_dlog(avctx, "\n");
  866. clut_id = *buf++;
  867. version = ((*buf)>>4)&15;
  868. buf += 1;
  869. clut = get_clut(ctx, clut_id);
  870. if (!clut) {
  871. clut = av_malloc(sizeof(DVBSubCLUT));
  872. if (!clut)
  873. return AVERROR(ENOMEM);
  874. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  875. clut->id = clut_id;
  876. clut->version = -1;
  877. clut->next = ctx->clut_list;
  878. ctx->clut_list = clut;
  879. }
  880. if (clut->version != version) {
  881. clut->version = version;
  882. while (buf + 4 < buf_end) {
  883. entry_id = *buf++;
  884. depth = (*buf) & 0xe0;
  885. if (depth == 0) {
  886. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  887. }
  888. full_range = (*buf++) & 1;
  889. if (full_range) {
  890. y = *buf++;
  891. cr = *buf++;
  892. cb = *buf++;
  893. alpha = *buf++;
  894. } else {
  895. y = buf[0] & 0xfc;
  896. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  897. cb = (buf[1] << 2) & 0xf0;
  898. alpha = (buf[1] << 6) & 0xc0;
  899. buf += 2;
  900. }
  901. if (y == 0)
  902. alpha = 0xff;
  903. YUV_TO_RGB1_CCIR(cb, cr);
  904. YUV_TO_RGB2_CCIR(r, g, b, y);
  905. ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  906. if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
  907. ff_dlog(avctx, "More than one bit level marked: %x\n", depth);
  908. if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
  909. return AVERROR_INVALIDDATA;
  910. }
  911. if (depth & 0x80 && entry_id < 4)
  912. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  913. else if (depth & 0x40 && entry_id < 16)
  914. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  915. else if (depth & 0x20)
  916. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  917. }
  918. }
  919. return 0;
  920. }
  921. static int dvbsub_parse_region_segment(AVCodecContext *avctx,
  922. const uint8_t *buf, int buf_size)
  923. {
  924. DVBSubContext *ctx = avctx->priv_data;
  925. const uint8_t *buf_end = buf + buf_size;
  926. int region_id, object_id;
  927. int av_unused version;
  928. DVBSubRegion *region;
  929. DVBSubObject *object;
  930. DVBSubObjectDisplay *display;
  931. int fill;
  932. int ret;
  933. if (buf_size < 10)
  934. return AVERROR_INVALIDDATA;
  935. region_id = *buf++;
  936. region = get_region(ctx, region_id);
  937. if (!region) {
  938. region = av_mallocz(sizeof(DVBSubRegion));
  939. if (!region)
  940. return AVERROR(ENOMEM);
  941. region->id = region_id;
  942. region->version = -1;
  943. region->next = ctx->region_list;
  944. ctx->region_list = region;
  945. }
  946. version = ((*buf)>>4) & 15;
  947. fill = ((*buf++) >> 3) & 1;
  948. region->width = AV_RB16(buf);
  949. buf += 2;
  950. region->height = AV_RB16(buf);
  951. buf += 2;
  952. ret = av_image_check_size2(region->width, region->height, avctx->max_pixels, AV_PIX_FMT_PAL8, 0, avctx);
  953. if (ret >= 0 && region->width * region->height * 2 > 320 * 1024 * 8) {
  954. ret = AVERROR_INVALIDDATA;
  955. av_log(avctx, AV_LOG_ERROR, "Pixel buffer memory constraint violated\n");
  956. }
  957. if (ret < 0) {
  958. region->width= region->height= 0;
  959. return ret;
  960. }
  961. if (region->width * region->height != region->buf_size) {
  962. av_free(region->pbuf);
  963. region->buf_size = region->width * region->height;
  964. region->pbuf = av_malloc(region->buf_size);
  965. if (!region->pbuf) {
  966. region->buf_size =
  967. region->width =
  968. region->height = 0;
  969. return AVERROR(ENOMEM);
  970. }
  971. fill = 1;
  972. region->dirty = 0;
  973. }
  974. region->depth = 1 << (((*buf++) >> 2) & 7);
  975. if(region->depth<2 || region->depth>8){
  976. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  977. region->depth= 4;
  978. }
  979. region->clut = *buf++;
  980. if (region->depth == 8) {
  981. region->bgcolor = *buf++;
  982. buf += 1;
  983. } else {
  984. buf += 1;
  985. if (region->depth == 4)
  986. region->bgcolor = (((*buf++) >> 4) & 15);
  987. else
  988. region->bgcolor = (((*buf++) >> 2) & 3);
  989. }
  990. ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  991. if (fill) {
  992. memset(region->pbuf, region->bgcolor, region->buf_size);
  993. ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
  994. }
  995. delete_region_display_list(ctx, region);
  996. while (buf + 5 < buf_end) {
  997. object_id = AV_RB16(buf);
  998. buf += 2;
  999. object = get_object(ctx, object_id);
  1000. if (!object) {
  1001. object = av_mallocz(sizeof(DVBSubObject));
  1002. if (!object)
  1003. return AVERROR(ENOMEM);
  1004. object->id = object_id;
  1005. object->next = ctx->object_list;
  1006. ctx->object_list = object;
  1007. }
  1008. object->type = (*buf) >> 6;
  1009. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  1010. if (!display)
  1011. return AVERROR(ENOMEM);
  1012. display->object_id = object_id;
  1013. display->region_id = region_id;
  1014. display->x_pos = AV_RB16(buf) & 0xfff;
  1015. buf += 2;
  1016. display->y_pos = AV_RB16(buf) & 0xfff;
  1017. buf += 2;
  1018. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  1019. display->fgcolor = *buf++;
  1020. display->bgcolor = *buf++;
  1021. }
  1022. display->region_list_next = region->display_list;
  1023. region->display_list = display;
  1024. display->object_list_next = object->display_list;
  1025. object->display_list = display;
  1026. }
  1027. return 0;
  1028. }
  1029. static int dvbsub_parse_page_segment(AVCodecContext *avctx,
  1030. const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
  1031. {
  1032. DVBSubContext *ctx = avctx->priv_data;
  1033. DVBSubRegionDisplay *display;
  1034. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  1035. const uint8_t *buf_end = buf + buf_size;
  1036. int region_id;
  1037. int page_state;
  1038. int timeout;
  1039. int version;
  1040. if (buf_size < 1)
  1041. return AVERROR_INVALIDDATA;
  1042. timeout = *buf++;
  1043. version = ((*buf)>>4) & 15;
  1044. page_state = ((*buf++) >> 2) & 3;
  1045. if (ctx->version == version) {
  1046. return 0;
  1047. }
  1048. ctx->time_out = timeout;
  1049. ctx->version = version;
  1050. ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  1051. if(ctx->compute_edt == 1)
  1052. save_subtitle_set(avctx, sub, got_output);
  1053. if (page_state == 1 || page_state == 2) {
  1054. delete_regions(ctx);
  1055. delete_objects(ctx);
  1056. delete_cluts(ctx);
  1057. }
  1058. tmp_display_list = ctx->display_list;
  1059. ctx->display_list = NULL;
  1060. while (buf + 5 < buf_end) {
  1061. region_id = *buf++;
  1062. buf += 1;
  1063. display = ctx->display_list;
  1064. while (display && display->region_id != region_id) {
  1065. display = display->next;
  1066. }
  1067. if (display) {
  1068. av_log(avctx, AV_LOG_ERROR, "duplicate region\n");
  1069. break;
  1070. }
  1071. display = tmp_display_list;
  1072. tmp_ptr = &tmp_display_list;
  1073. while (display && display->region_id != region_id) {
  1074. tmp_ptr = &display->next;
  1075. display = display->next;
  1076. }
  1077. if (!display) {
  1078. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  1079. if (!display)
  1080. return AVERROR(ENOMEM);
  1081. }
  1082. display->region_id = region_id;
  1083. display->x_pos = AV_RB16(buf);
  1084. buf += 2;
  1085. display->y_pos = AV_RB16(buf);
  1086. buf += 2;
  1087. *tmp_ptr = display->next;
  1088. display->next = ctx->display_list;
  1089. ctx->display_list = display;
  1090. ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  1091. }
  1092. while (tmp_display_list) {
  1093. display = tmp_display_list;
  1094. tmp_display_list = display->next;
  1095. av_freep(&display);
  1096. }
  1097. return 0;
  1098. }
  1099. #ifdef DEBUG
  1100. static void png_save(DVBSubContext *ctx, const char *filename, uint32_t *bitmap, int w, int h)
  1101. {
  1102. int x, y, v;
  1103. FILE *f;
  1104. char fname[40], fname2[40];
  1105. char command[1024];
  1106. snprintf(fname, sizeof(fname), "%s.ppm", filename);
  1107. f = fopen(fname, "w");
  1108. if (!f) {
  1109. perror(fname);
  1110. return;
  1111. }
  1112. fprintf(f, "P6\n"
  1113. "%d %d\n"
  1114. "%d\n",
  1115. w, h, 255);
  1116. for(y = 0; y < h; y++) {
  1117. for(x = 0; x < w; x++) {
  1118. v = bitmap[y * w + x];
  1119. putc((v >> 16) & 0xff, f);
  1120. putc((v >> 8) & 0xff, f);
  1121. putc((v >> 0) & 0xff, f);
  1122. }
  1123. }
  1124. fclose(f);
  1125. snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
  1126. f = fopen(fname2, "w");
  1127. if (!f) {
  1128. perror(fname2);
  1129. return;
  1130. }
  1131. fprintf(f, "P5\n"
  1132. "%d %d\n"
  1133. "%d\n",
  1134. w, h, 255);
  1135. for(y = 0; y < h; y++) {
  1136. for(x = 0; x < w; x++) {
  1137. v = bitmap[y * w + x];
  1138. putc((v >> 24) & 0xff, f);
  1139. }
  1140. }
  1141. fclose(f);
  1142. snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
  1143. if (system(command) != 0) {
  1144. av_log(ctx, AV_LOG_ERROR, "Error running pnmtopng\n");
  1145. return;
  1146. }
  1147. snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
  1148. if (system(command) != 0) {
  1149. av_log(ctx, AV_LOG_ERROR, "Error removing %s and %s\n", fname, fname2);
  1150. return;
  1151. }
  1152. }
  1153. static int save_display_set(DVBSubContext *ctx)
  1154. {
  1155. DVBSubRegion *region;
  1156. DVBSubRegionDisplay *display;
  1157. DVBSubCLUT *clut;
  1158. uint32_t *clut_table;
  1159. int x_pos, y_pos, width, height;
  1160. int x, y, y_off, x_off;
  1161. uint32_t *pbuf;
  1162. char filename[32];
  1163. static int fileno_index = 0;
  1164. x_pos = -1;
  1165. y_pos = -1;
  1166. width = 0;
  1167. height = 0;
  1168. for (display = ctx->display_list; display; display = display->next) {
  1169. region = get_region(ctx, display->region_id);
  1170. if (!region)
  1171. return -1;
  1172. if (x_pos == -1) {
  1173. x_pos = display->x_pos;
  1174. y_pos = display->y_pos;
  1175. width = region->width;
  1176. height = region->height;
  1177. } else {
  1178. if (display->x_pos < x_pos) {
  1179. width += (x_pos - display->x_pos);
  1180. x_pos = display->x_pos;
  1181. }
  1182. if (display->y_pos < y_pos) {
  1183. height += (y_pos - display->y_pos);
  1184. y_pos = display->y_pos;
  1185. }
  1186. if (display->x_pos + region->width > x_pos + width) {
  1187. width = display->x_pos + region->width - x_pos;
  1188. }
  1189. if (display->y_pos + region->height > y_pos + height) {
  1190. height = display->y_pos + region->height - y_pos;
  1191. }
  1192. }
  1193. }
  1194. if (x_pos >= 0) {
  1195. pbuf = av_malloc(width * height * 4);
  1196. if (!pbuf)
  1197. return -1;
  1198. for (display = ctx->display_list; display; display = display->next) {
  1199. region = get_region(ctx, display->region_id);
  1200. if (!region)
  1201. return -1;
  1202. x_off = display->x_pos - x_pos;
  1203. y_off = display->y_pos - y_pos;
  1204. clut = get_clut(ctx, region->clut);
  1205. if (!clut)
  1206. clut = &default_clut;
  1207. switch (region->depth) {
  1208. case 2:
  1209. clut_table = clut->clut4;
  1210. break;
  1211. case 8:
  1212. clut_table = clut->clut256;
  1213. break;
  1214. case 4:
  1215. default:
  1216. clut_table = clut->clut16;
  1217. break;
  1218. }
  1219. for (y = 0; y < region->height; y++) {
  1220. for (x = 0; x < region->width; x++) {
  1221. pbuf[((y + y_off) * width) + x_off + x] =
  1222. clut_table[region->pbuf[y * region->width + x]];
  1223. }
  1224. }
  1225. }
  1226. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  1227. png_save(ctx, filename, pbuf, width, height);
  1228. av_freep(&pbuf);
  1229. }
  1230. fileno_index++;
  1231. return 0;
  1232. }
  1233. #endif /* DEBUG */
  1234. static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
  1235. const uint8_t *buf,
  1236. int buf_size)
  1237. {
  1238. DVBSubContext *ctx = avctx->priv_data;
  1239. DVBSubDisplayDefinition *display_def = ctx->display_definition;
  1240. int dds_version, info_byte;
  1241. if (buf_size < 5)
  1242. return AVERROR_INVALIDDATA;
  1243. info_byte = bytestream_get_byte(&buf);
  1244. dds_version = info_byte >> 4;
  1245. if (display_def && display_def->version == dds_version)
  1246. return 0; // already have this display definition version
  1247. if (!display_def) {
  1248. display_def = av_mallocz(sizeof(*display_def));
  1249. if (!display_def)
  1250. return AVERROR(ENOMEM);
  1251. ctx->display_definition = display_def;
  1252. }
  1253. display_def->version = dds_version;
  1254. display_def->x = 0;
  1255. display_def->y = 0;
  1256. display_def->width = bytestream_get_be16(&buf) + 1;
  1257. display_def->height = bytestream_get_be16(&buf) + 1;
  1258. if (!avctx->width || !avctx->height) {
  1259. avctx->width = display_def->width;
  1260. avctx->height = display_def->height;
  1261. }
  1262. if (info_byte & 1<<3) { // display_window_flag
  1263. if (buf_size < 13)
  1264. return AVERROR_INVALIDDATA;
  1265. display_def->x = bytestream_get_be16(&buf);
  1266. display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
  1267. display_def->y = bytestream_get_be16(&buf);
  1268. display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
  1269. }
  1270. return 0;
  1271. }
  1272. static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1273. int buf_size, AVSubtitle *sub,int *got_output)
  1274. {
  1275. DVBSubContext *ctx = avctx->priv_data;
  1276. if(ctx->compute_edt == 0)
  1277. save_subtitle_set(avctx, sub, got_output);
  1278. #ifdef DEBUG
  1279. save_display_set(ctx);
  1280. #endif
  1281. return 0;
  1282. }
  1283. static int dvbsub_decode(AVCodecContext *avctx,
  1284. void *data, int *data_size,
  1285. AVPacket *avpkt)
  1286. {
  1287. const uint8_t *buf = avpkt->data;
  1288. int buf_size = avpkt->size;
  1289. DVBSubContext *ctx = avctx->priv_data;
  1290. AVSubtitle *sub = data;
  1291. const uint8_t *p, *p_end;
  1292. int segment_type;
  1293. int page_id;
  1294. int segment_length;
  1295. int i;
  1296. int ret = 0;
  1297. int got_segment = 0;
  1298. int got_dds = 0;
  1299. ff_dlog(avctx, "DVB sub packet:\n");
  1300. for (i=0; i < buf_size; i++) {
  1301. ff_dlog(avctx, "%02x ", buf[i]);
  1302. if (i % 16 == 15)
  1303. ff_dlog(avctx, "\n");
  1304. }
  1305. if (i % 16)
  1306. ff_dlog(avctx, "\n");
  1307. if (buf_size <= 6 || *buf != 0x0f) {
  1308. ff_dlog(avctx, "incomplete or broken packet");
  1309. return AVERROR_INVALIDDATA;
  1310. }
  1311. p = buf;
  1312. p_end = buf + buf_size;
  1313. while (p_end - p >= 6 && *p == 0x0f) {
  1314. p += 1;
  1315. segment_type = *p++;
  1316. page_id = AV_RB16(p);
  1317. p += 2;
  1318. segment_length = AV_RB16(p);
  1319. p += 2;
  1320. if (avctx->debug & FF_DEBUG_STARTCODE) {
  1321. av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
  1322. }
  1323. if (p_end - p < segment_length) {
  1324. ff_dlog(avctx, "incomplete or broken packet");
  1325. ret = -1;
  1326. goto end;
  1327. }
  1328. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
  1329. ctx->composition_id == -1 || ctx->ancillary_id == -1) {
  1330. int ret = 0;
  1331. switch (segment_type) {
  1332. case DVBSUB_PAGE_SEGMENT:
  1333. ret = dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
  1334. got_segment |= 1;
  1335. break;
  1336. case DVBSUB_REGION_SEGMENT:
  1337. ret = dvbsub_parse_region_segment(avctx, p, segment_length);
  1338. got_segment |= 2;
  1339. break;
  1340. case DVBSUB_CLUT_SEGMENT:
  1341. ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
  1342. if (ret < 0) goto end;
  1343. got_segment |= 4;
  1344. break;
  1345. case DVBSUB_OBJECT_SEGMENT:
  1346. ret = dvbsub_parse_object_segment(avctx, p, segment_length);
  1347. got_segment |= 8;
  1348. break;
  1349. case DVBSUB_DISPLAYDEFINITION_SEGMENT:
  1350. ret = dvbsub_parse_display_definition_segment(avctx, p,
  1351. segment_length);
  1352. got_dds = 1;
  1353. break;
  1354. case DVBSUB_DISPLAY_SEGMENT:
  1355. ret = dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
  1356. if (got_segment == 15 && !got_dds && !avctx->width && !avctx->height) {
  1357. // Default from ETSI EN 300 743 V1.3.1 (7.2.1)
  1358. avctx->width = 720;
  1359. avctx->height = 576;
  1360. }
  1361. got_segment |= 16;
  1362. break;
  1363. default:
  1364. ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1365. segment_type, page_id, segment_length);
  1366. break;
  1367. }
  1368. if (ret < 0)
  1369. goto end;
  1370. }
  1371. p += segment_length;
  1372. }
  1373. // Some streams do not send a display segment but if we have all the other
  1374. // segments then we need no further data.
  1375. if (got_segment == 15) {
  1376. av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
  1377. dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
  1378. }
  1379. end:
  1380. if(ret < 0) {
  1381. *data_size = 0;
  1382. avsubtitle_free(sub);
  1383. return ret;
  1384. } else {
  1385. if(ctx->compute_edt == 1 )
  1386. FFSWAP(int64_t, ctx->prev_start, sub->pts);
  1387. }
  1388. return p - buf;
  1389. }
  1390. #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
  1391. static const AVOption options[] = {
  1392. {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DS},
  1393. {"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},
  1394. {"dvb_substream", "", offsetof(DVBSubContext, substream), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, DS},
  1395. {NULL}
  1396. };
  1397. static const AVClass dvbsubdec_class = {
  1398. .class_name = "DVB Sub Decoder",
  1399. .item_name = av_default_item_name,
  1400. .option = options,
  1401. .version = LIBAVUTIL_VERSION_INT,
  1402. };
  1403. AVCodec ff_dvbsub_decoder = {
  1404. .name = "dvbsub",
  1405. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1406. .type = AVMEDIA_TYPE_SUBTITLE,
  1407. .id = AV_CODEC_ID_DVB_SUBTITLE,
  1408. .priv_data_size = sizeof(DVBSubContext),
  1409. .init = dvbsub_init_decoder,
  1410. .close = dvbsub_close_decoder,
  1411. .decode = dvbsub_decode,
  1412. .priv_class = &dvbsubdec_class,
  1413. };