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
43KB

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