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
|
/*
* export_mov.c
*
* Copyright (C) Thomas Oestreich - June 2001
* Copyright (C) (2002) Christian Vogelgsang
* <Vogelgsang@informatik.uni-erlangen.de> (extension for all codecs supported
* by quicktime4linux
* <stefanscheffler@gmx.net> 2004 fixes & features
* Copyright (C) ken@hero.com 2001 (initial module author)
*
* This file is part of transcode, a video stream processing tool
*
* transcode 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, or (at your option)
* any later version.
*
* transcode 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 GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define MOD_NAME "export_mov.so"
#define MOD_VERSION "v0.1.2 (2004-01-19)"
#define MOD_CODEC "(video) * | (audio) *"
#include "transcode.h"
#include "import/magic.h"
#include "encoder.h"
#include "libtc/optstr.h"
#include "libtcvideo/tcvideo.h"
#include <stdio.h>
#include <stdlib.h>
/* verbose flag */
static int verbose_flag=TC_QUIET;
/* set encoder capabilities */
static int capability_flag=
/* audio formats */
TC_CAP_PCM| /* pcm audio */
/* video formats */
TC_CAP_RGB| /* rgb frames */
TC_CAP_YUV| /* YUV 4:2:0 */
TC_CAP_YUY2|
TC_CAP_VID|
TC_CAP_YUV422; /* YUV 4:2:2 */
#define MOD_PRE mov
#include "export_def.h"
#include <quicktime.h>
#include <colormodels.h>
#include <lqt.h>
#define QT_LIST_AUDIO "audio codec"
#define QT_LIST_VIDEO "video codec"
#define QT_LIST_PARM "parameters"
/* exported quicktime file */
static quicktime_t *qtfile = NULL;
/* row pointer for source frames */
static unsigned char** row_ptr = NULL;
/* temporary buffer */
static uint8_t *tmp_buf;
/* toggle for raw frame export */
static int rawVideo = 0;
static int rawAudio = 0;
/* colormodels */
static ImageFormat tc_cm = 0;
static int qt_cm = 0;
/* store frame dimension */
static int w = 0, h = 0;
/* audio channels */
static int channels = 0;
/* sample size */
static int bits = 0;
/* encode audio buffers */
static int16_t* audbuf0 = NULL;
static int16_t* audbuf1 = NULL;
struct qt_codec_list {
char *name;
char *internal_name;
char *comments;
};
static TCVHandle tcvhandle = 0;
/* special paramters not retrievable from lqt */
struct qt_codec_list qt_param_list[] = {
{"", "", ""},
{"Special Parameters:", "", ""},
{"copyright", "", "Copyright string (no '=' or ',' allowed)"},
{"name", "", "Name string (no '=' or ',' allowed) "},
{"info", "", "Info string (no '=' or ',' allowed) "},
{NULL, NULL, NULL}};
#ifdef LIBQUICKTIME_000904
/* from libquicktime */
static int tc_quicktime_get_timescale(double frame_rate)
{
int timescale = 600;
/* Encode the 29.97, 23.976, 59.94 framerates */
if(frame_rate - (int)frame_rate != 0)
timescale = (int)(frame_rate * 1001 + 0.5);
else
if((600 / frame_rate) - (int)(600 / frame_rate) != 0)
timescale = (int)(frame_rate * 100 + 0.5);
return timescale;
}
#endif
/* print list of things. Shamelessly stolen from export_ffmpeg.c */
static int list(char *list_type)
{
int cod = 0;
int i = 0;
lqt_codec_info_t ** qi = NULL;
if (strcmp(list_type, QT_LIST_VIDEO) == 0) {
qi = lqt_query_registry(0, 1, 1, 0);
} else if (strcmp(list_type, QT_LIST_AUDIO) == 0) {
qi = lqt_query_registry(1, 0, 1, 0);
} else {
qi = lqt_query_registry(1, 1, 1, 0);
}
tc_log_info(MOD_NAME, "List of supported %s:", list_type);
tc_log_info(MOD_NAME, "Name comments");
tc_log_info(MOD_NAME, "--------------- "
"-----------------------------------");
while (qi[cod] != NULL) {
if (strcmp(list_type, QT_LIST_PARM) == 0) {
tc_log_info(MOD_NAME, "%s:", qi[cod]->name);
for(i = 0; i < qi[cod]->num_encoding_parameters; i++) {
if (qi[cod]->encoding_parameters[i].type != LQT_PARAMETER_SECTION) {
tc_log_info(MOD_NAME, " %-23s %s",
qi[cod]->encoding_parameters[i].name,
qi[cod]->encoding_parameters[i].real_name);
}
}
} else {
tc_log_info(MOD_NAME, "%-23s %s",
qi[cod]->name,
qi[cod]->description);
}
cod++;
}
return 1;
}
/* ------------------------------------------------------------
*
* open outputfile
*
* ------------------------------------------------------------*/
MOD_open
{
if(param->flag == TC_VIDEO)
return(0);
if(param->flag == TC_AUDIO)
return(0);
return(TC_EXPORT_ERROR);
}
/* ------------------------------------------------------------
*
* init codec
*
* ------------------------------------------------------------*/
MOD_init
{
int list_was_called = 0;
/* for codec parameters */
int jpeg_quality = 0;
int div3_bitrate_tolerance = 500000;
int vorbis_max_bitrate = 192;
int vorbis_min_bitrate = 128;
/* overwriting empty parameters now saves trouble later */
if (vob->ex_v_fcc == NULL) vob->ex_v_fcc = "";
if (vob->ex_a_fcc == NULL) vob->ex_a_fcc = "";
if (vob->ex_profile_name == NULL) vob->ex_profile_name = "";
if (!strcasecmp(vob->ex_v_fcc, "list")) list_was_called = list(QT_LIST_VIDEO);
if (!strcasecmp(vob->ex_a_fcc, "list")) list_was_called = list(QT_LIST_AUDIO);
if (!strcasecmp(vob->ex_profile_name, "list")) {
int i;
list_was_called = list(QT_LIST_PARM);
/* list special paramters at the end */
for(i = 0; qt_param_list[i].name != NULL; i++) {
tc_log_info(MOD_NAME, " %-23s %s",
qt_param_list[i].name,
qt_param_list[i].comments);
}
}
if (list_was_called) {
return(TC_EXPORT_ERROR);
}
/* video setup -------------------------------------------------- */
if(param->flag == TC_VIDEO) {
lqt_codec_info_t ** qt_codec_info = NULL;
char *qt_codec;
int divx_bitrate;
/* fetch frame size */
w = vob->ex_v_width;
h = vob->ex_v_height;
/* fetch qt codec from -F switch */
qt_codec = tc_strdup(vob->ex_v_fcc);
/* open target file for writing */
if(NULL == (qtfile = quicktime_open((char *)vob->video_out_file, 0, 1)) ){
tc_log_warn(MOD_NAME, "error opening qt file '%s'",
vob->video_out_file);
return(TC_EXPORT_ERROR);
}
/* set qt codecs */
/* not needed for passthrough*/
if (vob->im_v_codec != CODEC_RAW && vob->im_v_codec != CODEC_RAW_YUV && vob->im_v_codec != CODEC_RAW_RGB) {
if(qt_codec == NULL || strlen(qt_codec)==0) {
/* default */
qt_codec = "mjpa";
if (verbose_flag != TC_QUIET) {
tc_log_info(MOD_NAME, "Empty qt codec name. Switching to %s use '-F list'"
" to get a list of supported codec.", qt_codec);
}
}
/* check if we can encode with this codec */
qt_codec_info = lqt_find_video_codec_by_name(qt_codec);
if (!qt_codec_info) {
tc_log_warn(MOD_NAME, "qt video codec '%s' not supported!", qt_codec);
return(TC_EXPORT_ERROR);
}
#if !defined(LIBQUICKTIME_000904)
/* set proposed video codec */
lqt_set_video(qtfile, 1, w, h, vob->ex_fps,qt_codec_info[0]);
#else
/* set proposed video codec */
lqt_set_video(qtfile, 1, w, h,
tc_quicktime_get_timescale(vob->ex_fps) / vob->ex_fps+0.5,
tc_quicktime_get_timescale(vob->ex_fps), qt_codec_info[0]);
#endif
}
/* set color model */
switch(vob->im_v_codec) {
case CODEC_RGB:
qt_cm = BC_RGB888;
tc_cm = IMG_RGB_DEFAULT;
break;
case CODEC_YUV:
qt_cm = BC_YUV420P;
tc_cm = IMG_YUV_DEFAULT;
break;
case CODEC_YUV422:
tc_cm = IMG_YUV422P;
qt_cm = BC_YUV422P;
break;
case CODEC_YUY2:
tc_cm = IMG_YUY2;
qt_cm = BC_YUV422;
break;
/* passthrough */
case CODEC_RAW_RGB:
case CODEC_RAW_YUV:
case CODEC_RAW:
/* set out output codec to input codec */
if(qt_codec == NULL || strlen(qt_codec)==0) {
switch (vob->v_codec_flag) {
case TC_CODEC_MJPEG:
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"jpeg");
break;
case TC_CODEC_MPEG:
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"mpeg");
break;
case TC_CODEC_DV:
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"dvc ");
break;
case TC_CODEC_SVQ1:
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"SVQ1");
break;
case TC_CODEC_SVQ3:
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"SVQ3");
break;
case TC_CODEC_YUV420P:
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"yv12");
break;
case TC_CODEC_RGB:
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"raw ");
break;
case TC_CODEC_YUV2: /* test this */
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"yuv2");
break;
case TC_CODEC_DIVX3:
case TC_CODEC_DIVX4:
case TC_CODEC_DIVX5:
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,"DIVX");
break;
default:
tc_log_warn(MOD_NAME, "codec '%lx' not supported for pass-through",
vob->v_codec_flag);
tc_log_warn(MOD_NAME, " If you really know what you are doing you can force");
tc_log_warn(MOD_NAME, " a codec via -F <vc>, '-F list' returns a list");
return(TC_EXPORT_ERROR);
break;
}
}
else {
tc_log_warn(MOD_NAME,"Overriding the output codec is almost never a good idea");
quicktime_set_video(qtfile, 1, w, h, vob->ex_fps,qt_codec);
}
rawVideo = 1;
break;
default:
/* unsupported internal format */
tc_log_warn(MOD_NAME, "unsupported internal video format %x",
vob->ex_v_codec);
return(TC_EXPORT_ERROR);
break;
}
/* if cs conversation not supported for codec do conversation */
/* not required for pass through */
if (vob->im_v_codec != CODEC_RAW && vob->im_v_codec != CODEC_RAW_YUV && vob->im_v_codec != CODEC_RAW_RGB) {
if (quicktime_writes_cmodel(qtfile, qt_cm, 0) != 1) {
if (verbose_flag != TC_QUIET) {
tc_log_info(MOD_NAME,"Colorspace not supported for this codec converting to RGB");
}
qt_cm = BC_RGB888;
lqt_set_cmodel(qtfile, 0, qt_cm);
tcvhandle = tcv_init();
if (!tcvhandle) {
tc_log_warn(MOD_NAME, "image conversion init failed");
return TC_EXPORT_ERROR;
}
} else {
lqt_set_cmodel(qtfile, 0, qt_cm);
}
}
/* set codec parameters */
/* tc uses kb */
divx_bitrate = vob->divxbitrate * 1000;
/* if max bitrate > bitrate use difference for bitrate tolerance */
if (vob->video_max_bitrate > vob->divxbitrate) div3_bitrate_tolerance = (vob->video_max_bitrate - vob->divxbitrate) * 1000;
/* Audio */
/* must be changed when it's changed in lqt; "bit_rate" conflicts with ffmpeg video codec */
if (strcmp(qt_codec,"ffmpeg_mp2") == 0||
strcmp(qt_codec,"ffmpeg_mp3") == 0||
strcmp(qt_codec,"ffmpeg_ac3") == 0)
quicktime_set_parameter(qtfile, "bit_rate", &vob->mp3bitrate);
if (strcmp(qt_codec,"lame") == 0)
quicktime_set_parameter(qtfile, "mp3_bitrate", &vob->mp3bitrate);
/* have to check this */
if (strcmp(qt_codec,"vorbis") == 0) {
quicktime_set_parameter(qtfile, "vorbis_bitrate", &vob->mp3bitrate);
quicktime_set_parameter(qtfile, "vorbis_max_bitrate", &vorbis_max_bitrate);
quicktime_set_parameter(qtfile, "vorbis_min_bitrate", &vorbis_min_bitrate);
quicktime_set_parameter(qtfile, "vorbis_vbr", &vob->a_vbr);
}
/* Video */
/* jpeg_quality == 0-100 ; tc quality setting (-Q) == 1-5 */
jpeg_quality = 20 * vob->divxquality;
if (strcmp(qt_codec,"mjpa") == 0||
strcmp(qt_codec,"jpeg") == 0)
quicktime_set_parameter(qtfile, "jpeg_quality", &jpeg_quality);
/* looks terrible :/ */
if (strcmp(qt_codec,"ffmpeg_mjpg") == 0||
strcmp(qt_codec,"ffmpeg_h263p") == 0||
strcmp(qt_codec,"ffmpeg_h263") == 0||
strcmp(qt_codec,"ffmpeg_msmpeg4v3") == 0||
strcmp(qt_codec,"ffmpeg_msmpeg4v2") == 0||
strcmp(qt_codec,"ffmpeg_msmpeg4v1") == 0||
strcmp(qt_codec,"ffmpeg_msmpg1") == 0||
strcmp(qt_codec,"ffmpeg_mpg4") == 0){
int min_bitrate = 0;
quicktime_set_parameter(qtfile, "flags_gray", &vob->decolor); /* set format different for this */
switch (vob->decolor) {
case 1:
quicktime_set_parameter(qtfile, "aspect_ratio_info", "Square");
break;
case 2:
quicktime_set_parameter(qtfile, "aspect_ratio_info", "4:3");
break;
case 3:
quicktime_set_parameter(qtfile, "aspect_ratio_info", "16:9");
break;
default:
tc_log_warn(MOD_NAME, "Given aspect ratio not supported, using default");
break;
}
quicktime_set_parameter(qtfile, "flags_gray", &vob->decolor);
quicktime_set_parameter(qtfile, "bit_rate", &vob->divxbitrate);
quicktime_set_parameter(qtfile, "bit_rate_tolerance", &div3_bitrate_tolerance);
min_bitrate = vob->divxbitrate - div3_bitrate_tolerance;
quicktime_set_parameter(qtfile, "rc_max_rate", &vob->video_max_bitrate);
quicktime_set_parameter(qtfile, "qmax", &vob->max_quantizer);
quicktime_set_parameter(qtfile, "qmin", &vob->min_quantizer);
if (!strcmp(qt_codec,"ffmpeg_mjpg"))
quicktime_set_parameter(qtfile, "gob_size", &vob->divxkeyframes);
}
if (strcmp(vob->ex_v_fcc,"opendivx") == 0) {
quicktime_set_parameter(qtfile, "divx_bitrate", &divx_bitrate);
quicktime_set_parameter(qtfile, "divx_rc_period", &vob->rc_period);
quicktime_set_parameter(qtfile, "divx_rc_reaction_period", &vob->rc_reaction_period);
quicktime_set_parameter(qtfile, "divx_rc_reaction_ratio", &vob->rc_reaction_ratio);
quicktime_set_parameter(qtfile, "divx_max_key_interval", &vob->divxkeyframes);
quicktime_set_parameter(qtfile, "divx_min_quantizer", &vob->min_quantizer);
quicktime_set_parameter(qtfile, "divx_max_quantizer", &vob->max_quantizer);
quicktime_set_parameter(qtfile, "divx_quantizer", &vob->min_quantizer);
quicktime_set_parameter(qtfile, "divx_quality", &vob->quality);
}
if (strcmp(qt_codec,"rtjpeg") == 0)
quicktime_set_parameter(qtfile, "rtjpeg_quality", &jpeg_quality);
/* set extended parameters */
if (vob->ex_profile_name != NULL) {
int i;
char meta[128];
if (optstr_get(vob->ex_profile_name, "copyright", "%128[^:]", meta) > 0) {
quicktime_set_copyright(qtfile, meta);
}
if(optstr_get(vob->ex_profile_name, "name", "%128[^:]", meta) > 0) {
quicktime_set_name(qtfile, meta);
}
if(optstr_get(vob->ex_profile_name, "info", "%128[^:]", meta) > 0) {
quicktime_set_name(qtfile, meta);
}
for (i = 0; i < (* qt_codec_info)->num_encoding_parameters; i++) {
lqt_parameter_info_t param = (* qt_codec_info)->encoding_parameters[i];
if (param.type == LQT_PARAMETER_INT) {
int val;
if (optstr_get(vob->ex_profile_name, param.name, "%i", &val) > 0) {
lqt_set_video_parameter(qtfile, 0, param.name, &val);
}
}
else if (param.type == LQT_PARAMETER_FLOAT) {
float val;
if (optstr_get(vob->ex_profile_name, param.name, "%f", &val) > 0) {
lqt_set_video_parameter(qtfile, 0, param.name, &val);
}
}
else if (param.type == LQT_PARAMETER_STRING || param.type == LQT_PARAMETER_STRINGLIST) {
char val[128];
if (optstr_get(vob->ex_profile_name, param.name, "%128[^:]", val) > 0) {
lqt_set_video_parameter(qtfile, 0, param.name, val);
}
}
}
}
/* alloc row pointers for frame encoding */
row_ptr = malloc (sizeof(char *) * h);
/* same for temp buffer ... used during yuy2/yv12 encoding */
tmp_buf = malloc (w*h*2);
/* verbose */
tc_log_info(MOD_NAME, "video codec='%s' w=%d h=%d fps=%g",
qt_codec,w,h,vob->ex_fps);
return(0);
}
/* audio setup -------------------------------------------------- */
if(param->flag == TC_AUDIO){
const char *qt_codec;
lqt_codec_info_t ** qt_codec_info = 0;
/* no audio setup if we don't have any channels (size == 0 might be better?)*/
if(vob->dm_chan==0) return 0;
/* check given audio format */
if((vob->dm_chan!=1)&&(vob->dm_chan!=2)) {
tc_log_warn(MOD_NAME, "Only mono or stereo audio supported");
return(TC_EXPORT_ERROR);
}
channels = vob->dm_chan;
bits = vob->dm_bits;
/* fetch qt codec from -F switch */
qt_codec = vob->ex_a_fcc;
if(qt_codec == NULL || strlen(qt_codec)==0) {
qt_codec = "ima4";
if (verbose_flag != TC_QUIET) {
tc_log_info(MOD_NAME, "empty qt codec name - switching to %s"
" use '-F ,list'", qt_codec);
tc_log_info(MOD_NAME, "to get a list of supported codec");
}
}
/* check encoder mode */
switch(vob->im_a_codec) {
case CODEC_PCM:
/* allocate sample buffers */
audbuf0 = (int16_t*)malloc(vob->ex_a_size);
audbuf1 = (int16_t*)malloc(vob->ex_a_size);
/* need to encode audio */
rawAudio = 0;
break;
default:
/* unsupported internal format */
tc_log_warn(MOD_NAME, "unsupported internal audio format %x",
vob->ex_v_codec);
return(TC_EXPORT_ERROR);
break;
}
qt_codec_info = lqt_find_audio_codec_by_name(qt_codec);
if (!qt_codec_info){
tc_log_warn(MOD_NAME, "qt audio codec '%s' unsupported",
qt_codec);
return(TC_EXPORT_ERROR);
}
/* setup audio parameters */
lqt_set_audio(qtfile,channels,vob->a_rate,bits,qt_codec_info[0]);
/* verbose */
tc_log_info(MOD_NAME, "audio codec='%s' bits=%d chan=%d rate=%d",
qt_codec,bits,channels,vob->a_rate);
return(0);
}
return(TC_EXPORT_ERROR);
}
/* ------------------------------------------------------------
*
* encode and export frame
*
* ------------------------------------------------------------*/
MOD_encode
{
/* video -------------------------------------------------------- */
if(param->flag == TC_VIDEO) {
vob_t *vob = tc_get_vob();
/* raw mode is easy */
if(rawVideo) {
/* add divx keyframes if needed */
if(quicktime_divx_is_key(param->buffer, param->size) == 1) quicktime_insert_keyframe(qtfile, (int)tc_get_frames_encoded(), 0);
if(quicktime_write_frame(qtfile,param->buffer,param->size,0)<0) {
tc_log_warn(MOD_NAME, "error writing raw video frame");
return(TC_EXPORT_ERROR);
}
}
/* encode frame */
else {
uint8_t *ptr = param->buffer;
int iy;
switch(qt_cm) {
case BC_RGB888:
/* convert to rgb if unsupported */
if (tc_cm != IMG_RGB24) {
if (!tcv_convert(tcvhandle, param->buffer, param->buffer,
vob->ex_v_width, vob->ex_v_height,
tc_cm, IMG_RGB24)) {
tc_log_warn(MOD_NAME, "image format conversion failed");
return TC_EXPORT_ERROR;
}
}
/* setup row pointers for RGB */
for (iy = 0; iy < h; iy++) {
row_ptr[iy] = ptr;
ptr += w*3;
}
break;
case BC_YUV420P:
/* setup row pointers for YUV420P */
row_ptr[0] = ptr;
ptr = ptr + (h * w);
/* note, quicktime wants YV12 so we reverse the planes */
row_ptr[2] = ptr;
ptr = ptr + (h * w) / 4;
row_ptr[1] = ptr;
break;
case BC_YUV422P:
/* setup row pointers for YUV422P */
row_ptr[0] = ptr;
ptr = ptr + (h * w);
row_ptr[1] = ptr;
ptr = ptr + (h * w) / 2;
row_ptr[2] = ptr;
break;
case BC_YUV422:
/* setup row pointers for YUY2 */
for (iy = 0; iy < h; iy++) {
row_ptr[iy] = ptr;
ptr += w*2;
}
break;
}
if(quicktime_encode_video(qtfile, row_ptr, 0)<0) {
tc_log_warn(MOD_NAME, "error encoding video frame");
return(TC_EXPORT_ERROR);
}
}
return(0);
}
/* audio -------------------------------------------------------- */
if(param->flag == TC_AUDIO){
/* raw mode is easy */
if(rawAudio) {
if(quicktime_write_frame(qtfile,
param->buffer,param->size,0)<0) {
tc_log_warn(MOD_NAME, "error writing raw audio frame");
return(TC_EXPORT_ERROR);
}
}
/* encode audio */
else {
int s,t;
int16_t *aptr[2] = { audbuf0, audbuf1 };
/* calc number of samples */
int samples = param->size;
/* no audio */
if (samples == 0) return 0;
if(bits==16)
samples >>= 1;
if(channels==2)
samples >>= 1;
/* fill audio buffer */
if(bits==8) {
/* UNTESTED: please verify :) */
if(channels==1) {
for(s=0;s<samples;s++)
audbuf0[s] = ((((int16_t)param->buffer[s]) << 8)-0x8000);
}
else /* stereo */ {
for(s=0,t=0;s<samples;s++,t+=2) {
audbuf0[s] = ((((int16_t)param->buffer[t]) << 8)-0x8000);
audbuf1[s] = ((((int16_t)param->buffer[t+1]) << 8)-0x8000);
}
}
}
else /* 16 bit */ {
if(channels==1) {
aptr[0] = (int16_t *)param->buffer;
}
else /* stereo */ {
/* decouple channels */
for(s=0,t=0;s<samples;s++,t+=2) {
audbuf0[s] = ((int16_t *)param->buffer)[t];
audbuf1[s] = ((int16_t *)param->buffer)[t+1];
}
}
}
/* encode audio samples */
if ((quicktime_encode_audio(qtfile, aptr, NULL, samples)<0)){
tc_log_warn(MOD_NAME, "error encoding audio frame");
return(TC_EXPORT_ERROR);
}
}
return(0);
}
return(TC_EXPORT_ERROR);
}
/* ------------------------------------------------------------
*
* stop encoder
*
* ------------------------------------------------------------*/
MOD_stop
{
if(param->flag == TC_VIDEO) {
/* free row pointers */
if(row_ptr!=NULL)
free(row_ptr);
return(0);
}
if(param->flag == TC_AUDIO) {
/* free audio buffers */
if(audbuf0!=NULL)
free(audbuf0);
if(audbuf1!=NULL)
free(audbuf1);
return(0);
}
return(TC_EXPORT_ERROR);
}
/* ------------------------------------------------------------
*
* close outputfile
*
* ------------------------------------------------------------*/
MOD_close
{
if(param->flag == TC_VIDEO){
/* close quicktime file */
quicktime_close(qtfile);
qtfile=NULL;
return(0);
}
if(param->flag == TC_AUDIO) {
return(0);
}
return(TC_EXPORT_ERROR);
}
|