1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
|
/*****************************************************************
* gmerlin - a general purpose multimedia framework and applications
*
* Copyright (c) 2001 - 2010 Members of the Gmerlin project
* gmerlin-general@lists.sourceforge.net
* http://gmerlin.sourceforge.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <config.h>
#include <string.h>
#include <gmerlin/pluginregistry.h>
#include <gmerlin/encoder.h>
#include <gmerlin/utils.h>
#include <gmerlin/log.h>
#define LOG_DOMAIN "encoder"
/* Encoder flags */
typedef struct
{
int in_index;
int out_index;
bg_encoder_plugin_t * plugin;
void * priv;
gavl_audio_format_t format;
bg_cfg_section_t * section;
const bg_parameter_info_t * parameters;
char language[4];
} audio_stream_t;
typedef struct
{
int in_index;
int out_index;
bg_encoder_plugin_t * plugin;
void * priv;
gavl_video_format_t format;
bg_cfg_section_t * section;
const bg_parameter_info_t * parameters;
int pass;
int total_passes;
char * stats_file;
// int64_t last_timestamp;
} video_stream_t;
typedef struct
{
int in_index;
int out_index;
bg_encoder_plugin_t * plugin;
void * priv;
int timescale;
bg_cfg_section_t * section;
const bg_parameter_info_t * parameters;
char language[4];
} subtitle_text_stream_t;
typedef struct
{
int in_index;
int out_index;
bg_encoder_plugin_t * plugin;
void * priv;
gavl_video_format_t format;
bg_cfg_section_t * section;
const bg_parameter_info_t * parameters;
char language[4];
} subtitle_overlay_stream_t;
typedef struct
{
const bg_plugin_info_t * info;
bg_cfg_section_t * section;
} plugin_config_t;
typedef struct
{
bg_cfg_section_t * section;
} stream_config_t;
struct bg_encoder_s
{
plugin_config_t audio_plugin;
plugin_config_t video_plugin;
plugin_config_t subtitle_text_plugin;
plugin_config_t subtitle_overlay_plugin;
stream_config_t audio_stream;
stream_config_t video_stream;
stream_config_t subtitle_text_stream;
stream_config_t subtitle_overlay_stream;
int num_audio_streams;
int num_video_streams;
int num_subtitle_text_streams;
int num_subtitle_overlay_streams;
int total_streams;
audio_stream_t * audio_streams;
video_stream_t * video_streams;
subtitle_text_stream_t * subtitle_text_streams;
subtitle_overlay_stream_t * subtitle_overlay_streams;
int num_plugins;
bg_plugin_handle_t ** plugins;
int separate;
bg_plugin_registry_t * plugin_reg;
/* Config stuff */
bg_cfg_section_t * es;
bg_transcoder_track_t * tt;
int stream_mask;
bg_encoder_callbacks_t * cb_ext;
bg_encoder_callbacks_t cb_int;
char * filename_base;
const bg_metadata_t * metadata;
const bg_chapter_list_t * chapter_list;
};
static int cb_create_output_file(void * data, const char * filename)
{
int ret;
bg_encoder_t * e = data;
if(e->cb_ext && e->cb_ext->create_output_file)
ret = e->cb_ext->create_output_file(e->cb_ext->data, filename);
else
ret = 1;
if(ret)
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Created output file %s",
filename);
return ret;
}
static int cb_create_temp_file(void * data, const char * filename)
{
int ret;
bg_encoder_t * e = data;
if(e->cb_ext && e->cb_ext->create_temp_file)
ret = e->cb_ext->create_temp_file(e->cb_ext->data, filename);
else
ret = 1;
if(ret)
bg_log(BG_LOG_INFO, LOG_DOMAIN, "Created temp file %s\n",
filename);
return ret;
}
static void init_plugin_from_section(bg_encoder_t * e, plugin_config_t * ret,
bg_stream_type_t type)
{
const char * name;
name = bg_encoder_section_get_plugin(e->plugin_reg, e->es,
type, e->stream_mask);
if(name)
{
ret->info = bg_plugin_find_by_name(e->plugin_reg, name);
bg_encoder_section_get_plugin_config(e->plugin_reg,
e->es, type, e->stream_mask,
&ret->section, NULL);
}
}
static void init_stream_from_section(bg_encoder_t * e,
stream_config_t * ret,
bg_stream_type_t type)
{
bg_encoder_section_get_stream_config(e->plugin_reg, e->es,
type, e->stream_mask,
&ret->section, NULL);
}
static void init_from_section(bg_encoder_t * e)
{
if(e->stream_mask & BG_STREAM_AUDIO)
{
init_plugin_from_section(e, &e->audio_plugin, BG_STREAM_AUDIO);
init_stream_from_section(e, &e->audio_stream, BG_STREAM_AUDIO);
}
if(e->stream_mask & BG_STREAM_SUBTITLE_TEXT)
{
init_plugin_from_section(e, &e->subtitle_text_plugin,
BG_STREAM_SUBTITLE_TEXT);
init_stream_from_section(e, &e->subtitle_text_stream,
BG_STREAM_SUBTITLE_TEXT);
}
if(e->stream_mask & BG_STREAM_SUBTITLE_OVERLAY)
{
init_plugin_from_section(e, &e->subtitle_overlay_plugin,
BG_STREAM_SUBTITLE_OVERLAY);
init_stream_from_section(e, &e->subtitle_overlay_stream,
BG_STREAM_SUBTITLE_OVERLAY);
}
if(e->stream_mask & BG_STREAM_VIDEO)
{
init_plugin_from_section(e, &e->video_plugin, BG_STREAM_VIDEO);
init_stream_from_section(e, &e->video_stream, BG_STREAM_VIDEO);
}
}
static void init_from_tt(bg_encoder_t * e)
{
const char * plugin_name;
/* Video plugin (must come first) */
plugin_name = bg_transcoder_track_get_video_encoder(e->tt);
if(plugin_name)
{
e->video_plugin.info = bg_plugin_find_by_name(e->plugin_reg, plugin_name);
e->video_plugin.section = e->tt->video_encoder_section;
}
/* Audio plugin */
plugin_name = bg_transcoder_track_get_audio_encoder(e->tt);
if(plugin_name)
{
e->audio_plugin.info = bg_plugin_find_by_name(e->plugin_reg, plugin_name);
e->audio_plugin.section = e->tt->audio_encoder_section;
}
/* Subtitle text plugin */
plugin_name = bg_transcoder_track_get_subtitle_text_encoder(e->tt);
if(plugin_name)
{
e->subtitle_text_plugin.info = bg_plugin_find_by_name(e->plugin_reg, plugin_name);
e->subtitle_text_plugin.section = e->tt->subtitle_text_encoder_section;
}
/* Subtitle overlay plugin */
plugin_name = bg_transcoder_track_get_subtitle_overlay_encoder(e->tt);
if(plugin_name)
{
e->subtitle_overlay_plugin.info = bg_plugin_find_by_name(e->plugin_reg, plugin_name);
e->subtitle_overlay_plugin.section = e->tt->subtitle_overlay_encoder_section;
}
}
bg_encoder_t * bg_encoder_create(bg_plugin_registry_t * plugin_reg,
bg_cfg_section_t * es,
bg_transcoder_track_t * tt,
int stream_mask, int flag_mask)
{
bg_encoder_t * ret = calloc(1, sizeof(*ret));
ret->plugin_reg = plugin_reg;
ret->stream_mask = stream_mask;
ret->cb_int.create_output_file = cb_create_output_file;
ret->cb_int.create_temp_file = cb_create_temp_file;
ret->cb_int.data = ret;
/* Set plugin infos */
if(es)
{
ret->es = es;
init_from_section(ret);
}
else if(tt)
{
ret->tt = tt;
init_from_tt(ret);
}
return ret;
}
void
bg_encoder_set_callbacks(bg_encoder_t * e, bg_encoder_callbacks_t * cb)
{
e->cb_ext = cb;
}
void bg_encoder_destroy(bg_encoder_t * enc, int do_delete)
{
int i;
/* Close all encoder plugins */
for(i = 0; i < enc->num_plugins; i++)
{
bg_encoder_plugin_t * encoder =
(bg_encoder_plugin_t *)enc->plugins[i]->plugin;
encoder->close(enc->plugins[i]->priv, do_delete);
bg_plugin_unref(enc->plugins[i]);
}
if(enc->plugins)
free(enc->plugins);
if(enc->filename_base)
free(enc->filename_base);
free(enc);
}
int bg_encoder_open(bg_encoder_t * enc, const char * filename_base,
const bg_metadata_t * metadata,
const bg_chapter_list_t * chapter_list)
{
enc->filename_base = bg_strdup(enc->filename_base, filename_base);
enc->metadata = metadata;
enc->chapter_list = chapter_list;
return 1;
}
static bg_plugin_handle_t * load_encoder(bg_encoder_t * enc,
const bg_plugin_info_t * info,
bg_cfg_section_t * section,
const char * filename_base)
{
bg_plugin_handle_t * ret;
bg_encoder_plugin_t * plugin;
enc->plugins = realloc(enc->plugins,
(enc->num_plugins+1)* sizeof(enc->plugins));
enc->plugins[enc->num_plugins] =
bg_plugin_load(enc->plugin_reg, info);
ret = enc->plugins[enc->num_plugins];
plugin = (bg_encoder_plugin_t *)ret->plugin;
if(plugin->set_callbacks)
plugin->set_callbacks(ret->priv, &enc->cb_int);
if(plugin->common.set_parameter)
bg_cfg_section_apply(section,
info->parameters,
plugin->common.set_parameter,
ret->priv);
if(!plugin->open(ret->priv, filename_base, enc->metadata, enc->chapter_list))
{
bg_plugin_unref(ret);
return NULL;
}
enc->num_plugins++;
return ret;
}
static void check_separate(bg_encoder_t * enc)
{
/* Check for separate audio streams */
if(enc->num_audio_streams)
{
if((enc->audio_plugin.info) ||
((enc->video_plugin.info->max_audio_streams > 0) &&
(enc->num_audio_streams > enc->video_plugin.info->max_audio_streams)))
enc->separate |= BG_STREAM_AUDIO;
}
/* Check for separate subtitle text streams */
if(enc->num_subtitle_text_streams)
{
if((enc->subtitle_text_plugin.info) ||
((enc->video_plugin.info->max_subtitle_text_streams > 0) &&
(enc->num_subtitle_text_streams > enc->video_plugin.info->max_subtitle_text_streams)))
enc->separate |= BG_STREAM_SUBTITLE_TEXT;
}
/* Check for separate subtitle overlay streams */
if(enc->num_subtitle_overlay_streams)
{
if((enc->subtitle_overlay_plugin.info) ||
((enc->video_plugin.info->max_subtitle_overlay_streams > 0) &&
(enc->num_subtitle_overlay_streams >
enc->video_plugin.info->max_subtitle_overlay_streams)))
enc->separate |= BG_STREAM_SUBTITLE_OVERLAY;
}
/* Check for separate video streams */
if(enc->num_video_streams)
{
if((enc->video_plugin.info->max_video_streams > 0) &&
(enc->num_video_streams >
enc->video_plugin.info->max_video_streams))
enc->separate |= BG_STREAM_VIDEO;
}
/* If video is separate, all other streams get separate as well */
if(enc->separate & BG_STREAM_VIDEO)
{
enc->separate |= BG_STREAM_SUBTITLE_OVERLAY |
BG_STREAM_SUBTITLE_TEXT |
BG_STREAM_AUDIO;
}
else
{
/* Under some conditions, we want the video to be marked as separate as well */
if(((enc->separate & BG_STREAM_SUBTITLE_OVERLAY) || !enc->num_subtitle_overlay_streams) &&
((enc->separate & BG_STREAM_SUBTITLE_TEXT) || !enc->num_subtitle_text_streams) &&
((enc->separate & BG_STREAM_AUDIO) || !enc->num_audio_streams))
enc->separate |= BG_STREAM_VIDEO;
}
}
typedef struct
{
void (*func)(void * data, int index, const char * name,
const bg_parameter_value_t*val);
void * data;
int index;
} set_stream_param_struct_t;
static void set_stream_param(void * priv, const char * name,
const bg_parameter_value_t * val)
{
set_stream_param_struct_t * s;
s = (set_stream_param_struct_t *)priv;
s->func(s->data, s->index, name, val);
}
static bg_plugin_handle_t * get_stream_handle(bg_encoder_t * enc,
bg_stream_type_t type,
int stream, int in_index)
{
bg_plugin_handle_t * ret;
const bg_plugin_info_t * info = NULL;
bg_cfg_section_t * section = NULL;
char * filename_base;
const char * type_string = NULL;
if(enc->separate & type)
{
switch(type)
{
case BG_STREAM_AUDIO:
type_string = "audio";
info = enc->audio_plugin.info;
section = enc->audio_plugin.section;
break;
case BG_STREAM_VIDEO:
type_string = "video";
info = enc->video_plugin.info;
section = enc->video_plugin.section;
break;
case BG_STREAM_SUBTITLE_TEXT:
type_string = "subtext";
info = enc->subtitle_text_plugin.info;
section = enc->subtitle_text_plugin.section;
break;
case BG_STREAM_SUBTITLE_OVERLAY:
type_string = "subovl";
info = enc->subtitle_overlay_plugin.info;
section = enc->subtitle_overlay_plugin.section;
break;
}
if(enc->total_streams > 1)
{
filename_base = bg_sprintf("%s_%s_%02d", enc->filename_base, type_string, in_index+1);
ret = load_encoder(enc, info, section, filename_base);
free(filename_base);
}
else
ret = load_encoder(enc, info, section, enc->filename_base);
}
else
{
if(enc->num_plugins)
ret = enc->plugins[0];
else
ret = load_encoder(enc, enc->video_plugin.info, enc->video_plugin.section,
enc->filename_base);
}
return ret;
}
static int start_audio(bg_encoder_t * enc, int stream)
{
audio_stream_t * s;
bg_plugin_handle_t * h;
set_stream_param_struct_t st;
s = &enc->audio_streams[stream];
/* Get handle */
h = get_stream_handle(enc, BG_STREAM_AUDIO, stream, s->in_index);
if(!h)
return 0;
s->plugin = (bg_encoder_plugin_t*)h->plugin;
s->priv = h->priv;
/* Add stream */
s->out_index = s->plugin->add_audio_stream(s->priv, s->language, &s->format);
/* Apply parameters */
if(s->plugin->set_audio_parameter)
{
st.func = s->plugin->set_audio_parameter;
st.data = s->priv;
st.index = s->out_index;
bg_cfg_section_apply(s->section,
s->parameters,
set_stream_param,
&st);
}
return 1;
}
static int start_video(bg_encoder_t * enc, int stream)
{
video_stream_t * s;
bg_plugin_handle_t * h;
set_stream_param_struct_t st;
s = &enc->video_streams[stream];
/* Get handle */
h = get_stream_handle(enc, BG_STREAM_VIDEO, stream, s->in_index);
if(!h)
return 0;
s->plugin = (bg_encoder_plugin_t*)h->plugin;
s->priv = h->priv;
/* Add stream */
s->out_index = s->plugin->add_video_stream(s->priv, &s->format);
/* Apply parameters */
if(s->plugin->set_video_parameter)
{
st.func = s->plugin->set_video_parameter;
st.data = s->priv;
st.index = s->out_index;
bg_cfg_section_apply(s->section,
s->parameters,
set_stream_param,
&st);
}
/* Set pass */
if(s->total_passes)
{
if(!s->plugin->set_video_pass ||
!s->plugin->set_video_pass(s->priv, s->out_index, s->pass, s->total_passes,
s->stats_file))
{
bg_log(BG_LOG_ERROR, LOG_DOMAIN,
"Multipass encoding not supported by encoder plugin");
return 0;
}
}
return 1;
}
static int start_subtitle_text(bg_encoder_t * enc, int stream)
{
subtitle_text_stream_t * s;
bg_plugin_handle_t * h;
set_stream_param_struct_t st;
s = &enc->subtitle_text_streams[stream];
/* Get handle */
h = get_stream_handle(enc, BG_STREAM_SUBTITLE_TEXT, stream, s->in_index);
if(!h)
return 0;
s->plugin = (bg_encoder_plugin_t*)h->plugin;
s->priv = h->priv;
/* Add stream */
s->out_index = s->plugin->add_subtitle_text_stream(s->priv, s->language, &s->timescale);
/* Apply parameters */
if(s->plugin->set_subtitle_text_parameter)
{
st.func = s->plugin->set_subtitle_text_parameter;
st.data = s->priv;
st.index = s->out_index;
bg_cfg_section_apply(s->section,
s->parameters,
set_stream_param,
&st);
}
return 1;
}
static int start_subtitle_overlay(bg_encoder_t * enc, int stream)
{
subtitle_overlay_stream_t * s;
bg_plugin_handle_t * h;
set_stream_param_struct_t st;
s = &enc->subtitle_overlay_streams[stream];
/* Get handle */
h = get_stream_handle(enc, BG_STREAM_SUBTITLE_OVERLAY, stream, s->in_index);
if(!h)
return 0;
s->plugin = (bg_encoder_plugin_t*)h->plugin;
s->priv = h->priv;
/* Add stream */
s->out_index =
s->plugin->add_subtitle_overlay_stream(s->priv,
s->language, &s->format);
/* Apply parameters */
if(s->plugin->set_subtitle_overlay_parameter)
{
st.func = s->plugin->set_subtitle_overlay_parameter;
st.data = s->priv;
st.index = s->out_index;
bg_cfg_section_apply(s->section,
s->parameters,
set_stream_param,
&st);
}
return 1;
}
/* Start encoding */
int bg_encoder_start(bg_encoder_t * enc)
{
int i;
check_separate(enc);
enc->total_streams = enc->num_audio_streams +
enc->num_video_streams +
enc->num_subtitle_text_streams +
enc->num_subtitle_overlay_streams;
/* We make sure, that for the case of combined streams the
video is always the first one */
for(i = 0; i < enc->num_video_streams; i++)
{
if(!start_video(enc, i))
return 0;
}
for(i = 0; i < enc->num_audio_streams; i++)
{
if(!start_audio(enc, i))
return 0;
}
for(i = 0; i < enc->num_subtitle_text_streams; i++)
{
if(!start_subtitle_text(enc, i))
return 0;
}
for(i = 0; i < enc->num_subtitle_overlay_streams; i++)
{
if(!start_subtitle_overlay(enc, i))
return 0;
}
/* Start encoders */
for(i = 0; i < enc->num_plugins; i++)
{
bg_encoder_plugin_t * plugin =
(bg_encoder_plugin_t *)enc->plugins[i]->plugin;
if(plugin->start && !plugin->start(enc->plugins[i]->priv))
return 0;
}
return 1;
}
/* Add streams */
#define REALLOC_STREAM(streams, num) \
streams = realloc(streams, sizeof(*streams)*(num+1));\
s = &streams[num]; \
memset(s, 0, sizeof(*s));
/* Add streams */
int bg_encoder_add_audio_stream(bg_encoder_t * enc,
const char * language,
gavl_audio_format_t * format,
int source_index)
{
int ret;
audio_stream_t * s;
REALLOC_STREAM(enc->audio_streams,
enc->num_audio_streams);
gavl_audio_format_copy(&s->format, format);
s->in_index = source_index;
if(enc->tt)
s->section = enc->tt->audio_streams[source_index].encoder_section;
else
s->section = enc->audio_stream.section;
if(enc->audio_plugin.info)
s->parameters = enc->audio_plugin.info->audio_parameters;
else if(enc->video_plugin.info)
s->parameters = enc->video_plugin.info->audio_parameters;
if(language)
strncpy(s->language, language, 3);
ret = enc->num_audio_streams;
enc->num_audio_streams++;
return ret;
}
int bg_encoder_add_video_stream(bg_encoder_t * enc,
gavl_video_format_t * format,
int source_index)
{
int ret;
video_stream_t * s;
REALLOC_STREAM(enc->video_streams,
enc->num_video_streams);
gavl_video_format_copy(&s->format, format);
s->in_index = source_index;
if(enc->tt)
s->section = enc->tt->video_streams[source_index].encoder_section;
else
s->section = enc->video_stream.section;
s->parameters = enc->video_plugin.info->video_parameters;
ret = enc->num_video_streams;
enc->num_video_streams++;
return ret;
}
int bg_encoder_add_subtitle_text_stream(bg_encoder_t * enc,
const char * language,
int timescale,
int source_index)
{
int ret;
subtitle_text_stream_t * s;
REALLOC_STREAM(enc->subtitle_text_streams,
enc->num_subtitle_text_streams);
s->timescale = timescale;
s->in_index = source_index;
if(enc->tt)
s->section = enc->tt->subtitle_text_streams[source_index].encoder_section_text;
else
s->section = enc->subtitle_text_stream.section;
if(enc->subtitle_text_plugin.info)
s->parameters = enc->subtitle_text_plugin.info->subtitle_text_parameters;
else if(enc->video_plugin.info)
s->parameters = enc->video_plugin.info->subtitle_text_parameters;
if(language)
strncpy(s->language, language, 3);
ret = enc->num_subtitle_text_streams;
enc->num_subtitle_text_streams++;
return ret;
}
int bg_encoder_add_subtitle_overlay_stream(bg_encoder_t * enc,
const char * language,
gavl_video_format_t * format,
int source_index, bg_stream_type_t source_format)
{
int ret;
subtitle_overlay_stream_t * s;
REALLOC_STREAM(enc->subtitle_overlay_streams,
enc->num_subtitle_overlay_streams);
gavl_video_format_copy(&s->format, format);
s->in_index = source_index;
if(enc->tt)
{
if(source_format == BG_STREAM_SUBTITLE_TEXT)
s->section = enc->tt->subtitle_text_streams[source_index].encoder_section_overlay;
else
s->section = enc->tt->subtitle_overlay_streams[source_index].encoder_section;
}
else
s->section = enc->subtitle_overlay_stream.section;
if(enc->subtitle_overlay_plugin.info)
s->parameters = enc->subtitle_overlay_plugin.info->subtitle_overlay_parameters;
else if(enc->video_plugin.info)
s->parameters = enc->video_plugin.info->subtitle_overlay_parameters;
if(language)
strncpy(s->language, language, 3);
ret = enc->num_subtitle_overlay_streams;
enc->num_subtitle_overlay_streams++;
return ret;
}
void
bg_encoder_set_video_pass(bg_encoder_t * enc,
int stream, int pass, int total_passes,
const char * stats_file)
{
video_stream_t * s = &enc->video_streams[stream];
s->pass = pass;
s->total_passes = total_passes;
s->stats_file = bg_strdup(s->stats_file, stats_file);
}
/* Get formats */
void bg_encoder_get_audio_format(bg_encoder_t * enc,
int stream,
gavl_audio_format_t*ret)
{
audio_stream_t * s = &enc->audio_streams[stream];
s->plugin->get_audio_format(s->priv, s->out_index, ret);
}
void bg_encoder_get_video_format(bg_encoder_t * enc,
int stream,
gavl_video_format_t*ret)
{
video_stream_t * s = &enc->video_streams[stream];
s->plugin->get_video_format(s->priv, s->out_index, ret);
}
void bg_encoder_get_subtitle_overlay_format(bg_encoder_t * enc,
int stream,
gavl_video_format_t*ret)
{
subtitle_overlay_stream_t * s = &enc->subtitle_overlay_streams[stream];
s->plugin->get_subtitle_overlay_format(s->priv, s->out_index, ret);
}
void bg_encoder_get_subtitle_text_timescale(bg_encoder_t * enc,
int stream,
int * ret)
{
subtitle_text_stream_t * s = &enc->subtitle_text_streams[stream];
*ret = s->timescale;
}
/* Write frame */
int bg_encoder_write_audio_frame(bg_encoder_t * enc,
gavl_audio_frame_t * frame,
int stream)
{
audio_stream_t * s = &enc->audio_streams[stream];
return s->plugin->write_audio_frame(s->priv, frame, s->out_index);
}
int bg_encoder_write_video_frame(bg_encoder_t * enc,
gavl_video_frame_t * frame,
int stream)
{
video_stream_t * s = &enc->video_streams[stream];
#if 0
fprintf(stderr, "Write video frame %"PRId64"\n", frame->timestamp);
if(frame->timestamp < s->last_timestamp)
{
fprintf(stderr, "Error: %"PRId64" < %"PRId64"\n",
frame->timestamp, s->last_timestamp);
}
s->last_timestamp = frame->timestamp;
#endif
return s->plugin->write_video_frame(s->priv, frame, s->out_index);
}
int bg_encoder_write_subtitle_text(bg_encoder_t * enc,
const char * text,
int64_t start,
int64_t duration, int stream)
{
subtitle_text_stream_t * s = &enc->subtitle_text_streams[stream];
return s->plugin->write_subtitle_text(s->priv, text, start, duration, s->out_index);
}
int bg_encoder_write_subtitle_overlay(bg_encoder_t * enc,
gavl_overlay_t * ovl, int stream)
{
subtitle_overlay_stream_t * s = &enc->subtitle_overlay_streams[stream];
return s->plugin->write_subtitle_overlay(s->priv, ovl, s->out_index);
}
|