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 av_free(region->pbuf);
00336 av_free(region);
00337 }
00338
00339 while (ctx->clut_list) {
00340 clut = ctx->clut_list;
00341
00342 ctx->clut_list = clut->next;
00343
00344 av_free(clut);
00345 }
00346
00347 av_freep(&ctx->display_definition);
00348
00349
00350 if (ctx->object_list)
00351 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
00352 }
00353
00354 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
00355 {
00356 int i, r, g, b, a = 0;
00357 DVBSubContext *ctx = avctx->priv_data;
00358
00359 if (!avctx->extradata || avctx->extradata_size != 4) {
00360 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
00361 ctx->composition_id = -1;
00362 ctx->ancillary_id = -1;
00363 } else {
00364 ctx->composition_id = AV_RB16(avctx->extradata);
00365 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
00366 }
00367
00368 default_clut.id = -1;
00369 default_clut.next = NULL;
00370
00371 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
00372 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
00373 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
00374 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
00375
00376 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
00377 for (i = 1; i < 16; i++) {
00378 if (i < 8) {
00379 r = (i & 1) ? 255 : 0;
00380 g = (i & 2) ? 255 : 0;
00381 b = (i & 4) ? 255 : 0;
00382 } else {
00383 r = (i & 1) ? 127 : 0;
00384 g = (i & 2) ? 127 : 0;
00385 b = (i & 4) ? 127 : 0;
00386 }
00387 default_clut.clut16[i] = RGBA(r, g, b, 255);
00388 }
00389
00390 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
00391 for (i = 1; i < 256; i++) {
00392 if (i < 8) {
00393 r = (i & 1) ? 255 : 0;
00394 g = (i & 2) ? 255 : 0;
00395 b = (i & 4) ? 255 : 0;
00396 a = 63;
00397 } else {
00398 switch (i & 0x88) {
00399 case 0x00:
00400 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
00401 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
00402 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
00403 a = 255;
00404 break;
00405 case 0x08:
00406 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
00407 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
00408 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
00409 a = 127;
00410 break;
00411 case 0x80:
00412 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
00413 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
00414 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
00415 a = 255;
00416 break;
00417 case 0x88:
00418 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
00419 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
00420 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
00421 a = 255;
00422 break;
00423 }
00424 }
00425 default_clut.clut256[i] = RGBA(r, g, b, a);
00426 }
00427
00428 return 0;
00429 }
00430
00431 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
00432 {
00433 DVBSubContext *ctx = avctx->priv_data;
00434 DVBSubRegionDisplay *display;
00435
00436 delete_state(ctx);
00437
00438 while (ctx->display_list) {
00439 display = ctx->display_list;
00440 ctx->display_list = display->next;
00441
00442 av_free(display);
00443 }
00444
00445 return 0;
00446 }
00447
00448 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
00449 const uint8_t **srcbuf, int buf_size,
00450 int non_mod, uint8_t *map_table)
00451 {
00452 GetBitContext gb;
00453
00454 int bits;
00455 int run_length;
00456 int pixels_read = 0;
00457
00458 init_get_bits(&gb, *srcbuf, buf_size << 3);
00459
00460 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
00461 bits = get_bits(&gb, 2);
00462
00463 if (bits) {
00464 if (non_mod != 1 || bits != 1) {
00465 if (map_table)
00466 *destbuf++ = map_table[bits];
00467 else
00468 *destbuf++ = bits;
00469 }
00470 pixels_read++;
00471 } else {
00472 bits = get_bits1(&gb);
00473 if (bits == 1) {
00474 run_length = get_bits(&gb, 3) + 3;
00475 bits = get_bits(&gb, 2);
00476
00477 if (non_mod == 1 && bits == 1)
00478 pixels_read += run_length;
00479 else {
00480 if (map_table)
00481 bits = map_table[bits];
00482 while (run_length-- > 0 && pixels_read < dbuf_len) {
00483 *destbuf++ = bits;
00484 pixels_read++;
00485 }
00486 }
00487 } else {
00488 bits = get_bits1(&gb);
00489 if (bits == 0) {
00490 bits = get_bits(&gb, 2);
00491 if (bits == 2) {
00492 run_length = get_bits(&gb, 4) + 12;
00493 bits = get_bits(&gb, 2);
00494
00495 if (non_mod == 1 && bits == 1)
00496 pixels_read += run_length;
00497 else {
00498 if (map_table)
00499 bits = map_table[bits];
00500 while (run_length-- > 0 && pixels_read < dbuf_len) {
00501 *destbuf++ = bits;
00502 pixels_read++;
00503 }
00504 }
00505 } else if (bits == 3) {
00506 run_length = get_bits(&gb, 8) + 29;
00507 bits = get_bits(&gb, 2);
00508
00509 if (non_mod == 1 && bits == 1)
00510 pixels_read += run_length;
00511 else {
00512 if (map_table)
00513 bits = map_table[bits];
00514 while (run_length-- > 0 && pixels_read < dbuf_len) {
00515 *destbuf++ = bits;
00516 pixels_read++;
00517 }
00518 }
00519 } else if (bits == 1) {
00520 pixels_read += 2;
00521 if (map_table)
00522 bits = map_table[0];
00523 else
00524 bits = 0;
00525 if (pixels_read <= dbuf_len) {
00526 *destbuf++ = bits;
00527 *destbuf++ = bits;
00528 }
00529 } else {
00530 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
00531 return pixels_read;
00532 }
00533 } else {
00534 if (map_table)
00535 bits = map_table[0];
00536 else
00537 bits = 0;
00538 *destbuf++ = bits;
00539 pixels_read++;
00540 }
00541 }
00542 }
00543 }
00544
00545 if (get_bits(&gb, 6))
00546 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
00547
00548 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
00549
00550 return pixels_read;
00551 }
00552
00553 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
00554 const uint8_t **srcbuf, int buf_size,
00555 int non_mod, uint8_t *map_table)
00556 {
00557 GetBitContext gb;
00558
00559 int bits;
00560 int run_length;
00561 int pixels_read = 0;
00562
00563 init_get_bits(&gb, *srcbuf, buf_size << 3);
00564
00565 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
00566 bits = get_bits(&gb, 4);
00567
00568 if (bits) {
00569 if (non_mod != 1 || bits != 1) {
00570 if (map_table)
00571 *destbuf++ = map_table[bits];
00572 else
00573 *destbuf++ = bits;
00574 }
00575 pixels_read++;
00576 } else {
00577 bits = get_bits1(&gb);
00578 if (bits == 0) {
00579 run_length = get_bits(&gb, 3);
00580
00581 if (run_length == 0) {
00582 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
00583 return pixels_read;
00584 }
00585
00586 run_length += 2;
00587
00588 if (map_table)
00589 bits = map_table[0];
00590 else
00591 bits = 0;
00592
00593 while (run_length-- > 0 && pixels_read < dbuf_len) {
00594 *destbuf++ = bits;
00595 pixels_read++;
00596 }
00597 } else {
00598 bits = get_bits1(&gb);
00599 if (bits == 0) {
00600 run_length = get_bits(&gb, 2) + 4;
00601 bits = get_bits(&gb, 4);
00602
00603 if (non_mod == 1 && bits == 1)
00604 pixels_read += run_length;
00605 else {
00606 if (map_table)
00607 bits = map_table[bits];
00608 while (run_length-- > 0 && pixels_read < dbuf_len) {
00609 *destbuf++ = bits;
00610 pixels_read++;
00611 }
00612 }
00613 } else {
00614 bits = get_bits(&gb, 2);
00615 if (bits == 2) {
00616 run_length = get_bits(&gb, 4) + 9;
00617 bits = get_bits(&gb, 4);
00618
00619 if (non_mod == 1 && bits == 1)
00620 pixels_read += run_length;
00621 else {
00622 if (map_table)
00623 bits = map_table[bits];
00624 while (run_length-- > 0 && pixels_read < dbuf_len) {
00625 *destbuf++ = bits;
00626 pixels_read++;
00627 }
00628 }
00629 } else if (bits == 3) {
00630 run_length = get_bits(&gb, 8) + 25;
00631 bits = get_bits(&gb, 4);
00632
00633 if (non_mod == 1 && bits == 1)
00634 pixels_read += run_length;
00635 else {
00636 if (map_table)
00637 bits = map_table[bits];
00638 while (run_length-- > 0 && pixels_read < dbuf_len) {
00639 *destbuf++ = bits;
00640 pixels_read++;
00641 }
00642 }
00643 } else if (bits == 1) {
00644 pixels_read += 2;
00645 if (map_table)
00646 bits = map_table[0];
00647 else
00648 bits = 0;
00649 if (pixels_read <= dbuf_len) {
00650 *destbuf++ = bits;
00651 *destbuf++ = bits;
00652 }
00653 } else {
00654 if (map_table)
00655 bits = map_table[0];
00656 else
00657 bits = 0;
00658 *destbuf++ = bits;
00659 pixels_read ++;
00660 }
00661 }
00662 }
00663 }
00664 }
00665
00666 if (get_bits(&gb, 8))
00667 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
00668
00669 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
00670
00671 return pixels_read;
00672 }
00673
00674 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
00675 const uint8_t **srcbuf, int buf_size,
00676 int non_mod, uint8_t *map_table)
00677 {
00678 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
00679 int bits;
00680 int run_length;
00681 int pixels_read = 0;
00682
00683 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
00684 bits = *(*srcbuf)++;
00685
00686 if (bits) {
00687 if (non_mod != 1 || bits != 1) {
00688 if (map_table)
00689 *destbuf++ = map_table[bits];
00690 else
00691 *destbuf++ = bits;
00692 }
00693 pixels_read++;
00694 } else {
00695 bits = *(*srcbuf)++;
00696 run_length = bits & 0x7f;
00697 if ((bits & 0x80) == 0) {
00698 if (run_length == 0) {
00699 return pixels_read;
00700 }
00701
00702 if (map_table)
00703 bits = map_table[0];
00704 else
00705 bits = 0;
00706 while (run_length-- > 0 && pixels_read < dbuf_len) {
00707 *destbuf++ = bits;
00708 pixels_read++;
00709 }
00710 } else {
00711 bits = *(*srcbuf)++;
00712
00713 if (non_mod == 1 && bits == 1)
00714 pixels_read += run_length;
00715 if (map_table)
00716 bits = map_table[bits];
00717 else while (run_length-- > 0 && pixels_read < dbuf_len) {
00718 *destbuf++ = bits;
00719 pixels_read++;
00720 }
00721 }
00722 }
00723 }
00724
00725 if (*(*srcbuf)++)
00726 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
00727
00728 return pixels_read;
00729 }
00730
00731
00732
00733 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
00734 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
00735 {
00736 DVBSubContext *ctx = avctx->priv_data;
00737
00738 DVBSubRegion *region = get_region(ctx, display->region_id);
00739 const uint8_t *buf_end = buf + buf_size;
00740 uint8_t *pbuf;
00741 int x_pos, y_pos;
00742 int i;
00743
00744 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
00745 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
00746 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
00747 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
00748 uint8_t *map_table;
00749
00750 av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
00751 top_bottom ? "bottom" : "top");
00752
00753 #ifdef DEBUG_PACKET_CONTENTS
00754 for (i = 0; i < buf_size; i++) {
00755 if (i % 16 == 0)
00756 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
00757
00758 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
00759 if (i % 16 == 15)
00760 av_log(avctx, AV_LOG_INFO, "\n");
00761 }
00762
00763 if (i % 16)
00764 av_log(avctx, AV_LOG_INFO, "\n");
00765
00766 #endif
00767
00768 if (region == 0)
00769 return;
00770
00771 pbuf = region->pbuf;
00772
00773 x_pos = display->x_pos;
00774 y_pos = display->y_pos;
00775
00776 if ((y_pos & 1) != top_bottom)
00777 y_pos++;
00778
00779 while (buf < buf_end) {
00780 if (x_pos > region->width || y_pos > region->height) {
00781 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
00782 return;
00783 }
00784
00785 switch (*buf++) {
00786 case 0x10:
00787 if (region->depth == 8)
00788 map_table = map2to8;
00789 else if (region->depth == 4)
00790 map_table = map2to4;
00791 else
00792 map_table = NULL;
00793
00794 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
00795 region->width - x_pos, &buf, buf_end - buf,
00796 non_mod, map_table);
00797 break;
00798 case 0x11:
00799 if (region->depth < 4) {
00800 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
00801 return;
00802 }
00803
00804 if (region->depth == 8)
00805 map_table = map4to8;
00806 else
00807 map_table = NULL;
00808
00809 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
00810 region->width - x_pos, &buf, buf_end - buf,
00811 non_mod, map_table);
00812 break;
00813 case 0x12:
00814 if (region->depth < 8) {
00815 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
00816 return;
00817 }
00818
00819 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
00820 region->width - x_pos, &buf, buf_end - buf,
00821 non_mod, NULL);
00822 break;
00823
00824 case 0x20:
00825 map2to4[0] = (*buf) >> 4;
00826 map2to4[1] = (*buf++) & 0xf;
00827 map2to4[2] = (*buf) >> 4;
00828 map2to4[3] = (*buf++) & 0xf;
00829 break;
00830 case 0x21:
00831 for (i = 0; i < 4; i++)
00832 map2to8[i] = *buf++;
00833 break;
00834 case 0x22:
00835 for (i = 0; i < 16; i++)
00836 map4to8[i] = *buf++;
00837 break;
00838
00839 case 0xf0:
00840 x_pos = display->x_pos;
00841 y_pos += 2;
00842 break;
00843 default:
00844 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
00845 }
00846 }
00847
00848 }
00849
00850 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
00851 const uint8_t *buf, int buf_size)
00852 {
00853 DVBSubContext *ctx = avctx->priv_data;
00854
00855 const uint8_t *buf_end = buf + buf_size;
00856 const uint8_t *block;
00857 int object_id;
00858 DVBSubObject *object;
00859 DVBSubObjectDisplay *display;
00860 int top_field_len, bottom_field_len;
00861
00862 int coding_method, non_modifying_color;
00863
00864 object_id = AV_RB16(buf);
00865 buf += 2;
00866
00867 object = get_object(ctx, object_id);
00868
00869 if (!object)
00870 return;
00871
00872 coding_method = ((*buf) >> 2) & 3;
00873 non_modifying_color = ((*buf++) >> 1) & 1;
00874
00875 if (coding_method == 0) {
00876 top_field_len = AV_RB16(buf);
00877 buf += 2;
00878 bottom_field_len = AV_RB16(buf);
00879 buf += 2;
00880
00881 if (buf + top_field_len + bottom_field_len > buf_end) {
00882 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
00883 return;
00884 }
00885
00886 for (display = object->display_list; display; display = display->object_list_next) {
00887 block = buf;
00888
00889 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
00890 non_modifying_color);
00891
00892 if (bottom_field_len > 0)
00893 block = buf + top_field_len;
00894 else
00895 bottom_field_len = top_field_len;
00896
00897 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
00898 non_modifying_color);
00899 }
00900
00901
00902
00903 } else {
00904 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
00905 }
00906
00907 }
00908
00909 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
00910 const uint8_t *buf, int buf_size)
00911 {
00912 DVBSubContext *ctx = avctx->priv_data;
00913
00914 const uint8_t *buf_end = buf + buf_size;
00915 int clut_id;
00916 DVBSubCLUT *clut;
00917 int entry_id, depth , full_range;
00918 int y, cr, cb, alpha;
00919 int r, g, b, r_add, g_add, b_add;
00920
00921 #ifdef DEBUG_PACKET_CONTENTS
00922 int i;
00923
00924 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
00925
00926 for (i=0; i < buf_size; i++) {
00927 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
00928 if (i % 16 == 15)
00929 av_log(avctx, AV_LOG_INFO, "\n");
00930 }
00931
00932 if (i % 16)
00933 av_log(avctx, AV_LOG_INFO, "\n");
00934
00935 #endif
00936
00937 clut_id = *buf++;
00938 buf += 1;
00939
00940 clut = get_clut(ctx, clut_id);
00941
00942 if (!clut) {
00943 clut = av_malloc(sizeof(DVBSubCLUT));
00944
00945 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
00946
00947 clut->id = clut_id;
00948
00949 clut->next = ctx->clut_list;
00950 ctx->clut_list = clut;
00951 }
00952
00953 while (buf + 4 < buf_end) {
00954 entry_id = *buf++;
00955
00956 depth = (*buf) & 0xe0;
00957
00958 if (depth == 0) {
00959 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
00960 return;
00961 }
00962
00963 full_range = (*buf++) & 1;
00964
00965 if (full_range) {
00966 y = *buf++;
00967 cr = *buf++;
00968 cb = *buf++;
00969 alpha = *buf++;
00970 } else {
00971 y = buf[0] & 0xfc;
00972 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
00973 cb = (buf[1] << 2) & 0xf0;
00974 alpha = (buf[1] << 6) & 0xc0;
00975
00976 buf += 2;
00977 }
00978
00979 if (y == 0)
00980 alpha = 0xff;
00981
00982 YUV_TO_RGB1_CCIR(cb, cr);
00983 YUV_TO_RGB2_CCIR(r, g, b, y);
00984
00985 av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
00986
00987 if (depth & 0x80)
00988 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
00989 if (depth & 0x40)
00990 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
00991 if (depth & 0x20)
00992 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
00993 }
00994 }
00995
00996
00997 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
00998 const uint8_t *buf, int buf_size)
00999 {
01000 DVBSubContext *ctx = avctx->priv_data;
01001
01002 const uint8_t *buf_end = buf + buf_size;
01003 int region_id, object_id;
01004 DVBSubRegion *region;
01005 DVBSubObject *object;
01006 DVBSubObjectDisplay *display;
01007 int fill;
01008
01009 if (buf_size < 10)
01010 return;
01011
01012 region_id = *buf++;
01013
01014 region = get_region(ctx, region_id);
01015
01016 if (!region) {
01017 region = av_mallocz(sizeof(DVBSubRegion));
01018
01019 region->id = region_id;
01020
01021 region->next = ctx->region_list;
01022 ctx->region_list = region;
01023 }
01024
01025 fill = ((*buf++) >> 3) & 1;
01026
01027 region->width = AV_RB16(buf);
01028 buf += 2;
01029 region->height = AV_RB16(buf);
01030 buf += 2;
01031
01032 if (region->width * region->height != region->buf_size) {
01033 av_free(region->pbuf);
01034
01035 region->buf_size = region->width * region->height;
01036
01037 region->pbuf = av_malloc(region->buf_size);
01038
01039 fill = 1;
01040 }
01041
01042 region->depth = 1 << (((*buf++) >> 2) & 7);
01043 if(region->depth<2 || region->depth>8){
01044 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
01045 region->depth= 4;
01046 }
01047 region->clut = *buf++;
01048
01049 if (region->depth == 8)
01050 region->bgcolor = *buf++;
01051 else {
01052 buf += 1;
01053
01054 if (region->depth == 4)
01055 region->bgcolor = (((*buf++) >> 4) & 15);
01056 else
01057 region->bgcolor = (((*buf++) >> 2) & 3);
01058 }
01059
01060 av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
01061
01062 if (fill) {
01063 memset(region->pbuf, region->bgcolor, region->buf_size);
01064 av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
01065 }
01066
01067 delete_region_display_list(ctx, region);
01068
01069 while (buf + 5 < buf_end) {
01070 object_id = AV_RB16(buf);
01071 buf += 2;
01072
01073 object = get_object(ctx, object_id);
01074
01075 if (!object) {
01076 object = av_mallocz(sizeof(DVBSubObject));
01077
01078 object->id = object_id;
01079 object->next = ctx->object_list;
01080 ctx->object_list = object;
01081 }
01082
01083 object->type = (*buf) >> 6;
01084
01085 display = av_mallocz(sizeof(DVBSubObjectDisplay));
01086
01087 display->object_id = object_id;
01088 display->region_id = region_id;
01089
01090 display->x_pos = AV_RB16(buf) & 0xfff;
01091 buf += 2;
01092 display->y_pos = AV_RB16(buf) & 0xfff;
01093 buf += 2;
01094
01095 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
01096 display->fgcolor = *buf++;
01097 display->bgcolor = *buf++;
01098 }
01099
01100 display->region_list_next = region->display_list;
01101 region->display_list = display;
01102
01103 display->object_list_next = object->display_list;
01104 object->display_list = display;
01105 }
01106 }
01107
01108 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
01109 const uint8_t *buf, int buf_size)
01110 {
01111 DVBSubContext *ctx = avctx->priv_data;
01112 DVBSubRegionDisplay *display;
01113 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
01114
01115 const uint8_t *buf_end = buf + buf_size;
01116 int region_id;
01117 int page_state;
01118
01119 if (buf_size < 1)
01120 return;
01121
01122 ctx->time_out = *buf++;
01123 page_state = ((*buf++) >> 2) & 3;
01124
01125 av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
01126
01127 if (page_state == 2) {
01128 delete_state(ctx);
01129 }
01130
01131 tmp_display_list = ctx->display_list;
01132 ctx->display_list = NULL;
01133 ctx->display_list_size = 0;
01134
01135 while (buf + 5 < buf_end) {
01136 region_id = *buf++;
01137 buf += 1;
01138
01139 display = tmp_display_list;
01140 tmp_ptr = &tmp_display_list;
01141
01142 while (display && display->region_id != region_id) {
01143 tmp_ptr = &display->next;
01144 display = display->next;
01145 }
01146
01147 if (!display)
01148 display = av_mallocz(sizeof(DVBSubRegionDisplay));
01149
01150 display->region_id = region_id;
01151
01152 display->x_pos = AV_RB16(buf);
01153 buf += 2;
01154 display->y_pos = AV_RB16(buf);
01155 buf += 2;
01156
01157 *tmp_ptr = display->next;
01158
01159 display->next = ctx->display_list;
01160 ctx->display_list = display;
01161 ctx->display_list_size++;
01162
01163 av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
01164 }
01165
01166 while (tmp_display_list) {
01167 display = tmp_display_list;
01168
01169 tmp_display_list = display->next;
01170
01171 av_free(display);
01172 }
01173
01174 }
01175
01176
01177 #ifdef DEBUG_SAVE_IMAGES
01178 static void save_display_set(DVBSubContext *ctx)
01179 {
01180 DVBSubRegion *region;
01181 DVBSubRegionDisplay *display;
01182 DVBSubCLUT *clut;
01183 uint32_t *clut_table;
01184 int x_pos, y_pos, width, height;
01185 int x, y, y_off, x_off;
01186 uint32_t *pbuf;
01187 char filename[32];
01188 static int fileno_index = 0;
01189
01190 x_pos = -1;
01191 y_pos = -1;
01192 width = 0;
01193 height = 0;
01194
01195 for (display = ctx->display_list; display; display = display->next) {
01196 region = get_region(ctx, display->region_id);
01197
01198 if (x_pos == -1) {
01199 x_pos = display->x_pos;
01200 y_pos = display->y_pos;
01201 width = region->width;
01202 height = region->height;
01203 } else {
01204 if (display->x_pos < x_pos) {
01205 width += (x_pos - display->x_pos);
01206 x_pos = display->x_pos;
01207 }
01208
01209 if (display->y_pos < y_pos) {
01210 height += (y_pos - display->y_pos);
01211 y_pos = display->y_pos;
01212 }
01213
01214 if (display->x_pos + region->width > x_pos + width) {
01215 width = display->x_pos + region->width - x_pos;
01216 }
01217
01218 if (display->y_pos + region->height > y_pos + height) {
01219 height = display->y_pos + region->height - y_pos;
01220 }
01221 }
01222 }
01223
01224 if (x_pos >= 0) {
01225
01226 pbuf = av_malloc(width * height * 4);
01227
01228 for (display = ctx->display_list; display; display = display->next) {
01229 region = get_region(ctx, display->region_id);
01230
01231 x_off = display->x_pos - x_pos;
01232 y_off = display->y_pos - y_pos;
01233
01234 clut = get_clut(ctx, region->clut);
01235
01236 if (clut == 0)
01237 clut = &default_clut;
01238
01239 switch (region->depth) {
01240 case 2:
01241 clut_table = clut->clut4;
01242 break;
01243 case 8:
01244 clut_table = clut->clut256;
01245 break;
01246 case 4:
01247 default:
01248 clut_table = clut->clut16;
01249 break;
01250 }
01251
01252 for (y = 0; y < region->height; y++) {
01253 for (x = 0; x < region->width; x++) {
01254 pbuf[((y + y_off) * width) + x_off + x] =
01255 clut_table[region->pbuf[y * region->width + x]];
01256 }
01257 }
01258
01259 }
01260
01261 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
01262
01263 png_save2(filename, pbuf, width, height);
01264
01265 av_free(pbuf);
01266 }
01267
01268 fileno_index++;
01269 }
01270 #endif
01271
01272 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
01273 const uint8_t *buf,
01274 int buf_size)
01275 {
01276 DVBSubContext *ctx = avctx->priv_data;
01277 DVBSubDisplayDefinition *display_def = ctx->display_definition;
01278 int dds_version, info_byte;
01279
01280 if (buf_size < 5)
01281 return;
01282
01283 info_byte = bytestream_get_byte(&buf);
01284 dds_version = info_byte >> 4;
01285 if (display_def && display_def->version == dds_version)
01286 return;
01287
01288 if (!display_def) {
01289 display_def = av_mallocz(sizeof(*display_def));
01290 ctx->display_definition = display_def;
01291 }
01292 if (!display_def)
01293 return;
01294
01295 display_def->version = dds_version;
01296 display_def->x = 0;
01297 display_def->y = 0;
01298 display_def->width = bytestream_get_be16(&buf) + 1;
01299 display_def->height = bytestream_get_be16(&buf) + 1;
01300
01301 if (buf_size < 13)
01302 return;
01303
01304 if (info_byte & 1<<3) {
01305 display_def->x = bytestream_get_be16(&buf);
01306 display_def->y = bytestream_get_be16(&buf);
01307 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
01308 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
01309 }
01310 }
01311
01312 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
01313 int buf_size, AVSubtitle *sub)
01314 {
01315 DVBSubContext *ctx = avctx->priv_data;
01316 DVBSubDisplayDefinition *display_def = ctx->display_definition;
01317
01318 DVBSubRegion *region;
01319 DVBSubRegionDisplay *display;
01320 AVSubtitleRect *rect;
01321 DVBSubCLUT *clut;
01322 uint32_t *clut_table;
01323 int i;
01324 int offset_x=0, offset_y=0;
01325
01326 sub->rects = NULL;
01327 sub->start_display_time = 0;
01328 sub->end_display_time = ctx->time_out * 1000;
01329 sub->format = 0;
01330
01331 if (display_def) {
01332 offset_x = display_def->x;
01333 offset_y = display_def->y;
01334 }
01335
01336 sub->num_rects = ctx->display_list_size;
01337
01338 if (sub->num_rects > 0){
01339 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
01340 for(i=0; i<sub->num_rects; i++)
01341 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
01342 }
01343
01344 i = 0;
01345
01346 for (display = ctx->display_list; display; display = display->next) {
01347 region = get_region(ctx, display->region_id);
01348 rect = sub->rects[i];
01349
01350 if (!region)
01351 continue;
01352
01353 rect->x = display->x_pos + offset_x;
01354 rect->y = display->y_pos + offset_y;
01355 rect->w = region->width;
01356 rect->h = region->height;
01357 rect->nb_colors = 16;
01358 rect->type = SUBTITLE_BITMAP;
01359 rect->pict.linesize[0] = region->width;
01360
01361 clut = get_clut(ctx, region->clut);
01362
01363 if (!clut)
01364 clut = &default_clut;
01365
01366 switch (region->depth) {
01367 case 2:
01368 clut_table = clut->clut4;
01369 break;
01370 case 8:
01371 clut_table = clut->clut256;
01372 break;
01373 case 4:
01374 default:
01375 clut_table = clut->clut16;
01376 break;
01377 }
01378
01379 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
01380 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
01381
01382 rect->pict.data[0] = av_malloc(region->buf_size);
01383 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
01384
01385 i++;
01386 }
01387
01388 sub->num_rects = i;
01389
01390 #ifdef DEBUG_SAVE_IMAGES
01391 save_display_set(ctx);
01392 #endif
01393
01394 return 1;
01395 }
01396
01397 static int dvbsub_decode(AVCodecContext *avctx,
01398 void *data, int *data_size,
01399 AVPacket *avpkt)
01400 {
01401 const uint8_t *buf = avpkt->data;
01402 int buf_size = avpkt->size;
01403 DVBSubContext *ctx = avctx->priv_data;
01404 AVSubtitle *sub = data;
01405 const uint8_t *p, *p_end;
01406 int segment_type;
01407 int page_id;
01408 int segment_length;
01409
01410 #ifdef DEBUG_PACKET_CONTENTS
01411 int i;
01412
01413 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
01414
01415 for (i=0; i < buf_size; i++) {
01416 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
01417 if (i % 16 == 15)
01418 av_log(avctx, AV_LOG_INFO, "\n");
01419 }
01420
01421 if (i % 16)
01422 av_log(avctx, AV_LOG_INFO, "\n");
01423
01424 #endif
01425
01426 if (buf_size <= 6 || *buf != 0x0f) {
01427 av_dlog(avctx, "incomplete or broken packet");
01428 return -1;
01429 }
01430
01431 p = buf;
01432 p_end = buf + buf_size;
01433
01434 while (p_end - p >= 6 && *p == 0x0f) {
01435 p += 1;
01436 segment_type = *p++;
01437 page_id = AV_RB16(p);
01438 p += 2;
01439 segment_length = AV_RB16(p);
01440 p += 2;
01441
01442 if (p_end - p < segment_length) {
01443 av_dlog(avctx, "incomplete or broken packet");
01444 return -1;
01445 }
01446
01447 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
01448 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
01449 switch (segment_type) {
01450 case DVBSUB_PAGE_SEGMENT:
01451 dvbsub_parse_page_segment(avctx, p, segment_length);
01452 break;
01453 case DVBSUB_REGION_SEGMENT:
01454 dvbsub_parse_region_segment(avctx, p, segment_length);
01455 break;
01456 case DVBSUB_CLUT_SEGMENT:
01457 dvbsub_parse_clut_segment(avctx, p, segment_length);
01458 break;
01459 case DVBSUB_OBJECT_SEGMENT:
01460 dvbsub_parse_object_segment(avctx, p, segment_length);
01461 break;
01462 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
01463 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
01464 case DVBSUB_DISPLAY_SEGMENT:
01465 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
01466 break;
01467 default:
01468 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
01469 segment_type, page_id, segment_length);
01470 break;
01471 }
01472 }
01473
01474 p += segment_length;
01475 }
01476
01477 return p - buf;
01478 }
01479
01480
01481 AVCodec ff_dvbsub_decoder = {
01482 "dvbsub",
01483 AVMEDIA_TYPE_SUBTITLE,
01484 CODEC_ID_DVB_SUBTITLE,
01485 sizeof(DVBSubContext),
01486 dvbsub_init_decoder,
01487 NULL,
01488 dvbsub_close_decoder,
01489 dvbsub_decode,
01490 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
01491 };