00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "dsputil.h"
00023 #include "get_bits.h"
00024 #include "bytestream.h"
00025 #include "libavutil/colorspace.h"
00026
00027
00028
00029
00030
00031 #define DVBSUB_PAGE_SEGMENT 0x10
00032 #define DVBSUB_REGION_SEGMENT 0x11
00033 #define DVBSUB_CLUT_SEGMENT 0x12
00034 #define DVBSUB_OBJECT_SEGMENT 0x13
00035 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
00036 #define DVBSUB_DISPLAY_SEGMENT 0x80
00037
00038 #define cm (ff_cropTbl + MAX_NEG_CROP)
00039
00040 #ifdef DEBUG_SAVE_IMAGES
00041 #undef fprintf
00042 #if 0
00043 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
00044 uint32_t *rgba_palette)
00045 {
00046 int x, y, v;
00047 FILE *f;
00048 char fname[40], fname2[40];
00049 char command[1024];
00050
00051 snprintf(fname, 40, "%s.ppm", filename);
00052
00053 f = fopen(fname, "w");
00054 if (!f) {
00055 perror(fname);
00056 exit(1);
00057 }
00058 fprintf(f, "P6\n"
00059 "%d %d\n"
00060 "%d\n",
00061 w, h, 255);
00062 for(y = 0; y < h; y++) {
00063 for(x = 0; x < w; x++) {
00064 v = rgba_palette[bitmap[y * w + x]];
00065 putc((v >> 16) & 0xff, f);
00066 putc((v >> 8) & 0xff, f);
00067 putc((v >> 0) & 0xff, f);
00068 }
00069 }
00070 fclose(f);
00071
00072
00073 snprintf(fname2, 40, "%s-a.pgm", filename);
00074
00075 f = fopen(fname2, "w");
00076 if (!f) {
00077 perror(fname2);
00078 exit(1);
00079 }
00080 fprintf(f, "P5\n"
00081 "%d %d\n"
00082 "%d\n",
00083 w, h, 255);
00084 for(y = 0; y < h; y++) {
00085 for(x = 0; x < w; x++) {
00086 v = rgba_palette[bitmap[y * w + x]];
00087 putc((v >> 24) & 0xff, f);
00088 }
00089 }
00090 fclose(f);
00091
00092 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
00093 system(command);
00094
00095 snprintf(command, 1024, "rm %s %s", fname, fname2);
00096 system(command);
00097 }
00098 #endif
00099
00100 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
00101 {
00102 int x, y, v;
00103 FILE *f;
00104 char fname[40], fname2[40];
00105 char command[1024];
00106
00107 snprintf(fname, sizeof(fname), "%s.ppm", filename);
00108
00109 f = fopen(fname, "w");
00110 if (!f) {
00111 perror(fname);
00112 exit(1);
00113 }
00114 fprintf(f, "P6\n"
00115 "%d %d\n"
00116 "%d\n",
00117 w, h, 255);
00118 for(y = 0; y < h; y++) {
00119 for(x = 0; x < w; x++) {
00120 v = bitmap[y * w + x];
00121 putc((v >> 16) & 0xff, f);
00122 putc((v >> 8) & 0xff, f);
00123 putc((v >> 0) & 0xff, f);
00124 }
00125 }
00126 fclose(f);
00127
00128
00129 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
00130
00131 f = fopen(fname2, "w");
00132 if (!f) {
00133 perror(fname2);
00134 exit(1);
00135 }
00136 fprintf(f, "P5\n"
00137 "%d %d\n"
00138 "%d\n",
00139 w, h, 255);
00140 for(y = 0; y < h; y++) {
00141 for(x = 0; x < w; x++) {
00142 v = bitmap[y * w + x];
00143 putc((v >> 24) & 0xff, f);
00144 }
00145 }
00146 fclose(f);
00147
00148 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
00149 system(command);
00150
00151 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
00152 system(command);
00153 }
00154 #endif
00155
00156 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
00157
00158 typedef struct DVBSubCLUT {
00159 int id;
00160
00161 uint32_t clut4[4];
00162 uint32_t clut16[16];
00163 uint32_t clut256[256];
00164
00165 struct DVBSubCLUT *next;
00166 } DVBSubCLUT;
00167
00168 static DVBSubCLUT default_clut;
00169
00170 typedef struct DVBSubObjectDisplay {
00171 int object_id;
00172 int region_id;
00173
00174 int x_pos;
00175 int y_pos;
00176
00177 int fgcolor;
00178 int bgcolor;
00179
00180 struct DVBSubObjectDisplay *region_list_next;
00181 struct DVBSubObjectDisplay *object_list_next;
00182 } DVBSubObjectDisplay;
00183
00184 typedef struct DVBSubObject {
00185 int id;
00186
00187 int type;
00188
00189 DVBSubObjectDisplay *display_list;
00190
00191 struct DVBSubObject *next;
00192 } DVBSubObject;
00193
00194 typedef struct DVBSubRegionDisplay {
00195 int region_id;
00196
00197 int x_pos;
00198 int y_pos;
00199
00200 struct DVBSubRegionDisplay *next;
00201 } DVBSubRegionDisplay;
00202
00203 typedef struct DVBSubRegion {
00204 int id;
00205
00206 int width;
00207 int height;
00208 int depth;
00209
00210 int clut;
00211 int bgcolor;
00212
00213 uint8_t *pbuf;
00214 int buf_size;
00215
00216 DVBSubObjectDisplay *display_list;
00217
00218 struct DVBSubRegion *next;
00219 } DVBSubRegion;
00220
00221 typedef struct DVBSubDisplayDefinition {
00222 int version;
00223
00224 int x;
00225 int y;
00226 int width;
00227 int height;
00228 } DVBSubDisplayDefinition;
00229
00230 typedef struct DVBSubContext {
00231 int composition_id;
00232 int ancillary_id;
00233
00234 int time_out;
00235 DVBSubRegion *region_list;
00236 DVBSubCLUT *clut_list;
00237 DVBSubObject *object_list;
00238
00239 int display_list_size;
00240 DVBSubRegionDisplay *display_list;
00241 DVBSubDisplayDefinition *display_definition;
00242 } DVBSubContext;
00243
00244
00245 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
00246 {
00247 DVBSubObject *ptr = ctx->object_list;
00248
00249 while (ptr && ptr->id != object_id) {
00250 ptr = ptr->next;
00251 }
00252
00253 return ptr;
00254 }
00255
00256 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
00257 {
00258 DVBSubCLUT *ptr = ctx->clut_list;
00259
00260 while (ptr && ptr->id != clut_id) {
00261 ptr = ptr->next;
00262 }
00263
00264 return ptr;
00265 }
00266
00267 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
00268 {
00269 DVBSubRegion *ptr = ctx->region_list;
00270
00271 while (ptr && ptr->id != region_id) {
00272 ptr = ptr->next;
00273 }
00274
00275 return ptr;
00276 }
00277
00278 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
00279 {
00280 DVBSubObject *object, *obj2, **obj2_ptr;
00281 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
00282
00283 while (region->display_list) {
00284 display = region->display_list;
00285
00286 object = get_object(ctx, display->object_id);
00287
00288 if (object) {
00289 obj_disp_ptr = &object->display_list;
00290 obj_disp = *obj_disp_ptr;
00291
00292 while (obj_disp && obj_disp != display) {
00293 obj_disp_ptr = &obj_disp->object_list_next;
00294 obj_disp = *obj_disp_ptr;
00295 }
00296
00297 if (obj_disp) {
00298 *obj_disp_ptr = obj_disp->object_list_next;
00299
00300 if (!object->display_list) {
00301 obj2_ptr = &ctx->object_list;
00302 obj2 = *obj2_ptr;
00303
00304 while (obj2 != object) {
00305 assert(obj2);
00306 obj2_ptr = &obj2->next;
00307 obj2 = *obj2_ptr;
00308 }
00309
00310 *obj2_ptr = obj2->next;
00311
00312 av_free(obj2);
00313 }
00314 }
00315 }
00316
00317 region->display_list = display->region_list_next;
00318
00319 av_free(display);
00320 }
00321
00322 }
00323
00324 static void delete_state(DVBSubContext *ctx)
00325 {
00326 DVBSubRegion *region;
00327 DVBSubCLUT *clut;
00328
00329 while (ctx->region_list) {
00330 region = ctx->region_list;
00331
00332 ctx->region_list = region->next;
00333
00334 delete_region_display_list(ctx, region);
00335 if (region->pbuf)
00336 av_free(region->pbuf);
00337
00338 av_free(region);
00339 }
00340
00341 while (ctx->clut_list) {
00342 clut = ctx->clut_list;
00343
00344 ctx->clut_list = clut->next;
00345
00346 av_free(clut);
00347 }
00348
00349 av_freep(&ctx->display_definition);
00350
00351
00352 if (ctx->object_list)
00353 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
00354 }
00355
00356 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
00357 {
00358 int i, r, g, b, a = 0;
00359 DVBSubContext *ctx = avctx->priv_data;
00360
00361 if (!avctx->extradata || avctx->extradata_size != 4) {
00362 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
00363 ctx->composition_id = -1;
00364 ctx->ancillary_id = -1;
00365 } else {
00366 ctx->composition_id = AV_RB16(avctx->extradata);
00367 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
00368 }
00369
00370 default_clut.id = -1;
00371 default_clut.next = NULL;
00372
00373 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
00374 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
00375 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
00376 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
00377
00378 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
00379 for (i = 1; i < 16; i++) {
00380 if (i < 8) {
00381 r = (i & 1) ? 255 : 0;
00382 g = (i & 2) ? 255 : 0;
00383 b = (i & 4) ? 255 : 0;
00384 } else {
00385 r = (i & 1) ? 127 : 0;
00386 g = (i & 2) ? 127 : 0;
00387 b = (i & 4) ? 127 : 0;
00388 }
00389 default_clut.clut16[i] = RGBA(r, g, b, 255);
00390 }
00391
00392 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
00393 for (i = 1; i < 256; i++) {
00394 if (i < 8) {
00395 r = (i & 1) ? 255 : 0;
00396 g = (i & 2) ? 255 : 0;
00397 b = (i & 4) ? 255 : 0;
00398 a = 63;
00399 } else {
00400 switch (i & 0x88) {
00401 case 0x00:
00402 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
00403 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
00404 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
00405 a = 255;
00406 break;
00407 case 0x08:
00408 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
00409 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
00410 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
00411 a = 127;
00412 break;
00413 case 0x80:
00414 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
00415 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
00416 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
00417 a = 255;
00418 break;
00419 case 0x88:
00420 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
00421 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
00422 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
00423 a = 255;
00424 break;
00425 }
00426 }
00427 default_clut.clut256[i] = RGBA(r, g, b, a);
00428 }
00429
00430 return 0;
00431 }
00432
00433 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
00434 {
00435 DVBSubContext *ctx = avctx->priv_data;
00436 DVBSubRegionDisplay *display;
00437
00438 delete_state(ctx);
00439
00440 while (ctx->display_list) {
00441 display = ctx->display_list;
00442 ctx->display_list = display->next;
00443
00444 av_free(display);
00445 }
00446
00447 return 0;
00448 }
00449
00450 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
00451 const uint8_t **srcbuf, int buf_size,
00452 int non_mod, uint8_t *map_table)
00453 {
00454 GetBitContext gb;
00455
00456 int bits;
00457 int run_length;
00458 int pixels_read = 0;
00459
00460 init_get_bits(&gb, *srcbuf, buf_size << 3);
00461
00462 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
00463 bits = get_bits(&gb, 2);
00464
00465 if (bits) {
00466 if (non_mod != 1 || bits != 1) {
00467 if (map_table)
00468 *destbuf++ = map_table[bits];
00469 else
00470 *destbuf++ = bits;
00471 }
00472 pixels_read++;
00473 } else {
00474 bits = get_bits1(&gb);
00475 if (bits == 1) {
00476 run_length = get_bits(&gb, 3) + 3;
00477 bits = get_bits(&gb, 2);
00478
00479 if (non_mod == 1 && bits == 1)
00480 pixels_read += run_length;
00481 else {
00482 if (map_table)
00483 bits = map_table[bits];
00484 while (run_length-- > 0 && pixels_read < dbuf_len) {
00485 *destbuf++ = bits;
00486 pixels_read++;
00487 }
00488 }
00489 } else {
00490 bits = get_bits1(&gb);
00491 if (bits == 0) {
00492 bits = get_bits(&gb, 2);
00493 if (bits == 2) {
00494 run_length = get_bits(&gb, 4) + 12;
00495 bits = get_bits(&gb, 2);
00496
00497 if (non_mod == 1 && bits == 1)
00498 pixels_read += run_length;
00499 else {
00500 if (map_table)
00501 bits = map_table[bits];
00502 while (run_length-- > 0 && pixels_read < dbuf_len) {
00503 *destbuf++ = bits;
00504 pixels_read++;
00505 }
00506 }
00507 } else if (bits == 3) {
00508 run_length = get_bits(&gb, 8) + 29;
00509 bits = get_bits(&gb, 2);
00510
00511 if (non_mod == 1 && bits == 1)
00512 pixels_read += run_length;
00513 else {
00514 if (map_table)
00515 bits = map_table[bits];
00516 while (run_length-- > 0 && pixels_read < dbuf_len) {
00517 *destbuf++ = bits;
00518 pixels_read++;
00519 }
00520 }
00521 } else if (bits == 1) {
00522 pixels_read += 2;
00523 if (map_table)
00524 bits = map_table[0];
00525 else
00526 bits = 0;
00527 if (pixels_read <= dbuf_len) {
00528 *destbuf++ = bits;
00529 *destbuf++ = bits;
00530 }
00531 } else {
00532 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
00533 return pixels_read;
00534 }
00535 } else {
00536 if (map_table)
00537 bits = map_table[0];
00538 else
00539 bits = 0;
00540 *destbuf++ = bits;
00541 pixels_read++;
00542 }
00543 }
00544 }
00545 }
00546
00547 if (get_bits(&gb, 6))
00548 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
00549
00550 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
00551
00552 return pixels_read;
00553 }
00554
00555 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
00556 const uint8_t **srcbuf, int buf_size,
00557 int non_mod, uint8_t *map_table)
00558 {
00559 GetBitContext gb;
00560
00561 int bits;
00562 int run_length;
00563 int pixels_read = 0;
00564
00565 init_get_bits(&gb, *srcbuf, buf_size << 3);
00566
00567 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
00568 bits = get_bits(&gb, 4);
00569
00570 if (bits) {
00571 if (non_mod != 1 || bits != 1) {
00572 if (map_table)
00573 *destbuf++ = map_table[bits];
00574 else
00575 *destbuf++ = bits;
00576 }
00577 pixels_read++;
00578 } else {
00579 bits = get_bits1(&gb);
00580 if (bits == 0) {
00581 run_length = get_bits(&gb, 3);
00582
00583 if (run_length == 0) {
00584 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
00585 return pixels_read;
00586 }
00587
00588 run_length += 2;
00589
00590 if (map_table)
00591 bits = map_table[0];
00592 else
00593 bits = 0;
00594
00595 while (run_length-- > 0 && pixels_read < dbuf_len) {
00596 *destbuf++ = bits;
00597 pixels_read++;
00598 }
00599 } else {
00600 bits = get_bits1(&gb);
00601 if (bits == 0) {
00602 run_length = get_bits(&gb, 2) + 4;
00603 bits = get_bits(&gb, 4);
00604
00605 if (non_mod == 1 && bits == 1)
00606 pixels_read += run_length;
00607 else {
00608 if (map_table)
00609 bits = map_table[bits];
00610 while (run_length-- > 0 && pixels_read < dbuf_len) {
00611 *destbuf++ = bits;
00612 pixels_read++;
00613 }
00614 }
00615 } else {
00616 bits = get_bits(&gb, 2);
00617 if (bits == 2) {
00618 run_length = get_bits(&gb, 4) + 9;
00619 bits = get_bits(&gb, 4);
00620
00621 if (non_mod == 1 && bits == 1)
00622 pixels_read += run_length;
00623 else {
00624 if (map_table)
00625 bits = map_table[bits];
00626 while (run_length-- > 0 && pixels_read < dbuf_len) {
00627 *destbuf++ = bits;
00628 pixels_read++;
00629 }
00630 }
00631 } else if (bits == 3) {
00632 run_length = get_bits(&gb, 8) + 25;
00633 bits = get_bits(&gb, 4);
00634
00635 if (non_mod == 1 && bits == 1)
00636 pixels_read += run_length;
00637 else {
00638 if (map_table)
00639 bits = map_table[bits];
00640 while (run_length-- > 0 && pixels_read < dbuf_len) {
00641 *destbuf++ = bits;
00642 pixels_read++;
00643 }
00644 }
00645 } else if (bits == 1) {
00646 pixels_read += 2;
00647 if (map_table)
00648 bits = map_table[0];
00649 else
00650 bits = 0;
00651 if (pixels_read <= dbuf_len) {
00652 *destbuf++ = bits;
00653 *destbuf++ = bits;
00654 }
00655 } else {
00656 if (map_table)
00657 bits = map_table[0];
00658 else
00659 bits = 0;
00660 *destbuf++ = bits;
00661 pixels_read ++;
00662 }
00663 }
00664 }
00665 }
00666 }
00667
00668 if (get_bits(&gb, 8))
00669 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
00670
00671 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
00672
00673 return pixels_read;
00674 }
00675
00676 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
00677 const uint8_t **srcbuf, int buf_size,
00678 int non_mod, uint8_t *map_table)
00679 {
00680 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
00681 int bits;
00682 int run_length;
00683 int pixels_read = 0;
00684
00685 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
00686 bits = *(*srcbuf)++;
00687
00688 if (bits) {
00689 if (non_mod != 1 || bits != 1) {
00690 if (map_table)
00691 *destbuf++ = map_table[bits];
00692 else
00693 *destbuf++ = bits;
00694 }
00695 pixels_read++;
00696 } else {
00697 bits = *(*srcbuf)++;
00698 run_length = bits & 0x7f;
00699 if ((bits & 0x80) == 0) {
00700 if (run_length == 0) {
00701 return pixels_read;
00702 }
00703
00704 if (map_table)
00705 bits = map_table[0];
00706 else
00707 bits = 0;
00708 while (run_length-- > 0 && pixels_read < dbuf_len) {
00709 *destbuf++ = bits;
00710 pixels_read++;
00711 }
00712 } else {
00713 bits = *(*srcbuf)++;
00714
00715 if (non_mod == 1 && bits == 1)
00716 pixels_read += run_length;
00717 if (map_table)
00718 bits = map_table[bits];
00719 else while (run_length-- > 0 && pixels_read < dbuf_len) {
00720 *destbuf++ = bits;
00721 pixels_read++;
00722 }
00723 }
00724 }
00725 }
00726
00727 if (*(*srcbuf)++)
00728 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
00729
00730 return pixels_read;
00731 }
00732
00733
00734
00735 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
00736 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
00737 {
00738 DVBSubContext *ctx = avctx->priv_data;
00739
00740 DVBSubRegion *region = get_region(ctx, display->region_id);
00741 const uint8_t *buf_end = buf + buf_size;
00742 uint8_t *pbuf;
00743 int x_pos, y_pos;
00744 int i;
00745
00746 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
00747 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
00748 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
00749 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
00750 uint8_t *map_table;
00751
00752 dprintf(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
00753 top_bottom ? "bottom" : "top");
00754
00755 #ifdef DEBUG_PACKET_CONTENTS
00756 for (i = 0; i < buf_size; i++) {
00757 if (i % 16 == 0)
00758 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
00759
00760 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
00761 if (i % 16 == 15)
00762 av_log(avctx, AV_LOG_INFO, "\n");
00763 }
00764
00765 if (i % 16)
00766 av_log(avctx, AV_LOG_INFO, "\n");
00767
00768 #endif
00769
00770 if (region == 0)
00771 return;
00772
00773 pbuf = region->pbuf;
00774
00775 x_pos = display->x_pos;
00776 y_pos = display->y_pos;
00777
00778 if ((y_pos & 1) != top_bottom)
00779 y_pos++;
00780
00781 while (buf < buf_end) {
00782 if (x_pos > region->width || y_pos > region->height) {
00783 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
00784 return;
00785 }
00786
00787 switch (*buf++) {
00788 case 0x10:
00789 if (region->depth == 8)
00790 map_table = map2to8;
00791 else if (region->depth == 4)
00792 map_table = map2to4;
00793 else
00794 map_table = NULL;
00795
00796 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
00797 region->width - x_pos, &buf, buf_size,
00798 non_mod, map_table);
00799 break;
00800 case 0x11:
00801 if (region->depth < 4) {
00802 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
00803 return;
00804 }
00805
00806 if (region->depth == 8)
00807 map_table = map4to8;
00808 else
00809 map_table = NULL;
00810
00811 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
00812 region->width - x_pos, &buf, buf_size,
00813 non_mod, map_table);
00814 break;
00815 case 0x12:
00816 if (region->depth < 8) {
00817 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
00818 return;
00819 }
00820
00821 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
00822 region->width - x_pos, &buf, buf_size,
00823 non_mod, NULL);
00824 break;
00825
00826 case 0x20:
00827 map2to4[0] = (*buf) >> 4;
00828 map2to4[1] = (*buf++) & 0xf;
00829 map2to4[2] = (*buf) >> 4;
00830 map2to4[3] = (*buf++) & 0xf;
00831 break;
00832 case 0x21:
00833 for (i = 0; i < 4; i++)
00834 map2to8[i] = *buf++;
00835 break;
00836 case 0x22:
00837 for (i = 0; i < 16; i++)
00838 map4to8[i] = *buf++;
00839 break;
00840
00841 case 0xf0:
00842 x_pos = display->x_pos;
00843 y_pos += 2;
00844 break;
00845 default:
00846 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
00847 }
00848 }
00849
00850 }
00851
00852 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
00853 const uint8_t *buf, int buf_size)
00854 {
00855 DVBSubContext *ctx = avctx->priv_data;
00856
00857 const uint8_t *buf_end = buf + buf_size;
00858 const uint8_t *block;
00859 int object_id;
00860 DVBSubObject *object;
00861 DVBSubObjectDisplay *display;
00862 int top_field_len, bottom_field_len;
00863
00864 int coding_method, non_modifying_color;
00865
00866 object_id = AV_RB16(buf);
00867 buf += 2;
00868
00869 object = get_object(ctx, object_id);
00870
00871 if (!object)
00872 return;
00873
00874 coding_method = ((*buf) >> 2) & 3;
00875 non_modifying_color = ((*buf++) >> 1) & 1;
00876
00877 if (coding_method == 0) {
00878 top_field_len = AV_RB16(buf);
00879 buf += 2;
00880 bottom_field_len = AV_RB16(buf);
00881 buf += 2;
00882
00883 if (buf + top_field_len + bottom_field_len > buf_end) {
00884 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
00885 return;
00886 }
00887
00888 for (display = object->display_list; display; display = display->object_list_next) {
00889 block = buf;
00890
00891 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
00892 non_modifying_color);
00893
00894 if (bottom_field_len > 0)
00895 block = buf + top_field_len;
00896 else
00897 bottom_field_len = top_field_len;
00898
00899 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
00900 non_modifying_color);
00901 }
00902
00903
00904
00905 } else {
00906 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
00907 }
00908
00909 }
00910
00911 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
00912 const uint8_t *buf, int buf_size)
00913 {
00914 DVBSubContext *ctx = avctx->priv_data;
00915
00916 const uint8_t *buf_end = buf + buf_size;
00917 int clut_id;
00918 DVBSubCLUT *clut;
00919 int entry_id, depth , full_range;
00920 int y, cr, cb, alpha;
00921 int r, g, b, r_add, g_add, b_add;
00922
00923 #ifdef DEBUG_PACKET_CONTENTS
00924 int i;
00925
00926 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
00927
00928 for (i=0; i < buf_size; i++) {
00929 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
00930 if (i % 16 == 15)
00931 av_log(avctx, AV_LOG_INFO, "\n");
00932 }
00933
00934 if (i % 16)
00935 av_log(avctx, AV_LOG_INFO, "\n");
00936
00937 #endif
00938
00939 clut_id = *buf++;
00940 buf += 1;
00941
00942 clut = get_clut(ctx, clut_id);
00943
00944 if (!clut) {
00945 clut = av_malloc(sizeof(DVBSubCLUT));
00946
00947 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
00948
00949 clut->id = clut_id;
00950
00951 clut->next = ctx->clut_list;
00952 ctx->clut_list = clut;
00953 }
00954
00955 while (buf + 4 < buf_end) {
00956 entry_id = *buf++;
00957
00958 depth = (*buf) & 0xe0;
00959
00960 if (depth == 0) {
00961 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
00962 return;
00963 }
00964
00965 full_range = (*buf++) & 1;
00966
00967 if (full_range) {
00968 y = *buf++;
00969 cr = *buf++;
00970 cb = *buf++;
00971 alpha = *buf++;
00972 } else {
00973 y = buf[0] & 0xfc;
00974 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
00975 cb = (buf[1] << 2) & 0xf0;
00976 alpha = (buf[1] << 6) & 0xc0;
00977
00978 buf += 2;
00979 }
00980
00981 if (y == 0)
00982 alpha = 0xff;
00983
00984 YUV_TO_RGB1_CCIR(cb, cr);
00985 YUV_TO_RGB2_CCIR(r, g, b, y);
00986
00987 dprintf(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
00988
00989 if (depth & 0x80)
00990 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
00991 if (depth & 0x40)
00992 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
00993 if (depth & 0x20)
00994 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
00995 }
00996 }
00997
00998
00999 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
01000 const uint8_t *buf, int buf_size)
01001 {
01002 DVBSubContext *ctx = avctx->priv_data;
01003
01004 const uint8_t *buf_end = buf + buf_size;
01005 int region_id, object_id;
01006 DVBSubRegion *region;
01007 DVBSubObject *object;
01008 DVBSubObjectDisplay *display;
01009 int fill;
01010
01011 if (buf_size < 10)
01012 return;
01013
01014 region_id = *buf++;
01015
01016 region = get_region(ctx, region_id);
01017
01018 if (!region) {
01019 region = av_mallocz(sizeof(DVBSubRegion));
01020
01021 region->id = region_id;
01022
01023 region->next = ctx->region_list;
01024 ctx->region_list = region;
01025 }
01026
01027 fill = ((*buf++) >> 3) & 1;
01028
01029 region->width = AV_RB16(buf);
01030 buf += 2;
01031 region->height = AV_RB16(buf);
01032 buf += 2;
01033
01034 if (region->width * region->height != region->buf_size) {
01035 if (region->pbuf)
01036 av_free(region->pbuf);
01037
01038 region->buf_size = region->width * region->height;
01039
01040 region->pbuf = av_malloc(region->buf_size);
01041
01042 fill = 1;
01043 }
01044
01045 region->depth = 1 << (((*buf++) >> 2) & 7);
01046 if(region->depth<2 || region->depth>8){
01047 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
01048 region->depth= 4;
01049 }
01050 region->clut = *buf++;
01051
01052 if (region->depth == 8)
01053 region->bgcolor = *buf++;
01054 else {
01055 buf += 1;
01056
01057 if (region->depth == 4)
01058 region->bgcolor = (((*buf++) >> 4) & 15);
01059 else
01060 region->bgcolor = (((*buf++) >> 2) & 3);
01061 }
01062
01063 dprintf(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
01064
01065 if (fill) {
01066 memset(region->pbuf, region->bgcolor, region->buf_size);
01067 dprintf(avctx, "Fill region (%d)\n", region->bgcolor);
01068 }
01069
01070 delete_region_display_list(ctx, region);
01071
01072 while (buf + 5 < buf_end) {
01073 object_id = AV_RB16(buf);
01074 buf += 2;
01075
01076 object = get_object(ctx, object_id);
01077
01078 if (!object) {
01079 object = av_mallocz(sizeof(DVBSubObject));
01080
01081 object->id = object_id;
01082 object->next = ctx->object_list;
01083 ctx->object_list = object;
01084 }
01085
01086 object->type = (*buf) >> 6;
01087
01088 display = av_mallocz(sizeof(DVBSubObjectDisplay));
01089
01090 display->object_id = object_id;
01091 display->region_id = region_id;
01092
01093 display->x_pos = AV_RB16(buf) & 0xfff;
01094 buf += 2;
01095 display->y_pos = AV_RB16(buf) & 0xfff;
01096 buf += 2;
01097
01098 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
01099 display->fgcolor = *buf++;
01100 display->bgcolor = *buf++;
01101 }
01102
01103 display->region_list_next = region->display_list;
01104 region->display_list = display;
01105
01106 display->object_list_next = object->display_list;
01107 object->display_list = display;
01108 }
01109 }
01110
01111 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
01112 const uint8_t *buf, int buf_size)
01113 {
01114 DVBSubContext *ctx = avctx->priv_data;
01115 DVBSubRegionDisplay *display;
01116 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
01117
01118 const uint8_t *buf_end = buf + buf_size;
01119 int region_id;
01120 int page_state;
01121
01122 if (buf_size < 1)
01123 return;
01124
01125 ctx->time_out = *buf++;
01126 page_state = ((*buf++) >> 2) & 3;
01127
01128 dprintf(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
01129
01130 if (page_state == 2) {
01131 delete_state(ctx);
01132 }
01133
01134 tmp_display_list = ctx->display_list;
01135 ctx->display_list = NULL;
01136 ctx->display_list_size = 0;
01137
01138 while (buf + 5 < buf_end) {
01139 region_id = *buf++;
01140 buf += 1;
01141
01142 display = tmp_display_list;
01143 tmp_ptr = &tmp_display_list;
01144
01145 while (display && display->region_id != region_id) {
01146 tmp_ptr = &display->next;
01147 display = display->next;
01148 }
01149
01150 if (!display)
01151 display = av_mallocz(sizeof(DVBSubRegionDisplay));
01152
01153 display->region_id = region_id;
01154
01155 display->x_pos = AV_RB16(buf);
01156 buf += 2;
01157 display->y_pos = AV_RB16(buf);
01158 buf += 2;
01159
01160 *tmp_ptr = display->next;
01161
01162 display->next = ctx->display_list;
01163 ctx->display_list = display;
01164 ctx->display_list_size++;
01165
01166 dprintf(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
01167 }
01168
01169 while (tmp_display_list) {
01170 display = tmp_display_list;
01171
01172 tmp_display_list = display->next;
01173
01174 av_free(display);
01175 }
01176
01177 }
01178
01179
01180 #ifdef DEBUG_SAVE_IMAGES
01181 static void save_display_set(DVBSubContext *ctx)
01182 {
01183 DVBSubRegion *region;
01184 DVBSubRegionDisplay *display;
01185 DVBSubCLUT *clut;
01186 uint32_t *clut_table;
01187 int x_pos, y_pos, width, height;
01188 int x, y, y_off, x_off;
01189 uint32_t *pbuf;
01190 char filename[32];
01191 static int fileno_index = 0;
01192
01193 x_pos = -1;
01194 y_pos = -1;
01195 width = 0;
01196 height = 0;
01197
01198 for (display = ctx->display_list; display; display = display->next) {
01199 region = get_region(ctx, display->region_id);
01200
01201 if (x_pos == -1) {
01202 x_pos = display->x_pos;
01203 y_pos = display->y_pos;
01204 width = region->width;
01205 height = region->height;
01206 } else {
01207 if (display->x_pos < x_pos) {
01208 width += (x_pos - display->x_pos);
01209 x_pos = display->x_pos;
01210 }
01211
01212 if (display->y_pos < y_pos) {
01213 height += (y_pos - display->y_pos);
01214 y_pos = display->y_pos;
01215 }
01216
01217 if (display->x_pos + region->width > x_pos + width) {
01218 width = display->x_pos + region->width - x_pos;
01219 }
01220
01221 if (display->y_pos + region->height > y_pos + height) {
01222 height = display->y_pos + region->height - y_pos;
01223 }
01224 }
01225 }
01226
01227 if (x_pos >= 0) {
01228
01229 pbuf = av_malloc(width * height * 4);
01230
01231 for (display = ctx->display_list; display; display = display->next) {
01232 region = get_region(ctx, display->region_id);
01233
01234 x_off = display->x_pos - x_pos;
01235 y_off = display->y_pos - y_pos;
01236
01237 clut = get_clut(ctx, region->clut);
01238
01239 if (clut == 0)
01240 clut = &default_clut;
01241
01242 switch (region->depth) {
01243 case 2:
01244 clut_table = clut->clut4;
01245 break;
01246 case 8:
01247 clut_table = clut->clut256;
01248 break;
01249 case 4:
01250 default:
01251 clut_table = clut->clut16;
01252 break;
01253 }
01254
01255 for (y = 0; y < region->height; y++) {
01256 for (x = 0; x < region->width; x++) {
01257 pbuf[((y + y_off) * width) + x_off + x] =
01258 clut_table[region->pbuf[y * region->width + x]];
01259 }
01260 }
01261
01262 }
01263
01264 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
01265
01266 png_save2(filename, pbuf, width, height);
01267
01268 av_free(pbuf);
01269 }
01270
01271 fileno_index++;
01272 }
01273 #endif
01274
01275 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
01276 const uint8_t *buf,
01277 int buf_size)
01278 {
01279 DVBSubContext *ctx = avctx->priv_data;
01280 DVBSubDisplayDefinition *display_def = ctx->display_definition;
01281 int dds_version, info_byte;
01282
01283 if (buf_size < 5)
01284 return;
01285
01286 info_byte = bytestream_get_byte(&buf);
01287 dds_version = info_byte >> 4;
01288 if (display_def && display_def->version == dds_version)
01289 return;
01290
01291 if (!display_def) {
01292 display_def = av_mallocz(sizeof(*display_def));
01293 ctx->display_definition = display_def;
01294 }
01295 if (!display_def)
01296 return;
01297
01298 display_def->version = dds_version;
01299 display_def->x = 0;
01300 display_def->y = 0;
01301 display_def->width = bytestream_get_be16(&buf) + 1;
01302 display_def->height = bytestream_get_be16(&buf) + 1;
01303
01304 if (buf_size < 13)
01305 return;
01306
01307 if (info_byte & 1<<3) {
01308 display_def->x = bytestream_get_be16(&buf);
01309 display_def->y = bytestream_get_be16(&buf);
01310 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
01311 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
01312 }
01313 }
01314
01315 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
01316 int buf_size, AVSubtitle *sub)
01317 {
01318 DVBSubContext *ctx = avctx->priv_data;
01319 DVBSubDisplayDefinition *display_def = ctx->display_definition;
01320
01321 DVBSubRegion *region;
01322 DVBSubRegionDisplay *display;
01323 AVSubtitleRect *rect;
01324 DVBSubCLUT *clut;
01325 uint32_t *clut_table;
01326 int i;
01327 int offset_x=0, offset_y=0;
01328
01329 sub->rects = NULL;
01330 sub->start_display_time = 0;
01331 sub->end_display_time = ctx->time_out * 1000;
01332 sub->format = 0;
01333
01334 if (display_def) {
01335 offset_x = display_def->x;
01336 offset_y = display_def->y;
01337 }
01338
01339 sub->num_rects = ctx->display_list_size;
01340
01341 if (sub->num_rects > 0){
01342 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
01343 for(i=0; i<sub->num_rects; i++)
01344 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
01345 }
01346
01347 i = 0;
01348
01349 for (display = ctx->display_list; display; display = display->next) {
01350 region = get_region(ctx, display->region_id);
01351 rect = sub->rects[i];
01352
01353 if (!region)
01354 continue;
01355
01356 rect->x = display->x_pos + offset_x;
01357 rect->y = display->y_pos + offset_y;
01358 rect->w = region->width;
01359 rect->h = region->height;
01360 rect->nb_colors = 16;
01361 rect->type = SUBTITLE_BITMAP;
01362 rect->pict.linesize[0] = region->width;
01363
01364 clut = get_clut(ctx, region->clut);
01365
01366 if (!clut)
01367 clut = &default_clut;
01368
01369 switch (region->depth) {
01370 case 2:
01371 clut_table = clut->clut4;
01372 break;
01373 case 8:
01374 clut_table = clut->clut256;
01375 break;
01376 case 4:
01377 default:
01378 clut_table = clut->clut16;
01379 break;
01380 }
01381
01382 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
01383 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
01384
01385 rect->pict.data[0] = av_malloc(region->buf_size);
01386 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
01387
01388 i++;
01389 }
01390
01391 sub->num_rects = i;
01392
01393 #ifdef DEBUG_SAVE_IMAGES
01394 save_display_set(ctx);
01395 #endif
01396
01397 return 1;
01398 }
01399
01400 static int dvbsub_decode(AVCodecContext *avctx,
01401 void *data, int *data_size,
01402 AVPacket *avpkt)
01403 {
01404 const uint8_t *buf = avpkt->data;
01405 int buf_size = avpkt->size;
01406 DVBSubContext *ctx = avctx->priv_data;
01407 AVSubtitle *sub = data;
01408 const uint8_t *p, *p_end;
01409 int segment_type;
01410 int page_id;
01411 int segment_length;
01412
01413 #ifdef DEBUG_PACKET_CONTENTS
01414 int i;
01415
01416 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
01417
01418 for (i=0; i < buf_size; i++) {
01419 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
01420 if (i % 16 == 15)
01421 av_log(avctx, AV_LOG_INFO, "\n");
01422 }
01423
01424 if (i % 16)
01425 av_log(avctx, AV_LOG_INFO, "\n");
01426
01427 #endif
01428
01429 if (buf_size <= 2)
01430 return -1;
01431
01432 p = buf;
01433 p_end = buf + buf_size;
01434
01435 while (p < p_end && *p == 0x0f) {
01436 p += 1;
01437 segment_type = *p++;
01438 page_id = AV_RB16(p);
01439 p += 2;
01440 segment_length = AV_RB16(p);
01441 p += 2;
01442
01443 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
01444 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
01445 switch (segment_type) {
01446 case DVBSUB_PAGE_SEGMENT:
01447 dvbsub_parse_page_segment(avctx, p, segment_length);
01448 break;
01449 case DVBSUB_REGION_SEGMENT:
01450 dvbsub_parse_region_segment(avctx, p, segment_length);
01451 break;
01452 case DVBSUB_CLUT_SEGMENT:
01453 dvbsub_parse_clut_segment(avctx, p, segment_length);
01454 break;
01455 case DVBSUB_OBJECT_SEGMENT:
01456 dvbsub_parse_object_segment(avctx, p, segment_length);
01457 break;
01458 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
01459 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
01460 case DVBSUB_DISPLAY_SEGMENT:
01461 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
01462 break;
01463 default:
01464 dprintf(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
01465 segment_type, page_id, segment_length);
01466 break;
01467 }
01468 }
01469
01470 p += segment_length;
01471 }
01472
01473 if (p != p_end) {
01474 dprintf(avctx, "Junk at end of packet\n");
01475 return -1;
01476 }
01477
01478 return buf_size;
01479 }
01480
01481
01482 AVCodec dvbsub_decoder = {
01483 "dvbsub",
01484 AVMEDIA_TYPE_SUBTITLE,
01485 CODEC_ID_DVB_SUBTITLE,
01486 sizeof(DVBSubContext),
01487 dvbsub_init_decoder,
01488 NULL,
01489 dvbsub_close_decoder,
01490 dvbsub_decode,
01491 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
01492 };