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