dvbsubdec.c 46 KB

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