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.

1634 lines
44KB

  1. /*
  2. * DVB subtitle decoding for ffmpeg
  3. * Copyright (c) 2005 Ian Caulfield.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "bitstream.h"
  22. //#define DEBUG
  23. //#define DEBUG_PACKET_CONTENTS
  24. //#define DEBUG_SAVE_IMAGES
  25. #define DVBSUB_PAGE_SEGMENT 0x10
  26. #define DVBSUB_REGION_SEGMENT 0x11
  27. #define DVBSUB_CLUT_SEGMENT 0x12
  28. #define DVBSUB_OBJECT_SEGMENT 0x13
  29. #define DVBSUB_DISPLAY_SEGMENT 0x80
  30. static unsigned char *cm;
  31. #ifdef DEBUG_SAVE_IMAGES
  32. #undef fprintf
  33. #if 0
  34. static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
  35. uint32_t *rgba_palette)
  36. {
  37. int x, y, v;
  38. FILE *f;
  39. char fname[40], fname2[40];
  40. char command[1024];
  41. snprintf(fname, 40, "%s.ppm", filename);
  42. f = fopen(fname, "w");
  43. if (!f) {
  44. perror(fname);
  45. exit(1);
  46. }
  47. fprintf(f, "P6\n"
  48. "%d %d\n"
  49. "%d\n",
  50. w, h, 255);
  51. for(y = 0; y < h; y++) {
  52. for(x = 0; x < w; x++) {
  53. v = rgba_palette[bitmap[y * w + x]];
  54. putc((v >> 16) & 0xff, f);
  55. putc((v >> 8) & 0xff, f);
  56. putc((v >> 0) & 0xff, f);
  57. }
  58. }
  59. fclose(f);
  60. snprintf(fname2, 40, "%s-a.pgm", filename);
  61. f = fopen(fname2, "w");
  62. if (!f) {
  63. perror(fname2);
  64. exit(1);
  65. }
  66. fprintf(f, "P5\n"
  67. "%d %d\n"
  68. "%d\n",
  69. w, h, 255);
  70. for(y = 0; y < h; y++) {
  71. for(x = 0; x < w; x++) {
  72. v = rgba_palette[bitmap[y * w + x]];
  73. putc((v >> 24) & 0xff, f);
  74. }
  75. }
  76. fclose(f);
  77. snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
  78. system(command);
  79. snprintf(command, 1024, "rm %s %s", fname, fname2);
  80. system(command);
  81. }
  82. #endif
  83. static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
  84. {
  85. int x, y, v;
  86. FILE *f;
  87. char fname[40], fname2[40];
  88. char command[1024];
  89. snprintf(fname, 40, "%s.ppm", filename);
  90. f = fopen(fname, "w");
  91. if (!f) {
  92. perror(fname);
  93. exit(1);
  94. }
  95. fprintf(f, "P6\n"
  96. "%d %d\n"
  97. "%d\n",
  98. w, h, 255);
  99. for(y = 0; y < h; y++) {
  100. for(x = 0; x < w; x++) {
  101. v = bitmap[y * w + x];
  102. putc((v >> 16) & 0xff, f);
  103. putc((v >> 8) & 0xff, f);
  104. putc((v >> 0) & 0xff, f);
  105. }
  106. }
  107. fclose(f);
  108. snprintf(fname2, 40, "%s-a.pgm", filename);
  109. f = fopen(fname2, "w");
  110. if (!f) {
  111. perror(fname2);
  112. exit(1);
  113. }
  114. fprintf(f, "P5\n"
  115. "%d %d\n"
  116. "%d\n",
  117. w, h, 255);
  118. for(y = 0; y < h; y++) {
  119. for(x = 0; x < w; x++) {
  120. v = bitmap[y * w + x];
  121. putc((v >> 24) & 0xff, f);
  122. }
  123. }
  124. fclose(f);
  125. snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
  126. system(command);
  127. snprintf(command, 1024, "rm %s %s", fname, fname2);
  128. system(command);
  129. }
  130. #endif
  131. #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
  132. typedef struct DVBSubCLUT {
  133. int id;
  134. uint32_t clut4[4];
  135. uint32_t clut16[16];
  136. uint32_t clut256[256];
  137. struct DVBSubCLUT *next;
  138. } DVBSubCLUT;
  139. static DVBSubCLUT default_clut;
  140. typedef struct DVBSubObjectDisplay {
  141. int object_id;
  142. int region_id;
  143. int x_pos;
  144. int y_pos;
  145. int fgcolour;
  146. int bgcolour;
  147. struct DVBSubObjectDisplay *region_list_next;
  148. struct DVBSubObjectDisplay *object_list_next;
  149. } DVBSubObjectDisplay;
  150. typedef struct DVBSubObject {
  151. int id;
  152. int type;
  153. DVBSubObjectDisplay *display_list;
  154. struct DVBSubObject *next;
  155. } DVBSubObject;
  156. typedef struct DVBSubRegionDisplay {
  157. int region_id;
  158. int x_pos;
  159. int y_pos;
  160. struct DVBSubRegionDisplay *next;
  161. } DVBSubRegionDisplay;
  162. typedef struct DVBSubRegion {
  163. int id;
  164. int width;
  165. int height;
  166. int depth;
  167. int clut;
  168. int bgcolour;
  169. uint8_t *pbuf;
  170. int buf_size;
  171. DVBSubObjectDisplay *display_list;
  172. struct DVBSubRegion *next;
  173. } DVBSubRegion;
  174. typedef struct DVBSubContext {
  175. int composition_id;
  176. int ancillary_id;
  177. int time_out;
  178. DVBSubRegion *region_list;
  179. DVBSubCLUT *clut_list;
  180. DVBSubObject *object_list;
  181. int display_list_size;
  182. DVBSubRegionDisplay *display_list;
  183. } DVBSubContext;
  184. static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
  185. {
  186. DVBSubObject *ptr = ctx->object_list;
  187. while (ptr != NULL && ptr->id != object_id) {
  188. ptr = ptr->next;
  189. }
  190. return ptr;
  191. }
  192. static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
  193. {
  194. DVBSubCLUT *ptr = ctx->clut_list;
  195. while (ptr != NULL && ptr->id != clut_id) {
  196. ptr = ptr->next;
  197. }
  198. return ptr;
  199. }
  200. static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
  201. {
  202. DVBSubRegion *ptr = ctx->region_list;
  203. while (ptr != NULL && ptr->id != region_id) {
  204. ptr = ptr->next;
  205. }
  206. return ptr;
  207. }
  208. static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
  209. {
  210. DVBSubObject *object, *obj2, **obj2_ptr;
  211. DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
  212. while (region->display_list != NULL) {
  213. display = region->display_list;
  214. object = get_object(ctx, display->object_id);
  215. if (object != NULL) {
  216. obj_disp = object->display_list;
  217. obj_disp_ptr = &object->display_list;
  218. while (obj_disp != NULL && obj_disp != display) {
  219. obj_disp_ptr = &obj_disp->object_list_next;
  220. obj_disp = obj_disp->object_list_next;
  221. }
  222. if (obj_disp) {
  223. *obj_disp_ptr = obj_disp->object_list_next;
  224. if (object->display_list == NULL) {
  225. obj2 = ctx->object_list;
  226. obj2_ptr = &ctx->object_list;
  227. while (obj2 != NULL && obj2 != object) {
  228. obj2_ptr = &obj2->next;
  229. obj2 = obj2->next;
  230. }
  231. *obj2_ptr = obj2->next;
  232. av_free(obj2);
  233. }
  234. }
  235. }
  236. region->display_list = display->region_list_next;
  237. av_free(display);
  238. }
  239. }
  240. static void delete_state(DVBSubContext *ctx)
  241. {
  242. DVBSubRegion *region;
  243. DVBSubCLUT *clut;
  244. while (ctx->region_list != NULL)
  245. {
  246. region = ctx->region_list;
  247. ctx->region_list = region->next;
  248. delete_region_display_list(ctx, region);
  249. if (region->pbuf != NULL)
  250. av_free(region->pbuf);
  251. av_free(region);
  252. }
  253. while (ctx->clut_list != NULL)
  254. {
  255. clut = ctx->clut_list;
  256. ctx->clut_list = clut->next;
  257. av_free(clut);
  258. }
  259. /* Should already be null */
  260. if (ctx->object_list != NULL)
  261. av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
  262. }
  263. static int dvbsub_init_decoder(AVCodecContext *avctx)
  264. {
  265. int i, r, g, b, a = 0;
  266. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  267. cm = cropTbl + MAX_NEG_CROP;
  268. memset(avctx->priv_data, 0, sizeof(DVBSubContext));
  269. ctx->composition_id = avctx->sub_id & 0xffff;
  270. ctx->ancillary_id = avctx->sub_id >> 16;
  271. default_clut.id = -1;
  272. default_clut.next = NULL;
  273. default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
  274. default_clut.clut4[1] = RGBA(255, 255, 255, 255);
  275. default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
  276. default_clut.clut4[3] = RGBA(127, 127, 127, 255);
  277. default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
  278. for (i = 1; i < 16; i++) {
  279. if (i < 8) {
  280. r = (i & 1) ? 255 : 0;
  281. g = (i & 2) ? 255 : 0;
  282. b = (i & 4) ? 255 : 0;
  283. } else {
  284. r = (i & 1) ? 127 : 0;
  285. g = (i & 2) ? 127 : 0;
  286. b = (i & 4) ? 127 : 0;
  287. }
  288. default_clut.clut16[i] = RGBA(r, g, b, 255);
  289. }
  290. default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
  291. for (i = 1; i < 256; i++) {
  292. if (i < 8) {
  293. r = (i & 1) ? 255 : 0;
  294. g = (i & 2) ? 255 : 0;
  295. b = (i & 4) ? 255 : 0;
  296. a = 63;
  297. } else {
  298. switch (i & 0x88) {
  299. case 0x00:
  300. r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
  301. g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
  302. b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
  303. a = 255;
  304. break;
  305. case 0x08:
  306. r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
  307. g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
  308. b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
  309. a = 127;
  310. break;
  311. case 0x80:
  312. r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
  313. g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
  314. b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
  315. a = 255;
  316. break;
  317. case 0x88:
  318. r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
  319. g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
  320. b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
  321. a = 255;
  322. break;
  323. }
  324. }
  325. default_clut.clut256[i] = RGBA(r, g, b, a);
  326. }
  327. return 0;
  328. }
  329. static int dvbsub_close_decoder(AVCodecContext *avctx)
  330. {
  331. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  332. DVBSubRegionDisplay *display;
  333. delete_state(ctx);
  334. while (ctx->display_list != NULL)
  335. {
  336. display = ctx->display_list;
  337. ctx->display_list = display->next;
  338. av_free(display);
  339. }
  340. return 0;
  341. }
  342. static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
  343. uint8_t **srcbuf, int buf_size,
  344. int non_mod, uint8_t *map_table)
  345. {
  346. GetBitContext gb;
  347. int bits;
  348. int run_length;
  349. int pixels_read = 0;
  350. init_get_bits(&gb, *srcbuf, buf_size << 8);
  351. while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
  352. bits = get_bits(&gb, 2);
  353. if (bits != 0) {
  354. if (non_mod != 1 || bits != 1) {
  355. if (map_table != NULL)
  356. *destbuf++ = map_table[bits];
  357. else
  358. *destbuf++ = bits;
  359. }
  360. pixels_read++;
  361. } else {
  362. bits = get_bits(&gb, 1);
  363. if (bits == 1) {
  364. run_length = get_bits(&gb, 3) + 3;
  365. bits = get_bits(&gb, 2);
  366. if (non_mod == 1 && bits == 1)
  367. pixels_read += run_length;
  368. else {
  369. if (map_table != NULL)
  370. bits = map_table[bits];
  371. while (run_length-- > 0 && pixels_read < dbuf_len) {
  372. *destbuf++ = bits;
  373. pixels_read++;
  374. }
  375. }
  376. } else {
  377. bits = get_bits(&gb, 1);
  378. if (bits == 0) {
  379. bits = get_bits(&gb, 2);
  380. if (bits == 2) {
  381. run_length = get_bits(&gb, 4) + 12;
  382. bits = get_bits(&gb, 2);
  383. if (non_mod == 1 && bits == 1)
  384. pixels_read += run_length;
  385. else {
  386. if (map_table != NULL)
  387. bits = map_table[bits];
  388. while (run_length-- > 0 && pixels_read < dbuf_len) {
  389. *destbuf++ = bits;
  390. pixels_read++;
  391. }
  392. }
  393. } else if (bits == 3) {
  394. run_length = get_bits(&gb, 8) + 29;
  395. bits = get_bits(&gb, 2);
  396. if (non_mod == 1 && bits == 1)
  397. pixels_read += run_length;
  398. else {
  399. if (map_table != NULL)
  400. bits = map_table[bits];
  401. while (run_length-- > 0 && pixels_read < dbuf_len) {
  402. *destbuf++ = bits;
  403. pixels_read++;
  404. }
  405. }
  406. } else if (bits == 1) {
  407. pixels_read += 2;
  408. if (map_table != NULL)
  409. bits = map_table[0];
  410. else
  411. bits = 0;
  412. if (pixels_read <= dbuf_len) {
  413. *destbuf++ = bits;
  414. *destbuf++ = bits;
  415. }
  416. } else {
  417. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  418. return pixels_read;
  419. }
  420. } else {
  421. if (map_table != NULL)
  422. bits = map_table[0];
  423. else
  424. bits = 0;
  425. *destbuf++ = bits;
  426. pixels_read++;
  427. }
  428. }
  429. }
  430. }
  431. if (get_bits(&gb, 6) != 0)
  432. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  433. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  434. return pixels_read;
  435. }
  436. static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
  437. uint8_t **srcbuf, int buf_size,
  438. int non_mod, uint8_t *map_table)
  439. {
  440. GetBitContext gb;
  441. int bits;
  442. int run_length;
  443. int pixels_read = 0;
  444. init_get_bits(&gb, *srcbuf, buf_size << 8);
  445. while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
  446. bits = get_bits(&gb, 4);
  447. if (bits != 0) {
  448. if (non_mod != 1 || bits != 1) {
  449. if (map_table != NULL)
  450. *destbuf++ = map_table[bits];
  451. else
  452. *destbuf++ = bits;
  453. }
  454. pixels_read++;
  455. } else {
  456. bits = get_bits(&gb, 1);
  457. if (bits == 0) {
  458. run_length = get_bits(&gb, 3);
  459. if (run_length == 0) {
  460. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  461. return pixels_read;
  462. }
  463. run_length += 2;
  464. if (map_table != NULL)
  465. bits = map_table[0];
  466. else
  467. bits = 0;
  468. while (run_length-- > 0 && pixels_read < dbuf_len) {
  469. *destbuf++ = bits;
  470. pixels_read++;
  471. }
  472. } else {
  473. bits = get_bits(&gb, 1);
  474. if (bits == 0) {
  475. run_length = get_bits(&gb, 2) + 4;
  476. bits = get_bits(&gb, 4);
  477. if (non_mod == 1 && bits == 1)
  478. pixels_read += run_length;
  479. else {
  480. if (map_table != NULL)
  481. bits = map_table[bits];
  482. while (run_length-- > 0 && pixels_read < dbuf_len) {
  483. *destbuf++ = bits;
  484. pixels_read++;
  485. }
  486. }
  487. } else {
  488. bits = get_bits(&gb, 2);
  489. if (bits == 2) {
  490. run_length = get_bits(&gb, 4) + 9;
  491. bits = get_bits(&gb, 4);
  492. if (non_mod == 1 && bits == 1)
  493. pixels_read += run_length;
  494. else {
  495. if (map_table != NULL)
  496. bits = map_table[bits];
  497. while (run_length-- > 0 && pixels_read < dbuf_len) {
  498. *destbuf++ = bits;
  499. pixels_read++;
  500. }
  501. }
  502. } else if (bits == 3) {
  503. run_length = get_bits(&gb, 8) + 25;
  504. bits = get_bits(&gb, 4);
  505. if (non_mod == 1 && bits == 1)
  506. pixels_read += run_length;
  507. else {
  508. if (map_table != NULL)
  509. bits = map_table[bits];
  510. while (run_length-- > 0 && pixels_read < dbuf_len) {
  511. *destbuf++ = bits;
  512. pixels_read++;
  513. }
  514. }
  515. } else if (bits == 1) {
  516. pixels_read += 2;
  517. if (map_table != NULL)
  518. bits = map_table[0];
  519. else
  520. bits = 0;
  521. if (pixels_read <= dbuf_len) {
  522. *destbuf++ = bits;
  523. *destbuf++ = bits;
  524. }
  525. } else {
  526. if (map_table != NULL)
  527. bits = map_table[0];
  528. else
  529. bits = 0;
  530. *destbuf++ = bits;
  531. pixels_read ++;
  532. }
  533. }
  534. }
  535. }
  536. }
  537. if (get_bits(&gb, 8) != 0)
  538. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  539. (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
  540. return pixels_read;
  541. }
  542. static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
  543. uint8_t **srcbuf, int buf_size,
  544. int non_mod, uint8_t *map_table)
  545. {
  546. uint8_t *sbuf_end = (*srcbuf) + buf_size;
  547. int bits;
  548. int run_length;
  549. int pixels_read = 0;
  550. while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
  551. bits = *(*srcbuf)++;
  552. if (bits != 0) {
  553. if (non_mod != 1 || bits != 1) {
  554. if (map_table != NULL)
  555. *destbuf++ = map_table[bits];
  556. else
  557. *destbuf++ = bits;
  558. }
  559. pixels_read++;
  560. } else {
  561. bits = *(*srcbuf)++;
  562. run_length = bits & 0x7f;
  563. if ((bits & 0x80) == 0) {
  564. if (run_length == 0) {
  565. return pixels_read;
  566. }
  567. if (map_table != NULL)
  568. bits = map_table[0];
  569. else
  570. bits = 0;
  571. while (run_length-- > 0 && pixels_read < dbuf_len) {
  572. *destbuf++ = bits;
  573. pixels_read++;
  574. }
  575. } else {
  576. bits = *(*srcbuf)++;
  577. if (non_mod == 1 && bits == 1)
  578. pixels_read += run_length;
  579. if (map_table != NULL)
  580. bits = map_table[bits];
  581. else while (run_length-- > 0 && pixels_read < dbuf_len) {
  582. *destbuf++ = bits;
  583. pixels_read++;
  584. }
  585. }
  586. }
  587. }
  588. if (*(*srcbuf)++ != 0)
  589. av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
  590. return pixels_read;
  591. }
  592. static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
  593. uint8_t *buf, int buf_size, int top_bottom, int non_mod)
  594. {
  595. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  596. DVBSubRegion *region = get_region(ctx, display->region_id);
  597. uint8_t *buf_end = buf + buf_size;
  598. uint8_t *pbuf;
  599. int x_pos, y_pos;
  600. int i;
  601. uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
  602. uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
  603. uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  604. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  605. uint8_t *map_table;
  606. #ifdef DEBUG
  607. av_log(avctx, AV_LOG_INFO, "DVB pixel block size %d, %s field:\n", buf_size,
  608. top_bottom ? "bottom" : "top");
  609. #endif
  610. #ifdef DEBUG_PACKET_CONTENTS
  611. for (i = 0; i < buf_size; i++)
  612. {
  613. if (i % 16 == 0)
  614. av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
  615. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  616. if (i % 16 == 15)
  617. av_log(avctx, AV_LOG_INFO, "\n");
  618. }
  619. if (i % 16 != 0)
  620. av_log(avctx, AV_LOG_INFO, "\n");
  621. #endif
  622. if (region == 0)
  623. return;
  624. pbuf = region->pbuf;
  625. x_pos = display->x_pos;
  626. y_pos = display->y_pos;
  627. if ((y_pos & 1) != top_bottom)
  628. y_pos++;
  629. while (buf < buf_end) {
  630. if (x_pos > region->width || y_pos > region->height) {
  631. av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
  632. return;
  633. }
  634. switch (*buf++) {
  635. case 0x10:
  636. if (region->depth == 8)
  637. map_table = map2to8;
  638. else if (region->depth == 4)
  639. map_table = map2to4;
  640. else
  641. map_table = NULL;
  642. x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
  643. region->width - x_pos, &buf, buf_size,
  644. non_mod, map_table);
  645. break;
  646. case 0x11:
  647. if (region->depth < 4) {
  648. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  649. return;
  650. }
  651. if (region->depth == 8)
  652. map_table = map4to8;
  653. else
  654. map_table = NULL;
  655. x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
  656. region->width - x_pos, &buf, buf_size,
  657. non_mod, map_table);
  658. break;
  659. case 0x12:
  660. if (region->depth < 8) {
  661. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  662. return;
  663. }
  664. x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
  665. region->width - x_pos, &buf, buf_size,
  666. non_mod, NULL);
  667. break;
  668. case 0x20:
  669. map2to4[0] = (*buf) >> 4;
  670. map2to4[1] = (*buf++) & 0xf;
  671. map2to4[2] = (*buf) >> 4;
  672. map2to4[3] = (*buf++) & 0xf;
  673. break;
  674. case 0x21:
  675. for (i = 0; i < 4; i++)
  676. map2to8[i] = *buf++;
  677. break;
  678. case 0x22:
  679. for (i = 0; i < 16; i++)
  680. map4to8[i] = *buf++;
  681. break;
  682. case 0xf0:
  683. x_pos = display->x_pos;
  684. y_pos += 2;
  685. break;
  686. default:
  687. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  688. }
  689. }
  690. }
  691. static void dvbsub_parse_object_segment(AVCodecContext *avctx,
  692. uint8_t *buf, int buf_size)
  693. {
  694. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  695. uint8_t *buf_end = buf + buf_size;
  696. uint8_t *block;
  697. int object_id;
  698. DVBSubObject *object;
  699. DVBSubObjectDisplay *display;
  700. int top_field_len, bottom_field_len;
  701. int coding_method, non_modifying_colour;
  702. object_id = BE_16(buf);
  703. buf += 2;
  704. object = get_object(ctx, object_id);
  705. if (!object)
  706. return;
  707. coding_method = ((*buf) >> 2) & 3;
  708. non_modifying_colour = ((*buf++) >> 1) & 1;
  709. if (coding_method == 0) {
  710. top_field_len = BE_16(buf);
  711. buf += 2;
  712. bottom_field_len = BE_16(buf);
  713. buf += 2;
  714. if (buf + top_field_len + bottom_field_len > buf_end) {
  715. av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
  716. return;
  717. }
  718. for (display = object->display_list; display != 0; display = display->object_list_next) {
  719. block = buf;
  720. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  721. non_modifying_colour);
  722. if (bottom_field_len > 0)
  723. block = buf + top_field_len;
  724. else
  725. bottom_field_len = top_field_len;
  726. dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
  727. non_modifying_colour);
  728. }
  729. /* } else if (coding_method == 1) {*/
  730. } else {
  731. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  732. }
  733. }
  734. #define SCALEBITS 10
  735. #define ONE_HALF (1 << (SCALEBITS - 1))
  736. #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
  737. #define YUV_TO_RGB1_CCIR(cb1, cr1)\
  738. {\
  739. cb = (cb1) - 128;\
  740. cr = (cr1) - 128;\
  741. r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
  742. g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
  743. ONE_HALF;\
  744. b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
  745. }
  746. #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
  747. {\
  748. y = ((y1) - 16) * FIX(255.0/219.0);\
  749. r = cm[(y + r_add) >> SCALEBITS];\
  750. g = cm[(y + g_add) >> SCALEBITS];\
  751. b = cm[(y + b_add) >> SCALEBITS];\
  752. }
  753. static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
  754. uint8_t *buf, int buf_size)
  755. {
  756. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  757. uint8_t *buf_end = buf + buf_size;
  758. int clut_id;
  759. DVBSubCLUT *clut;
  760. int entry_id, depth , full_range;
  761. int y, cr, cb, alpha;
  762. int r, g, b, r_add, g_add, b_add;
  763. #ifdef DEBUG_PACKET_CONTENTS
  764. int i;
  765. av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
  766. for (i=0; i < buf_size; i++)
  767. {
  768. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  769. if (i % 16 == 15)
  770. av_log(avctx, AV_LOG_INFO, "\n");
  771. }
  772. if (i % 16 != 0)
  773. av_log(avctx, AV_LOG_INFO, "\n");
  774. #endif
  775. clut_id = *buf++;
  776. buf += 1;
  777. clut = get_clut(ctx, clut_id);
  778. if (clut == NULL) {
  779. clut = av_malloc(sizeof(DVBSubCLUT));
  780. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  781. clut->id = clut_id;
  782. clut->next = ctx->clut_list;
  783. ctx->clut_list = clut;
  784. }
  785. while (buf + 4 < buf_end)
  786. {
  787. entry_id = *buf++;
  788. depth = (*buf) & 0xe0;
  789. if (depth == 0) {
  790. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  791. return;
  792. }
  793. full_range = (*buf++) & 1;
  794. if (full_range) {
  795. y = *buf++;
  796. cr = *buf++;
  797. cb = *buf++;
  798. alpha = *buf++;
  799. } else {
  800. y = buf[0] & 0xfc;
  801. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  802. cb = (buf[1] << 2) & 0xf0;
  803. alpha = (buf[1] << 6) & 0xc0;
  804. buf += 2;
  805. }
  806. if (y == 0)
  807. alpha = 0xff;
  808. YUV_TO_RGB1_CCIR(cb, cr);
  809. YUV_TO_RGB2_CCIR(r, g, b, y);
  810. #ifdef DEBUG
  811. av_log(avctx, AV_LOG_INFO, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  812. #endif
  813. if (depth & 0x80)
  814. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  815. if (depth & 0x40)
  816. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  817. if (depth & 0x20)
  818. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  819. }
  820. }
  821. static void dvbsub_parse_region_segment(AVCodecContext *avctx,
  822. uint8_t *buf, int buf_size)
  823. {
  824. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  825. uint8_t *buf_end = buf + buf_size;
  826. int region_id, object_id;
  827. DVBSubRegion *region;
  828. DVBSubObject *object;
  829. DVBSubObjectDisplay *display;
  830. int fill;
  831. if (buf_size < 10)
  832. return;
  833. region_id = *buf++;
  834. region = get_region(ctx, region_id);
  835. if (region == NULL)
  836. {
  837. region = av_mallocz(sizeof(DVBSubRegion));
  838. region->id = region_id;
  839. region->next = ctx->region_list;
  840. ctx->region_list = region;
  841. }
  842. fill = ((*buf++) >> 3) & 1;
  843. region->width = BE_16(buf);
  844. buf += 2;
  845. region->height = BE_16(buf);
  846. buf += 2;
  847. if (region->width * region->height != region->buf_size) {
  848. if (region->pbuf != 0)
  849. av_free(region->pbuf);
  850. region->buf_size = region->width * region->height;
  851. region->pbuf = av_malloc(region->buf_size);
  852. fill = 1;
  853. }
  854. region->depth = 1 << (((*buf++) >> 2) & 7);
  855. region->clut = *buf++;
  856. if (region->depth == 8)
  857. region->bgcolour = *buf++;
  858. else {
  859. buf += 1;
  860. if (region->depth == 4)
  861. region->bgcolour = (((*buf++) >> 4) & 15);
  862. else
  863. region->bgcolour = (((*buf++) >> 2) & 3);
  864. }
  865. #ifdef DEBUG
  866. av_log(avctx, AV_LOG_INFO, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  867. #endif
  868. if (fill) {
  869. memset(region->pbuf, region->bgcolour, region->buf_size);
  870. #ifdef DEBUG
  871. av_log(avctx, AV_LOG_INFO, "Fill region (%d)\n", region->bgcolour);
  872. #endif
  873. }
  874. delete_region_display_list(ctx, region);
  875. while (buf + 5 < buf_end) {
  876. object_id = BE_16(buf);
  877. buf += 2;
  878. object = get_object(ctx, object_id);
  879. if (object == NULL) {
  880. object = av_mallocz(sizeof(DVBSubObject));
  881. object->id = object_id;
  882. object->next = ctx->object_list;
  883. ctx->object_list = object;
  884. }
  885. object->type = (*buf) >> 6;
  886. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  887. display->object_id = object_id;
  888. display->region_id = region_id;
  889. display->x_pos = BE_16(buf) & 0xfff;
  890. buf += 2;
  891. display->y_pos = BE_16(buf) & 0xfff;
  892. buf += 2;
  893. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  894. display->fgcolour = *buf++;
  895. display->bgcolour = *buf++;
  896. }
  897. display->region_list_next = region->display_list;
  898. region->display_list = display;
  899. display->object_list_next = object->display_list;
  900. object->display_list = display;
  901. }
  902. }
  903. static void dvbsub_parse_page_segment(AVCodecContext *avctx,
  904. uint8_t *buf, int buf_size)
  905. {
  906. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  907. DVBSubRegionDisplay *display;
  908. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  909. uint8_t *buf_end = buf + buf_size;
  910. int region_id;
  911. int page_state;
  912. if (buf_size < 1)
  913. return;
  914. ctx->time_out = *buf++;
  915. page_state = ((*buf++) >> 2) & 3;
  916. #ifdef DEBUG
  917. av_log(avctx, AV_LOG_INFO, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  918. #endif
  919. if (page_state == 2)
  920. {
  921. delete_state(ctx);
  922. }
  923. tmp_display_list = ctx->display_list;
  924. ctx->display_list = NULL;
  925. ctx->display_list_size = 0;
  926. while (buf + 5 < buf_end) {
  927. region_id = *buf++;
  928. buf += 1;
  929. display = tmp_display_list;
  930. tmp_ptr = &tmp_display_list;
  931. while (display != NULL && display->region_id != region_id) {
  932. tmp_ptr = &display->next;
  933. display = display->next;
  934. }
  935. if (display == NULL)
  936. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  937. display->region_id = region_id;
  938. display->x_pos = BE_16(buf);
  939. buf += 2;
  940. display->y_pos = BE_16(buf);
  941. buf += 2;
  942. *tmp_ptr = display->next;
  943. display->next = ctx->display_list;
  944. ctx->display_list = display;
  945. ctx->display_list_size++;
  946. #ifdef DEBUG
  947. av_log(avctx, AV_LOG_INFO, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  948. #endif
  949. }
  950. while (tmp_display_list != 0) {
  951. display = tmp_display_list;
  952. tmp_display_list = display->next;
  953. av_free(display);
  954. }
  955. }
  956. #ifdef DEBUG_SAVE_IMAGES
  957. static void save_display_set(DVBSubContext *ctx)
  958. {
  959. DVBSubRegion *region;
  960. DVBSubRegionDisplay *display;
  961. DVBSubCLUT *clut;
  962. uint32_t *clut_table;
  963. int x_pos, y_pos, width, height;
  964. int x, y, y_off, x_off;
  965. uint32_t *pbuf;
  966. char filename[32];
  967. static int fileno_index = 0;
  968. x_pos = -1;
  969. y_pos = -1;
  970. width = 0;
  971. height = 0;
  972. for (display = ctx->display_list; display != NULL; display = display->next) {
  973. region = get_region(ctx, display->region_id);
  974. if (x_pos == -1) {
  975. x_pos = display->x_pos;
  976. y_pos = display->y_pos;
  977. width = region->width;
  978. height = region->height;
  979. } else {
  980. if (display->x_pos < x_pos) {
  981. width += (x_pos - display->x_pos);
  982. x_pos = display->x_pos;
  983. }
  984. if (display->y_pos < y_pos) {
  985. height += (y_pos - display->y_pos);
  986. y_pos = display->y_pos;
  987. }
  988. if (display->x_pos + region->width > x_pos + width) {
  989. width = display->x_pos + region->width - x_pos;
  990. }
  991. if (display->y_pos + region->height > y_pos + height) {
  992. height = display->y_pos + region->height - y_pos;
  993. }
  994. }
  995. }
  996. if (x_pos >= 0) {
  997. pbuf = av_malloc(width * height * 4);
  998. for (display = ctx->display_list; display != NULL; display = display->next) {
  999. region = get_region(ctx, display->region_id);
  1000. x_off = display->x_pos - x_pos;
  1001. y_off = display->y_pos - y_pos;
  1002. clut = get_clut(ctx, region->clut);
  1003. if (clut == 0)
  1004. clut = &default_clut;
  1005. switch (region->depth) {
  1006. case 2:
  1007. clut_table = clut->clut4;
  1008. break;
  1009. case 8:
  1010. clut_table = clut->clut256;
  1011. break;
  1012. case 4:
  1013. default:
  1014. clut_table = clut->clut16;
  1015. break;
  1016. }
  1017. for (y = 0; y < region->height; y++) {
  1018. for (x = 0; x < region->width; x++) {
  1019. pbuf[((y + y_off) * width) + x_off + x] =
  1020. clut_table[region->pbuf[y * region->width + x]];
  1021. }
  1022. }
  1023. }
  1024. snprintf(filename, 32, "dvbs.%d", fileno_index);
  1025. png_save2(filename, pbuf, width, height);
  1026. av_free(pbuf);
  1027. }
  1028. fileno_index++;
  1029. }
  1030. #endif
  1031. static int dvbsub_display_end_segment(AVCodecContext *avctx, uint8_t *buf,
  1032. int buf_size, AVSubtitle *sub)
  1033. {
  1034. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  1035. DVBSubRegion *region;
  1036. DVBSubRegionDisplay *display;
  1037. AVSubtitleRect *rect;
  1038. DVBSubCLUT *clut;
  1039. uint32_t *clut_table;
  1040. int i;
  1041. sub->rects = NULL;
  1042. sub->start_display_time = 0;
  1043. sub->end_display_time = ctx->time_out * 1000;
  1044. sub->format = 0;
  1045. sub->num_rects = ctx->display_list_size;
  1046. if (sub->num_rects == 0)
  1047. return 0;
  1048. sub->rects = av_mallocz(sizeof(AVSubtitleRect) * sub->num_rects);
  1049. i = 0;
  1050. for (display = ctx->display_list; display != NULL; display = display->next) {
  1051. region = get_region(ctx, display->region_id);
  1052. rect = &sub->rects[i];
  1053. if (region == NULL)
  1054. continue;
  1055. rect->x = display->x_pos;
  1056. rect->y = display->y_pos;
  1057. rect->w = region->width;
  1058. rect->h = region->height;
  1059. rect->nb_colors = 16;
  1060. rect->linesize = region->width;
  1061. clut = get_clut(ctx, region->clut);
  1062. if (clut == NULL)
  1063. clut = &default_clut;
  1064. switch (region->depth) {
  1065. case 2:
  1066. clut_table = clut->clut4;
  1067. break;
  1068. case 8:
  1069. clut_table = clut->clut256;
  1070. break;
  1071. case 4:
  1072. default:
  1073. clut_table = clut->clut16;
  1074. break;
  1075. }
  1076. rect->rgba_palette = av_malloc((1 << region->depth) * sizeof(uint32_t));
  1077. memcpy(rect->rgba_palette, clut_table, (1 << region->depth) * sizeof(uint32_t));
  1078. rect->bitmap = av_malloc(region->buf_size);
  1079. memcpy(rect->bitmap, region->pbuf, region->buf_size);
  1080. i++;
  1081. }
  1082. sub->num_rects = i;
  1083. #ifdef DEBUG_SAVE_IMAGES
  1084. save_display_set(ctx);
  1085. #endif
  1086. return 1;
  1087. }
  1088. static int dvbsub_decode(AVCodecContext *avctx,
  1089. void *data, int *data_size,
  1090. uint8_t *buf, int buf_size)
  1091. {
  1092. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  1093. AVSubtitle *sub = (AVSubtitle*) data;
  1094. uint8_t *p, *p_end;
  1095. int segment_type;
  1096. int page_id;
  1097. int segment_length;
  1098. #ifdef DEBUG_PACKET_CONTENTS
  1099. int i;
  1100. av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
  1101. for (i=0; i < buf_size; i++)
  1102. {
  1103. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  1104. if (i % 16 == 15)
  1105. av_log(avctx, AV_LOG_INFO, "\n");
  1106. }
  1107. if (i % 16 != 0)
  1108. av_log(avctx, AV_LOG_INFO, "\n");
  1109. #endif
  1110. if (buf_size <= 2)
  1111. return -1;
  1112. p = buf;
  1113. p_end = buf + buf_size;
  1114. while (p < p_end && *p == 0x0f)
  1115. {
  1116. p += 1;
  1117. segment_type = *p++;
  1118. page_id = BE_16(p);
  1119. p += 2;
  1120. segment_length = BE_16(p);
  1121. p += 2;
  1122. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
  1123. switch (segment_type) {
  1124. case DVBSUB_PAGE_SEGMENT:
  1125. dvbsub_parse_page_segment(avctx, p, segment_length);
  1126. break;
  1127. case DVBSUB_REGION_SEGMENT:
  1128. dvbsub_parse_region_segment(avctx, p, segment_length);
  1129. break;
  1130. case DVBSUB_CLUT_SEGMENT:
  1131. dvbsub_parse_clut_segment(avctx, p, segment_length);
  1132. break;
  1133. case DVBSUB_OBJECT_SEGMENT:
  1134. dvbsub_parse_object_segment(avctx, p, segment_length);
  1135. break;
  1136. case DVBSUB_DISPLAY_SEGMENT:
  1137. *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
  1138. break;
  1139. default:
  1140. #ifdef DEBUG
  1141. av_log(avctx, AV_LOG_INFO, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1142. segment_type, page_id, segment_length);
  1143. #endif
  1144. break;
  1145. }
  1146. }
  1147. p += segment_length;
  1148. }
  1149. if (p != p_end)
  1150. {
  1151. #ifdef DEBUG
  1152. av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
  1153. #endif
  1154. return -1;
  1155. }
  1156. return 0;
  1157. }
  1158. AVCodec dvbsub_decoder = {
  1159. "dvbsub",
  1160. CODEC_TYPE_SUBTITLE,
  1161. CODEC_ID_DVB_SUBTITLE,
  1162. sizeof(DVBSubContext),
  1163. dvbsub_init_decoder,
  1164. NULL,
  1165. dvbsub_close_decoder,
  1166. dvbsub_decode,
  1167. };
  1168. /* Parser (mostly) copied from dvdsub.c */
  1169. #define PARSE_BUF_SIZE (65536)
  1170. /* parser definition */
  1171. typedef struct DVBSubParseContext {
  1172. uint8_t *packet_buf;
  1173. int packet_start;
  1174. int packet_index;
  1175. int in_packet;
  1176. } DVBSubParseContext;
  1177. static int dvbsub_parse_init(AVCodecParserContext *s)
  1178. {
  1179. DVBSubParseContext *pc = s->priv_data;
  1180. pc->packet_buf = av_malloc(PARSE_BUF_SIZE);
  1181. return 0;
  1182. }
  1183. static int dvbsub_parse(AVCodecParserContext *s,
  1184. AVCodecContext *avctx,
  1185. uint8_t **poutbuf, int *poutbuf_size,
  1186. const uint8_t *buf, int buf_size)
  1187. {
  1188. DVBSubParseContext *pc = s->priv_data;
  1189. uint8_t *p, *p_end;
  1190. int len, buf_pos = 0;
  1191. #ifdef DEBUG
  1192. av_log(avctx, AV_LOG_INFO, "DVB parse packet pts=%Lx, lpts=%Lx, cpts=%Lx:\n",
  1193. s->pts, s->last_pts, s->cur_frame_pts[s->cur_frame_start_index]);
  1194. #endif
  1195. #ifdef DEBUG_PACKET_CONTENTS
  1196. int i;
  1197. for (i=0; i < buf_size; i++)
  1198. {
  1199. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  1200. if (i % 16 == 15)
  1201. av_log(avctx, AV_LOG_INFO, "\n");
  1202. }
  1203. if (i % 16 != 0)
  1204. av_log(avctx, AV_LOG_INFO, "\n");
  1205. #endif
  1206. *poutbuf = NULL;
  1207. *poutbuf_size = 0;
  1208. s->fetch_timestamp = 1;
  1209. if (s->last_pts != s->pts && s->last_pts != AV_NOPTS_VALUE) /* Start of a new packet */
  1210. {
  1211. if (pc->packet_index != pc->packet_start)
  1212. {
  1213. #ifdef DEBUG
  1214. av_log(avctx, AV_LOG_INFO, "Discarding %d bytes\n",
  1215. pc->packet_index - pc->packet_start);
  1216. #endif
  1217. }
  1218. pc->packet_start = 0;
  1219. pc->packet_index = 0;
  1220. if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) {
  1221. #ifdef DEBUG
  1222. av_log(avctx, AV_LOG_INFO, "Bad packet header\n");
  1223. #endif
  1224. return -1;
  1225. }
  1226. buf_pos = 2;
  1227. pc->in_packet = 1;
  1228. } else {
  1229. if (pc->packet_start != 0)
  1230. {
  1231. if (pc->packet_index != pc->packet_start)
  1232. {
  1233. memmove(pc->packet_buf, pc->packet_buf + pc->packet_start,
  1234. pc->packet_index - pc->packet_start);
  1235. pc->packet_index -= pc->packet_start;
  1236. pc->packet_start = 0;
  1237. } else {
  1238. pc->packet_start = 0;
  1239. pc->packet_index = 0;
  1240. }
  1241. }
  1242. }
  1243. if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE)
  1244. return -1;
  1245. /* if not currently in a packet, discard data */
  1246. if (pc->in_packet == 0)
  1247. return buf_size;
  1248. memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos);
  1249. pc->packet_index += buf_size - buf_pos;
  1250. p = pc->packet_buf;
  1251. p_end = pc->packet_buf + pc->packet_index;
  1252. while (p < p_end)
  1253. {
  1254. if (*p == 0x0f)
  1255. {
  1256. if (p + 6 <= p_end)
  1257. {
  1258. len = BE_16(p + 4);
  1259. if (p + len + 6 <= p_end)
  1260. {
  1261. *poutbuf_size += len + 6;
  1262. p += len + 6;
  1263. } else
  1264. break;
  1265. } else
  1266. break;
  1267. } else if (*p == 0xff) {
  1268. if (p + 1 < p_end)
  1269. {
  1270. #ifdef DEBUG
  1271. av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
  1272. #endif
  1273. }
  1274. pc->packet_index = p - pc->packet_buf;
  1275. pc->in_packet = 0;
  1276. break;
  1277. } else {
  1278. av_log(avctx, AV_LOG_ERROR, "Junk in packet\n");
  1279. pc->packet_index = p - pc->packet_buf;
  1280. pc->in_packet = 0;
  1281. break;
  1282. }
  1283. }
  1284. if (*poutbuf_size > 0)
  1285. {
  1286. *poutbuf = pc->packet_buf;
  1287. pc->packet_start = *poutbuf_size;
  1288. }
  1289. if (s->last_pts == AV_NOPTS_VALUE)
  1290. s->last_pts = s->pts;
  1291. return buf_size;
  1292. }
  1293. static void dvbsub_parse_close(AVCodecParserContext *s)
  1294. {
  1295. DVBSubParseContext *pc = s->priv_data;
  1296. av_freep(&pc->packet_buf);
  1297. }
  1298. AVCodecParser dvbsub_parser = {
  1299. { CODEC_ID_DVB_SUBTITLE },
  1300. sizeof(DVBSubParseContext),
  1301. dvbsub_parse_init,
  1302. dvbsub_parse,
  1303. dvbsub_parse_close,
  1304. };