dvbsubdec.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. #include "colorspace.h"
  25. //#define DEBUG
  26. //#define DEBUG_PACKET_CONTENTS
  27. //#define DEBUG_SAVE_IMAGES
  28. #define DVBSUB_PAGE_SEGMENT 0x10
  29. #define DVBSUB_REGION_SEGMENT 0x11
  30. #define DVBSUB_CLUT_SEGMENT 0x12
  31. #define DVBSUB_OBJECT_SEGMENT 0x13
  32. #define DVBSUB_DISPLAY_SEGMENT 0x80
  33. #define cm (ff_cropTbl + MAX_NEG_CROP)
  34. #ifdef DEBUG_SAVE_IMAGES
  35. #undef fprintf
  36. #if 0
  37. static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
  38. uint32_t *rgba_palette)
  39. {
  40. int x, y, v;
  41. FILE *f;
  42. char fname[40], fname2[40];
  43. char command[1024];
  44. snprintf(fname, 40, "%s.ppm", filename);
  45. f = fopen(fname, "w");
  46. if (!f) {
  47. perror(fname);
  48. exit(1);
  49. }
  50. fprintf(f, "P6\n"
  51. "%d %d\n"
  52. "%d\n",
  53. w, h, 255);
  54. for(y = 0; y < h; y++) {
  55. for(x = 0; x < w; x++) {
  56. v = rgba_palette[bitmap[y * w + x]];
  57. putc((v >> 16) & 0xff, f);
  58. putc((v >> 8) & 0xff, f);
  59. putc((v >> 0) & 0xff, f);
  60. }
  61. }
  62. fclose(f);
  63. snprintf(fname2, 40, "%s-a.pgm", filename);
  64. f = fopen(fname2, "w");
  65. if (!f) {
  66. perror(fname2);
  67. exit(1);
  68. }
  69. fprintf(f, "P5\n"
  70. "%d %d\n"
  71. "%d\n",
  72. w, h, 255);
  73. for(y = 0; y < h; y++) {
  74. for(x = 0; x < w; x++) {
  75. v = rgba_palette[bitmap[y * w + x]];
  76. putc((v >> 24) & 0xff, f);
  77. }
  78. }
  79. fclose(f);
  80. snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
  81. system(command);
  82. snprintf(command, 1024, "rm %s %s", fname, fname2);
  83. system(command);
  84. }
  85. #endif
  86. static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
  87. {
  88. int x, y, v;
  89. FILE *f;
  90. char fname[40], fname2[40];
  91. char command[1024];
  92. snprintf(fname, sizeof(fname), "%s.ppm", filename);
  93. f = fopen(fname, "w");
  94. if (!f) {
  95. perror(fname);
  96. exit(1);
  97. }
  98. fprintf(f, "P6\n"
  99. "%d %d\n"
  100. "%d\n",
  101. w, h, 255);
  102. for(y = 0; y < h; y++) {
  103. for(x = 0; x < w; x++) {
  104. v = bitmap[y * w + x];
  105. putc((v >> 16) & 0xff, f);
  106. putc((v >> 8) & 0xff, f);
  107. putc((v >> 0) & 0xff, f);
  108. }
  109. }
  110. fclose(f);
  111. snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
  112. f = fopen(fname2, "w");
  113. if (!f) {
  114. perror(fname2);
  115. exit(1);
  116. }
  117. fprintf(f, "P5\n"
  118. "%d %d\n"
  119. "%d\n",
  120. w, h, 255);
  121. for(y = 0; y < h; y++) {
  122. for(x = 0; x < w; x++) {
  123. v = bitmap[y * w + x];
  124. putc((v >> 24) & 0xff, f);
  125. }
  126. }
  127. fclose(f);
  128. snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
  129. system(command);
  130. snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
  131. system(command);
  132. }
  133. #endif
  134. #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
  135. typedef struct DVBSubCLUT {
  136. int id;
  137. uint32_t clut4[4];
  138. uint32_t clut16[16];
  139. uint32_t clut256[256];
  140. struct DVBSubCLUT *next;
  141. } DVBSubCLUT;
  142. static DVBSubCLUT default_clut;
  143. typedef struct DVBSubObjectDisplay {
  144. int object_id;
  145. int region_id;
  146. int x_pos;
  147. int y_pos;
  148. int fgcolor;
  149. int bgcolor;
  150. struct DVBSubObjectDisplay *region_list_next;
  151. struct DVBSubObjectDisplay *object_list_next;
  152. } DVBSubObjectDisplay;
  153. typedef struct DVBSubObject {
  154. int id;
  155. int type;
  156. DVBSubObjectDisplay *display_list;
  157. struct DVBSubObject *next;
  158. } DVBSubObject;
  159. typedef struct DVBSubRegionDisplay {
  160. int region_id;
  161. int x_pos;
  162. int y_pos;
  163. struct DVBSubRegionDisplay *next;
  164. } DVBSubRegionDisplay;
  165. typedef struct DVBSubRegion {
  166. int id;
  167. int width;
  168. int height;
  169. int depth;
  170. int clut;
  171. int bgcolor;
  172. uint8_t *pbuf;
  173. int buf_size;
  174. DVBSubObjectDisplay *display_list;
  175. struct DVBSubRegion *next;
  176. } DVBSubRegion;
  177. typedef struct DVBSubContext {
  178. int composition_id;
  179. int ancillary_id;
  180. int time_out;
  181. DVBSubRegion *region_list;
  182. DVBSubCLUT *clut_list;
  183. DVBSubObject *object_list;
  184. int display_list_size;
  185. DVBSubRegionDisplay *display_list;
  186. } DVBSubContext;
  187. static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
  188. {
  189. DVBSubObject *ptr = ctx->object_list;
  190. while (ptr && ptr->id != object_id) {
  191. ptr = ptr->next;
  192. }
  193. return ptr;
  194. }
  195. static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
  196. {
  197. DVBSubCLUT *ptr = ctx->clut_list;
  198. while (ptr && ptr->id != clut_id) {
  199. ptr = ptr->next;
  200. }
  201. return ptr;
  202. }
  203. static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
  204. {
  205. DVBSubRegion *ptr = ctx->region_list;
  206. while (ptr && ptr->id != region_id) {
  207. ptr = ptr->next;
  208. }
  209. return ptr;
  210. }
  211. static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
  212. {
  213. DVBSubObject *object, *obj2, **obj2_ptr;
  214. DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
  215. while (region->display_list) {
  216. display = region->display_list;
  217. object = get_object(ctx, display->object_id);
  218. if (object) {
  219. obj_disp_ptr = &object->display_list;
  220. obj_disp = *obj_disp_ptr;
  221. while (obj_disp && obj_disp != display) {
  222. obj_disp_ptr = &obj_disp->object_list_next;
  223. obj_disp = *obj_disp_ptr;
  224. }
  225. if (obj_disp) {
  226. *obj_disp_ptr = obj_disp->object_list_next;
  227. if (!object->display_list) {
  228. obj2_ptr = &ctx->object_list;
  229. obj2 = *obj2_ptr;
  230. while (obj2 != object) {
  231. assert(obj2);
  232. obj2_ptr = &obj2->next;
  233. obj2 = *obj2_ptr;
  234. }
  235. *obj2_ptr = obj2->next;
  236. av_free(obj2);
  237. }
  238. }
  239. }
  240. region->display_list = display->region_list_next;
  241. av_free(display);
  242. }
  243. }
  244. static void delete_state(DVBSubContext *ctx)
  245. {
  246. DVBSubRegion *region;
  247. DVBSubCLUT *clut;
  248. while (ctx->region_list) {
  249. region = ctx->region_list;
  250. ctx->region_list = region->next;
  251. delete_region_display_list(ctx, region);
  252. if (region->pbuf)
  253. av_free(region->pbuf);
  254. av_free(region);
  255. }
  256. while (ctx->clut_list) {
  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)
  263. av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
  264. }
  265. static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
  266. {
  267. int i, r, g, b, a = 0;
  268. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  269. memset(avctx->priv_data, 0, sizeof(DVBSubContext));
  270. ctx->composition_id = avctx->sub_id & 0xffff;
  271. ctx->ancillary_id = avctx->sub_id >> 16;
  272. default_clut.id = -1;
  273. default_clut.next = NULL;
  274. default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
  275. default_clut.clut4[1] = RGBA(255, 255, 255, 255);
  276. default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
  277. default_clut.clut4[3] = RGBA(127, 127, 127, 255);
  278. default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
  279. for (i = 1; i < 16; i++) {
  280. if (i < 8) {
  281. r = (i & 1) ? 255 : 0;
  282. g = (i & 2) ? 255 : 0;
  283. b = (i & 4) ? 255 : 0;
  284. } else {
  285. r = (i & 1) ? 127 : 0;
  286. g = (i & 2) ? 127 : 0;
  287. b = (i & 4) ? 127 : 0;
  288. }
  289. default_clut.clut16[i] = RGBA(r, g, b, 255);
  290. }
  291. default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
  292. for (i = 1; i < 256; i++) {
  293. if (i < 8) {
  294. r = (i & 1) ? 255 : 0;
  295. g = (i & 2) ? 255 : 0;
  296. b = (i & 4) ? 255 : 0;
  297. a = 63;
  298. } else {
  299. switch (i & 0x88) {
  300. case 0x00:
  301. r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
  302. g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
  303. b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
  304. a = 255;
  305. break;
  306. case 0x08:
  307. r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
  308. g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
  309. b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
  310. a = 127;
  311. break;
  312. case 0x80:
  313. r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
  314. g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
  315. b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
  316. a = 255;
  317. break;
  318. case 0x88:
  319. r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
  320. g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
  321. b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
  322. a = 255;
  323. break;
  324. }
  325. }
  326. default_clut.clut256[i] = RGBA(r, g, b, a);
  327. }
  328. return 0;
  329. }
  330. static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
  331. {
  332. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  333. DVBSubRegionDisplay *display;
  334. delete_state(ctx);
  335. while (ctx->display_list) {
  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. const 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) {
  354. if (non_mod != 1 || bits != 1) {
  355. if (map_table)
  356. *destbuf++ = map_table[bits];
  357. else
  358. *destbuf++ = bits;
  359. }
  360. pixels_read++;
  361. } else {
  362. bits = get_bits1(&gb);
  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)
  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_bits1(&gb);
  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)
  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)
  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)
  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)
  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))
  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. const 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) {
  448. if (non_mod != 1 || bits != 1) {
  449. if (map_table)
  450. *destbuf++ = map_table[bits];
  451. else
  452. *destbuf++ = bits;
  453. }
  454. pixels_read++;
  455. } else {
  456. bits = get_bits1(&gb);
  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)
  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_bits1(&gb);
  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)
  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)
  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)
  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)
  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)
  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))
  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. const uint8_t **srcbuf, int buf_size,
  544. int non_mod, uint8_t *map_table)
  545. {
  546. const 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) {
  553. if (non_mod != 1 || bits != 1) {
  554. if (map_table)
  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)
  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)
  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)++)
  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. const 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. const 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. if (i % 16 == 0)
  613. av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
  614. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  615. if (i % 16 == 15)
  616. av_log(avctx, AV_LOG_INFO, "\n");
  617. }
  618. if (i % 16)
  619. av_log(avctx, AV_LOG_INFO, "\n");
  620. #endif
  621. if (region == 0)
  622. return;
  623. pbuf = region->pbuf;
  624. x_pos = display->x_pos;
  625. y_pos = display->y_pos;
  626. if ((y_pos & 1) != top_bottom)
  627. y_pos++;
  628. while (buf < buf_end) {
  629. if (x_pos > region->width || y_pos > region->height) {
  630. av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
  631. return;
  632. }
  633. switch (*buf++) {
  634. case 0x10:
  635. if (region->depth == 8)
  636. map_table = map2to8;
  637. else if (region->depth == 4)
  638. map_table = map2to4;
  639. else
  640. map_table = NULL;
  641. x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
  642. region->width - x_pos, &buf, buf_size,
  643. non_mod, map_table);
  644. break;
  645. case 0x11:
  646. if (region->depth < 4) {
  647. av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
  648. return;
  649. }
  650. if (region->depth == 8)
  651. map_table = map4to8;
  652. else
  653. map_table = NULL;
  654. x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
  655. region->width - x_pos, &buf, buf_size,
  656. non_mod, map_table);
  657. break;
  658. case 0x12:
  659. if (region->depth < 8) {
  660. av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
  661. return;
  662. }
  663. x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
  664. region->width - x_pos, &buf, buf_size,
  665. non_mod, NULL);
  666. break;
  667. case 0x20:
  668. map2to4[0] = (*buf) >> 4;
  669. map2to4[1] = (*buf++) & 0xf;
  670. map2to4[2] = (*buf) >> 4;
  671. map2to4[3] = (*buf++) & 0xf;
  672. break;
  673. case 0x21:
  674. for (i = 0; i < 4; i++)
  675. map2to8[i] = *buf++;
  676. break;
  677. case 0x22:
  678. for (i = 0; i < 16; i++)
  679. map4to8[i] = *buf++;
  680. break;
  681. case 0xf0:
  682. x_pos = display->x_pos;
  683. y_pos += 2;
  684. break;
  685. default:
  686. av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
  687. }
  688. }
  689. }
  690. static void dvbsub_parse_object_segment(AVCodecContext *avctx,
  691. const uint8_t *buf, int buf_size)
  692. {
  693. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  694. const uint8_t *buf_end = buf + buf_size;
  695. const uint8_t *block;
  696. int object_id;
  697. DVBSubObject *object;
  698. DVBSubObjectDisplay *display;
  699. int top_field_len, bottom_field_len;
  700. int coding_method, non_modifying_color;
  701. object_id = AV_RB16(buf);
  702. buf += 2;
  703. object = get_object(ctx, object_id);
  704. if (!object)
  705. return;
  706. coding_method = ((*buf) >> 2) & 3;
  707. non_modifying_color = ((*buf++) >> 1) & 1;
  708. if (coding_method == 0) {
  709. top_field_len = AV_RB16(buf);
  710. buf += 2;
  711. bottom_field_len = AV_RB16(buf);
  712. buf += 2;
  713. if (buf + top_field_len + bottom_field_len > buf_end) {
  714. av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
  715. return;
  716. }
  717. for (display = object->display_list; display; display = display->object_list_next) {
  718. block = buf;
  719. dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
  720. non_modifying_color);
  721. if (bottom_field_len > 0)
  722. block = buf + top_field_len;
  723. else
  724. bottom_field_len = top_field_len;
  725. dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
  726. non_modifying_color);
  727. }
  728. /* } else if (coding_method == 1) {*/
  729. } else {
  730. av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
  731. }
  732. }
  733. static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
  734. const uint8_t *buf, int buf_size)
  735. {
  736. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  737. const uint8_t *buf_end = buf + buf_size;
  738. int clut_id;
  739. DVBSubCLUT *clut;
  740. int entry_id, depth , full_range;
  741. int y, cr, cb, alpha;
  742. int r, g, b, r_add, g_add, b_add;
  743. #ifdef DEBUG_PACKET_CONTENTS
  744. int i;
  745. av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
  746. for (i=0; i < buf_size; i++) {
  747. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  748. if (i % 16 == 15)
  749. av_log(avctx, AV_LOG_INFO, "\n");
  750. }
  751. if (i % 16)
  752. av_log(avctx, AV_LOG_INFO, "\n");
  753. #endif
  754. clut_id = *buf++;
  755. buf += 1;
  756. clut = get_clut(ctx, clut_id);
  757. if (!clut) {
  758. clut = av_malloc(sizeof(DVBSubCLUT));
  759. memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
  760. clut->id = clut_id;
  761. clut->next = ctx->clut_list;
  762. ctx->clut_list = clut;
  763. }
  764. while (buf + 4 < buf_end) {
  765. entry_id = *buf++;
  766. depth = (*buf) & 0xe0;
  767. if (depth == 0) {
  768. av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
  769. return;
  770. }
  771. full_range = (*buf++) & 1;
  772. if (full_range) {
  773. y = *buf++;
  774. cr = *buf++;
  775. cb = *buf++;
  776. alpha = *buf++;
  777. } else {
  778. y = buf[0] & 0xfc;
  779. cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
  780. cb = (buf[1] << 2) & 0xf0;
  781. alpha = (buf[1] << 6) & 0xc0;
  782. buf += 2;
  783. }
  784. if (y == 0)
  785. alpha = 0xff;
  786. YUV_TO_RGB1_CCIR(cb, cr);
  787. YUV_TO_RGB2_CCIR(r, g, b, y);
  788. #ifdef DEBUG
  789. av_log(avctx, AV_LOG_INFO, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
  790. #endif
  791. if (depth & 0x80)
  792. clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
  793. if (depth & 0x40)
  794. clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
  795. if (depth & 0x20)
  796. clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
  797. }
  798. }
  799. static void dvbsub_parse_region_segment(AVCodecContext *avctx,
  800. const uint8_t *buf, int buf_size)
  801. {
  802. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  803. const uint8_t *buf_end = buf + buf_size;
  804. int region_id, object_id;
  805. DVBSubRegion *region;
  806. DVBSubObject *object;
  807. DVBSubObjectDisplay *display;
  808. int fill;
  809. if (buf_size < 10)
  810. return;
  811. region_id = *buf++;
  812. region = get_region(ctx, region_id);
  813. if (!region) {
  814. region = av_mallocz(sizeof(DVBSubRegion));
  815. region->id = region_id;
  816. region->next = ctx->region_list;
  817. ctx->region_list = region;
  818. }
  819. fill = ((*buf++) >> 3) & 1;
  820. region->width = AV_RB16(buf);
  821. buf += 2;
  822. region->height = AV_RB16(buf);
  823. buf += 2;
  824. if (region->width * region->height != region->buf_size) {
  825. if (region->pbuf)
  826. av_free(region->pbuf);
  827. region->buf_size = region->width * region->height;
  828. region->pbuf = av_malloc(region->buf_size);
  829. fill = 1;
  830. }
  831. region->depth = 1 << (((*buf++) >> 2) & 7);
  832. if(region->depth<2 || region->depth>8){
  833. av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
  834. region->depth= 4;
  835. }
  836. region->clut = *buf++;
  837. if (region->depth == 8)
  838. region->bgcolor = *buf++;
  839. else {
  840. buf += 1;
  841. if (region->depth == 4)
  842. region->bgcolor = (((*buf++) >> 4) & 15);
  843. else
  844. region->bgcolor = (((*buf++) >> 2) & 3);
  845. }
  846. #ifdef DEBUG
  847. av_log(avctx, AV_LOG_INFO, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
  848. #endif
  849. if (fill) {
  850. memset(region->pbuf, region->bgcolor, region->buf_size);
  851. #ifdef DEBUG
  852. av_log(avctx, AV_LOG_INFO, "Fill region (%d)\n", region->bgcolor);
  853. #endif
  854. }
  855. delete_region_display_list(ctx, region);
  856. while (buf + 5 < buf_end) {
  857. object_id = AV_RB16(buf);
  858. buf += 2;
  859. object = get_object(ctx, object_id);
  860. if (!object) {
  861. object = av_mallocz(sizeof(DVBSubObject));
  862. object->id = object_id;
  863. object->next = ctx->object_list;
  864. ctx->object_list = object;
  865. }
  866. object->type = (*buf) >> 6;
  867. display = av_mallocz(sizeof(DVBSubObjectDisplay));
  868. display->object_id = object_id;
  869. display->region_id = region_id;
  870. display->x_pos = AV_RB16(buf) & 0xfff;
  871. buf += 2;
  872. display->y_pos = AV_RB16(buf) & 0xfff;
  873. buf += 2;
  874. if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
  875. display->fgcolor = *buf++;
  876. display->bgcolor = *buf++;
  877. }
  878. display->region_list_next = region->display_list;
  879. region->display_list = display;
  880. display->object_list_next = object->display_list;
  881. object->display_list = display;
  882. }
  883. }
  884. static void dvbsub_parse_page_segment(AVCodecContext *avctx,
  885. const uint8_t *buf, int buf_size)
  886. {
  887. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  888. DVBSubRegionDisplay *display;
  889. DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
  890. const uint8_t *buf_end = buf + buf_size;
  891. int region_id;
  892. int page_state;
  893. if (buf_size < 1)
  894. return;
  895. ctx->time_out = *buf++;
  896. page_state = ((*buf++) >> 2) & 3;
  897. #ifdef DEBUG
  898. av_log(avctx, AV_LOG_INFO, "Page time out %ds, state %d\n", ctx->time_out, page_state);
  899. #endif
  900. if (page_state == 2) {
  901. delete_state(ctx);
  902. }
  903. tmp_display_list = ctx->display_list;
  904. ctx->display_list = NULL;
  905. ctx->display_list_size = 0;
  906. while (buf + 5 < buf_end) {
  907. region_id = *buf++;
  908. buf += 1;
  909. display = tmp_display_list;
  910. tmp_ptr = &tmp_display_list;
  911. while (display && display->region_id != region_id) {
  912. tmp_ptr = &display->next;
  913. display = display->next;
  914. }
  915. if (!display)
  916. display = av_mallocz(sizeof(DVBSubRegionDisplay));
  917. display->region_id = region_id;
  918. display->x_pos = AV_RB16(buf);
  919. buf += 2;
  920. display->y_pos = AV_RB16(buf);
  921. buf += 2;
  922. *tmp_ptr = display->next;
  923. display->next = ctx->display_list;
  924. ctx->display_list = display;
  925. ctx->display_list_size++;
  926. #ifdef DEBUG
  927. av_log(avctx, AV_LOG_INFO, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
  928. #endif
  929. }
  930. while (tmp_display_list) {
  931. display = tmp_display_list;
  932. tmp_display_list = display->next;
  933. av_free(display);
  934. }
  935. }
  936. #ifdef DEBUG_SAVE_IMAGES
  937. static void save_display_set(DVBSubContext *ctx)
  938. {
  939. DVBSubRegion *region;
  940. DVBSubRegionDisplay *display;
  941. DVBSubCLUT *clut;
  942. uint32_t *clut_table;
  943. int x_pos, y_pos, width, height;
  944. int x, y, y_off, x_off;
  945. uint32_t *pbuf;
  946. char filename[32];
  947. static int fileno_index = 0;
  948. x_pos = -1;
  949. y_pos = -1;
  950. width = 0;
  951. height = 0;
  952. for (display = ctx->display_list; display; display = display->next) {
  953. region = get_region(ctx, display->region_id);
  954. if (x_pos == -1) {
  955. x_pos = display->x_pos;
  956. y_pos = display->y_pos;
  957. width = region->width;
  958. height = region->height;
  959. } else {
  960. if (display->x_pos < x_pos) {
  961. width += (x_pos - display->x_pos);
  962. x_pos = display->x_pos;
  963. }
  964. if (display->y_pos < y_pos) {
  965. height += (y_pos - display->y_pos);
  966. y_pos = display->y_pos;
  967. }
  968. if (display->x_pos + region->width > x_pos + width) {
  969. width = display->x_pos + region->width - x_pos;
  970. }
  971. if (display->y_pos + region->height > y_pos + height) {
  972. height = display->y_pos + region->height - y_pos;
  973. }
  974. }
  975. }
  976. if (x_pos >= 0) {
  977. pbuf = av_malloc(width * height * 4);
  978. for (display = ctx->display_list; display; display = display->next) {
  979. region = get_region(ctx, display->region_id);
  980. x_off = display->x_pos - x_pos;
  981. y_off = display->y_pos - y_pos;
  982. clut = get_clut(ctx, region->clut);
  983. if (clut == 0)
  984. clut = &default_clut;
  985. switch (region->depth) {
  986. case 2:
  987. clut_table = clut->clut4;
  988. break;
  989. case 8:
  990. clut_table = clut->clut256;
  991. break;
  992. case 4:
  993. default:
  994. clut_table = clut->clut16;
  995. break;
  996. }
  997. for (y = 0; y < region->height; y++) {
  998. for (x = 0; x < region->width; x++) {
  999. pbuf[((y + y_off) * width) + x_off + x] =
  1000. clut_table[region->pbuf[y * region->width + x]];
  1001. }
  1002. }
  1003. }
  1004. snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
  1005. png_save2(filename, pbuf, width, height);
  1006. av_free(pbuf);
  1007. }
  1008. fileno_index++;
  1009. }
  1010. #endif
  1011. static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
  1012. int buf_size, AVSubtitle *sub)
  1013. {
  1014. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  1015. DVBSubRegion *region;
  1016. DVBSubRegionDisplay *display;
  1017. AVSubtitleRect *rect;
  1018. DVBSubCLUT *clut;
  1019. uint32_t *clut_table;
  1020. int i;
  1021. sub->rects = NULL;
  1022. sub->start_display_time = 0;
  1023. sub->end_display_time = ctx->time_out * 1000;
  1024. sub->format = 0;
  1025. sub->num_rects = ctx->display_list_size;
  1026. if (sub->num_rects > 0){
  1027. sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
  1028. for(i=0; i<sub->num_rects; i++)
  1029. sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
  1030. }
  1031. i = 0;
  1032. for (display = ctx->display_list; display; display = display->next) {
  1033. region = get_region(ctx, display->region_id);
  1034. rect = sub->rects[i];
  1035. if (!region)
  1036. continue;
  1037. rect->x = display->x_pos;
  1038. rect->y = display->y_pos;
  1039. rect->w = region->width;
  1040. rect->h = region->height;
  1041. rect->nb_colors = 16;
  1042. rect->pict.linesize[0] = region->width;
  1043. clut = get_clut(ctx, region->clut);
  1044. if (!clut)
  1045. clut = &default_clut;
  1046. switch (region->depth) {
  1047. case 2:
  1048. clut_table = clut->clut4;
  1049. break;
  1050. case 8:
  1051. clut_table = clut->clut256;
  1052. break;
  1053. case 4:
  1054. default:
  1055. clut_table = clut->clut16;
  1056. break;
  1057. }
  1058. rect->pict.data[1] = av_malloc((1 << region->depth) * sizeof(uint32_t));
  1059. memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
  1060. rect->pict.data[0] = av_malloc(region->buf_size);
  1061. memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
  1062. i++;
  1063. }
  1064. sub->num_rects = i;
  1065. #ifdef DEBUG_SAVE_IMAGES
  1066. save_display_set(ctx);
  1067. #endif
  1068. return 1;
  1069. }
  1070. static int dvbsub_decode(AVCodecContext *avctx,
  1071. void *data, int *data_size,
  1072. const uint8_t *buf, int buf_size)
  1073. {
  1074. DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
  1075. AVSubtitle *sub = (AVSubtitle*) data;
  1076. const uint8_t *p, *p_end;
  1077. int segment_type;
  1078. int page_id;
  1079. int segment_length;
  1080. #ifdef DEBUG_PACKET_CONTENTS
  1081. int i;
  1082. av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
  1083. for (i=0; i < buf_size; i++) {
  1084. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  1085. if (i % 16 == 15)
  1086. av_log(avctx, AV_LOG_INFO, "\n");
  1087. }
  1088. if (i % 16)
  1089. av_log(avctx, AV_LOG_INFO, "\n");
  1090. #endif
  1091. if (buf_size <= 2)
  1092. return -1;
  1093. p = buf;
  1094. p_end = buf + buf_size;
  1095. while (p < p_end && *p == 0x0f) {
  1096. p += 1;
  1097. segment_type = *p++;
  1098. page_id = AV_RB16(p);
  1099. p += 2;
  1100. segment_length = AV_RB16(p);
  1101. p += 2;
  1102. if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
  1103. switch (segment_type) {
  1104. case DVBSUB_PAGE_SEGMENT:
  1105. dvbsub_parse_page_segment(avctx, p, segment_length);
  1106. break;
  1107. case DVBSUB_REGION_SEGMENT:
  1108. dvbsub_parse_region_segment(avctx, p, segment_length);
  1109. break;
  1110. case DVBSUB_CLUT_SEGMENT:
  1111. dvbsub_parse_clut_segment(avctx, p, segment_length);
  1112. break;
  1113. case DVBSUB_OBJECT_SEGMENT:
  1114. dvbsub_parse_object_segment(avctx, p, segment_length);
  1115. break;
  1116. case DVBSUB_DISPLAY_SEGMENT:
  1117. *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
  1118. break;
  1119. default:
  1120. #ifdef DEBUG
  1121. av_log(avctx, AV_LOG_INFO, "Subtitling segment type 0x%x, page id %d, length %d\n",
  1122. segment_type, page_id, segment_length);
  1123. #endif
  1124. break;
  1125. }
  1126. }
  1127. p += segment_length;
  1128. }
  1129. if (p != p_end) {
  1130. #ifdef DEBUG
  1131. av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
  1132. #endif
  1133. return -1;
  1134. }
  1135. return buf_size;
  1136. }
  1137. AVCodec dvbsub_decoder = {
  1138. "dvbsub",
  1139. CODEC_TYPE_SUBTITLE,
  1140. CODEC_ID_DVB_SUBTITLE,
  1141. sizeof(DVBSubContext),
  1142. dvbsub_init_decoder,
  1143. NULL,
  1144. dvbsub_close_decoder,
  1145. dvbsub_decode,
  1146. .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
  1147. };