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.

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