ffmpeg2theora.c 105 KB
Newer Older
1
/* -*- tab-width:4;c-file-style:"cc-mode"; -*- */
2
/*
Jan Gerber's avatar
Jan Gerber committed
3
 * ffmpeg2theora.c -- Convert ffmpeg supported a/v files to  Ogg Theora / Ogg Vorbis
Jan Gerber's avatar
Jan Gerber committed
4
 * Copyright (C) 2003-2008 <j@v2v.cc>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * 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, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Jan Gerber's avatar
Jan Gerber committed
19
 *
20
21
22
23
24
25
26
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
27
#include <math.h>
28
#include <errno.h>
29

30
31
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
32
#ifdef HAVE_FRAMEHOOK
Jan Gerber's avatar
Jan Gerber committed
33
#include "libavformat/framehook.h"
34
#endif
35
36
#include "libswscale/swscale.h"
#include "libpostproc/postprocess.h"
37

Jan Gerber's avatar
Jan Gerber committed
38
#include "theora/theoraenc.h"
39
40
41
#include "vorbis/codec.h"
#include "vorbis/vorbisenc.h"

42
43
44
45
#ifdef WIN32
#include "fcntl.h"
#endif

46
#include "theorautils.h"
47
#include "iso639.h"
48
49
#include "subtitles.h"
#include "ffmpeg2theora.h"
50
#include "avinfo.h"
51

52
53
#define LENGTH(x) (sizeof(x) / sizeof(*x))

54
enum {
Jan Gerber's avatar
Jan Gerber committed
55
56
    NULL_FLAG,
    DEINTERLACE_FLAG,
Jan Gerber's avatar
Jan Gerber committed
57
    SOFTTARGET_FLAG,
Jan Gerber's avatar
Jan Gerber committed
58
59
60
    TWOPASS_FLAG,
    FIRSTPASS_FLAG,
    SECONDPASS_FLAG,
Jan Gerber's avatar
Jan Gerber committed
61
    OPTIMIZE_FLAG,
62
    NOSYNC_FLAG,
Jan Gerber's avatar
Jan Gerber committed
63
64
    NOAUDIO_FLAG,
    NOVIDEO_FLAG,
65
    NOSUBTITLES_FLAG,
66
    NOMETADATA_FLAG,
67
    NOOSHASH_FLAG,
Jan Gerber's avatar
Jan Gerber committed
68
69
70
71
72
73
74
75
76
    NOUPSCALING_FLAG,
    CROPTOP_FLAG,
    CROPBOTTOM_FLAG,
    CROPRIGHT_FLAG,
    CROPLEFT_FLAG,
    ASPECT_FLAG,
    MAXSIZE_FLAG,
    INPUTFPS_FLAG,
    AUDIOSTREAM_FLAG,
Jan Gerber's avatar
Jan Gerber committed
77
    VIDEOSTREAM_FLAG,
Jan Gerber's avatar
Jan Gerber committed
78
79
80
81
82
83
84
85
86
87
    SUBTITLES_FLAG,
    SUBTITLES_ENCODING_FLAG,
    SUBTITLES_LANGUAGE_FLAG,
    SUBTITLES_CATEGORY_FLAG,
    SUBTITLES_IGNORE_NON_UTF8_FLAG,
    VHOOK_FLAG,
    FRONTEND_FLAG,
    FRONTENDFILE_FLAG,
    SPEEDLEVEL_FLAG,
    PP_FLAG,
88
89
    NOSKELETON,
    INFO_FLAG
90
} F2T_FLAGS;
Jan Gerber's avatar
Jan Gerber committed
91

Jan Gerber's avatar
Jan Gerber committed
92
enum {
Jan Gerber's avatar
Jan Gerber committed
93
94
95
96
97
98
    V2V_PRESET_NONE,
    V2V_PRESET_PRO,
    V2V_PRESET_PREVIEW,
    V2V_PRESET_VIDEOBIN,
    V2V_PRESET_PADMA,
    V2V_PRESET_PADMASTREAM,
Jan Gerber's avatar
Jan Gerber committed
99
} F2T_PRESETS;
Jan Gerber's avatar
Jan Gerber committed
100

101

102
103
104
105
106
107
108
109
110
#define PAL_HALF_WIDTH 384
#define PAL_HALF_HEIGHT 288
#define NTSC_HALF_WIDTH 320
#define NTSC_HALF_HEIGHT 240

#define PAL_FULL_WIDTH 720
#define PAL_FULL_HEIGHT 576
#define NTSC_FULL_WIDTH 720
#define NTSC_FULL_HEIGHT 480
Jan Gerber's avatar
Jan Gerber committed
111

Jan Gerber's avatar
Jan Gerber committed
112

Jan Gerber's avatar
Jan Gerber committed
113
oggmux_info info;
114

115
116
static int using_stdin = 0;

Jan Gerber's avatar
Jan Gerber committed
117
118
static int padcolor[3] = { 16, 128, 128 };

119

Jan Gerber's avatar
Jan Gerber committed
120
121
122
123
124
125
static int ilog(unsigned _v){
  int ret;
  for(ret=0;_v;ret++)_v>>=1;
  return ret;
}

126
/**
Jan Gerber's avatar
Jan Gerber committed
127
 * Allocate and initialise an AVFrame.
128
 */
Jan Gerber's avatar
Jan Gerber committed
129
static AVFrame *frame_alloc(int pix_fmt, int width, int height) {
Jan Gerber's avatar
Jan Gerber committed
130
131
132
133
    AVFrame *picture;
    uint8_t *picture_buf;
    int size;

Jan Gerber's avatar
Jan Gerber committed
134
    picture = avcodec_alloc_frame();
Jan Gerber's avatar
Jan Gerber committed
135
136
137
138
    if (!picture)
        return NULL;
    size = avpicture_get_size (pix_fmt, width, height);
    picture_buf = av_malloc (size);
Jan Gerber's avatar
Jan Gerber committed
139
    if (!picture_buf) {
Jan Gerber's avatar
Jan Gerber committed
140
141
142
        av_free (picture);
        return NULL;
    }
Jan Gerber's avatar
Jan Gerber committed
143
    avpicture_fill((AVPicture *) picture, picture_buf, pix_fmt, width, height);
Jan Gerber's avatar
Jan Gerber committed
144
    return picture;
145
146
}

147
148
149
/**
 * Frees an AVFrame.
 */
Jan Gerber's avatar
Jan Gerber committed
150
static void frame_dealloc(AVFrame *frame) {
151
    if (frame) {
Jan Gerber's avatar
Jan Gerber committed
152
        avpicture_free((AVPicture*)frame);
153
154
155
156
        av_free(frame);
    }
}

157
158
159
160
/**
 * initialize ff2theora with default values
 * @return ff2theora struct
 */
Jan Gerber's avatar
Jan Gerber committed
161
static ff2theora ff2theora_init() {
Jan Gerber's avatar
Jan Gerber committed
162
    ff2theora this = calloc (1, sizeof (*this));
Jan Gerber's avatar
Jan Gerber committed
163
    if (this != NULL) {
Jan Gerber's avatar
Jan Gerber committed
164
        this->disable_audio=0;
165
        this->disable_video=0;
166
        this->disable_subtitles=0;
167
        this->disable_metadata=0;
168
        this->disable_oshash=0;
169
        this->no_upscaling=0;
Jan Gerber's avatar
Jan Gerber committed
170
171
172
173
174
175
        this->video_index = -1;
        this->audio_index = -1;
        this->start_time=0;
        this->end_time=0; /* 0 denotes no end time set */

        // audio
176
177
        this->sample_rate = -1;  // samplerate hmhmhm
        this->channels = -1;
178
        this->audio_quality = 1.00;// audio quality 1
Jan Gerber's avatar
Jan Gerber committed
179
        this->audio_bitrate=0;
180
        this->audiostream = -1;
Jan Gerber's avatar
Jan Gerber committed
181

Jan Gerber's avatar
Jan Gerber committed
182
        // video
Jan Gerber's avatar
Jan Gerber committed
183
        this->videostream = -1;
Jan Gerber's avatar
Jan Gerber committed
184
185
        this->picture_width=0;      // set to 0 to not resize the output
        this->picture_height=0;      // set to 0 to not resize the output
Jan Gerber's avatar
Jan Gerber committed
186
        this->video_quality=-1; // defaults set later
Jan Gerber's avatar
Jan Gerber committed
187
        this->video_bitrate=0;
Jan Gerber's avatar
Jan Gerber committed
188
        this->keyint=0;
189
190
        this->force_input_fps.num = -1;
        this->force_input_fps.den = 1;
191
        this->sync = 1;
Jan Gerber's avatar
Jan Gerber committed
192
193
194
        this->aspect_numerator=0;
        this->aspect_denominator=0;
        this->frame_aspect=0;
195
196
        this->max_x=-1;
        this->max_y=-1;
Jan Gerber's avatar
Jan Gerber committed
197
        this->deinterlace=0; // auto by default, if input is flaged as interlaced it will deinterlace.
Jan Gerber's avatar
Jan Gerber committed
198
199
        this->soft_target=0;
        this->buf_delay=-1;
Jan Gerber's avatar
Jan Gerber committed
200
        this->vhook=0;
201
202
        this->framerate_new.num = -1;
        this->framerate_new.den = 1;
Jan Gerber's avatar
Jan Gerber committed
203

Jan Gerber's avatar
Jan Gerber committed
204
205
206
207
        this->frame_topBand=0;
        this->frame_bottomBand=0;
        this->frame_leftBand=0;
        this->frame_rightBand=0;
Jan Gerber's avatar
Jan Gerber committed
208

209
210
        this->n_kate_streams=0;
        this->kate_streams=NULL;
211
        this->ignore_non_utf8 = 0;
212

Jan Gerber's avatar
Jan Gerber committed
213
        this->pix_fmt = PIX_FMT_YUV420P;
214
215
216
217
218
219
220
221
222

        // ffmpeg2theora --nosound -f dv -H 32000 -S 0 -v 8 -x 384 -y 288 -G 1.5 input.dv
        this->video_gamma  = 0.0;
        this->video_bright = 0.0;
        this->video_contr  = 0.0;
        this->video_satur  = 1.0;

        this->y_lut_used = 0;
        this->uv_lut_used = 0;
223
224
        this->sws_colorspace_ctx = NULL;
        this->sws_scale_ctx = NULL;
Jan Gerber's avatar
Jan Gerber committed
225
226
    }
    return this;
227
228
}

229
230
231
232
233
234
235
236
237
// gamma lookup table code

static void y_lut_init(ff2theora this) {
    int i;
    double v;

    double c = this->video_contr;
    double b = this->video_bright;
    double g = this->video_gamma;
Jan Gerber's avatar
Jan Gerber committed
238

239
240
241
242
243
244
245
    if ((g < 0.01) || (g > 100.0)) g = 1.0;
    if ((c < 0.01) || (c > 100.0)) c = 1.0;
    if ((b < -1.0) || (b > 1.0))   b = 0.0;

    if (g == 1.0 && c == 1.0 && b == 0.0) return;
    this->y_lut_used = 1;

Jan Gerber's avatar
typo    
Jan Gerber committed
246
    fprintf(stderr, "  Video correction: gamma=%g, contrast=%g, brightness=%g\n", g, c, b);
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

    g = 1.0 / g;    // larger values shall make brighter video.

    for (i = 0; i < 256; i++) {
        v = (double) i / 255.0;
        v = c * v + b * 0.1;
        if (v < 0.0) v = 0.0;
        v = pow(v, g) * 255.0;    // mplayer's vf_eq2.c multiplies with 256 here, strange...

        if (v >= 255)
            this->y_lut[i] = 255;
        else
            this->y_lut[i] = (unsigned char)(v+0.5);
    }
}


static void uv_lut_init(ff2theora this) {
    int i;
    double v, s;
    s = this->video_satur;

    if ((s < 0.0) || (s > 100.0)) s = 1.0;

    if (s == 1.0) return;
    this->uv_lut_used = 1;

Jan Gerber's avatar
typo    
Jan Gerber committed
274
    fprintf(stderr, "  Color correction: saturation=%g\n", s);
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

    for (i = 0; i < 256; i++) {
        v = 127.0 + (s * ((double)i - 127.0));
        if (v < 0.0) v = 0.0;

        if (v >= 255.0)
            this->uv_lut[i] = 255;
        else
            this->uv_lut[i] = (unsigned char)(v+0.5);
    }
}

static void lut_init(ff2theora this) {
    y_lut_init(this);
    uv_lut_init(this);
}

static void lut_apply(unsigned char *lut, unsigned char *src, unsigned char *dst, int width, int height, int stride) {
    int x, y;

    for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
            dst[x] = lut[src[x]];
        }
        src += stride;
        dst += stride;
    }
}

Jan Gerber's avatar
Jan Gerber committed
304
static void prepare_ycbcr_buffer(ff2theora this, th_ycbcr_buffer ycbcr, AVFrame *frame) {
305
    int i;
Jan Gerber's avatar
Jan Gerber committed
306
    /* pysical pages */
Jan Gerber's avatar
Jan Gerber committed
307
308
309
310
    ycbcr[0].width = this->frame_width;
    ycbcr[0].height = this->frame_height;
    ycbcr[0].stride = frame->linesize[0];
    ycbcr[0].data = frame->data[0];
Jan Gerber's avatar
Jan Gerber committed
311

Jan Gerber's avatar
Jan Gerber committed
312
313
314
315
    ycbcr[1].width = this->frame_width / 2;
    ycbcr[1].height = this->frame_height / 2;
    ycbcr[1].stride = frame->linesize[1];
    ycbcr[1].data = frame->data[1];
Jan Gerber's avatar
Jan Gerber committed
316

Jan Gerber's avatar
Jan Gerber committed
317
318
319
320
    ycbcr[2].width = this->frame_width / 2;
    ycbcr[2].height = this->frame_height / 2;
    ycbcr[2].stride = frame->linesize[1];
    ycbcr[2].data = frame->data[2];
321

322
    if (this->y_lut_used) {
Jan Gerber's avatar
Jan Gerber committed
323
        lut_apply(this->y_lut, ycbcr[0].data, ycbcr[0].data, ycbcr[0].width, ycbcr[0].height, ycbcr[0].stride);
Jan Gerber's avatar
Jan Gerber committed
324
    }
325
    if (this->uv_lut_used) {
Jan Gerber's avatar
Jan Gerber committed
326
327
        lut_apply(this->uv_lut, ycbcr[1].data, ycbcr[1].data, ycbcr[1].width, ycbcr[1].height, ycbcr[1].stride);
        lut_apply(this->uv_lut, ycbcr[2].data, ycbcr[2].data, ycbcr[2].width, ycbcr[2].height, ycbcr[2].stride);
Jan Gerber's avatar
Jan Gerber committed
328
329
330
    }
}

331
332
333
334
335
336
337
static int is_supported_subtitle_stream(ff2theora this, int idx)
{
  AVCodecContext *enc = this->context->streams[idx]->codec;
  if (enc->codec_type != CODEC_TYPE_SUBTITLE) return 0;
  switch (enc->codec_id) {
    case CODEC_ID_TEXT:
    case CODEC_ID_SSA:
338
    case CODEC_ID_MOV_TEXT:
339
340
341
342
343
344
345
      return 1;
    default:
      return 0;
  }
  return 0;
}

346
static char *get_raw_text_from_ssa(const char *ssa)
347
{
348
  int n,intag,inescape;
349
  char *multiblock = NULL, *realloced_mb;
350
351
352
353
  char *allocated;
  const char *dialogue, *ptr, *tag_start;

  if (!ssa) return NULL;
354
355
356
357
358
359
360
361
362
363
364

  /* turns out some SSA packets have several blocks, each on a single line, so loop */
  while (ssa) {
    dialogue=strstr(ssa, "Dialogue:");
    if (!dialogue) break;

    ptr = dialogue;
    for (n=0;n<9;++n) {
      ptr=strchr(ptr,',');
      if (!ptr) return NULL;
      ++ptr;
365
    }
366
367
368
369
370
371
372
373
374
375
    dialogue = ptr;
    allocated = strdup(dialogue);

    /* find all "{...}" tags - the following must work for UTF-8 */
    intag=inescape=0;
    n=0;
    for (ptr=dialogue; *ptr && *ptr!='\n'; ++ptr) {
      if (*ptr=='{') {
        if (intag==0) tag_start = ptr;
        ++intag;
376
      }
377
378
379
380
381
      else if (*ptr=='}') {
        --intag;
        if (intag == 0) {
          /* tag parsing - none for now */
        }
382
      }
383
384
385
386
387
388
389
      else if (!intag) {
        if (inescape) {
          if (*ptr == 'N' || *ptr == 'n')
            allocated[n++] = '\n';
          else if (*ptr == 'h')
            allocated[n++] = ' ';
          inescape=0;
390
391
        }
        else {
392
393
394
395
396
397
          if (*ptr=='\\') {
            inescape=1;
          }
          else {
            allocated[n++]=*ptr;
          }
398
399
400
        }
      }
    }
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
    allocated[n]=0;

    /* skip over what we read */
    ssa = ptr;

    /* remove any trailing newlines (also \r characters) */
    n = strlen(allocated);
    while (n>0 && (allocated[n-1]=='\n' || allocated[n-1]=='\r'))
      allocated[--n]=0;

    /* add this new block */
    realloced_mb = (char*)realloc(multiblock, (multiblock?strlen(multiblock):0) + strlen(allocated) + 2); /* \n + 0 */
    if (realloced_mb) {
      if (multiblock) strcat(realloced_mb, "\n"); else strcpy(realloced_mb, "");
      strcat(realloced_mb, allocated);
      multiblock = realloced_mb;
      free(allocated);
    }
419
420
  }

421
  return multiblock;
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
}

static const float get_ssa_time(const char *p)
{
    int hour, min, sec, hsec;
    int r;

    if(sscanf(p, "%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
        return 0;

    min+= 60*hour;
    sec+= 60*min;
    return (float)(sec*100+hsec)/100;
}

static const float get_duration_from_ssa(const char *ssa)
{
  int n;
  float d = 2.0f;
  double start, end;
  const char *ptr=ssa;

  ptr=strchr(ptr,',');
  if (!ptr) return d;
  ptr++;
  start = get_ssa_time(ptr);
  ptr=strchr(ptr,',');
  if (!ptr) return d;
  ptr++;
  end = get_ssa_time(ptr);

  return end-start;
}

static const char *find_language_for_subtitle_stream(const AVStream *s)
{
  const char *lang=find_iso639_1(s->language);
  if (!lang) {
    fprintf(stderr,"WARNING - unrecognized ISO 639-2 language code: %s\n",s->language);
  }
  return lang;
}

465
void ff2theora_output(ff2theora this) {
466
    unsigned int i;
Jan Gerber's avatar
Jan Gerber committed
467
468
    AVCodecContext *aenc = NULL;
    AVCodecContext *venc = NULL;
469
    int venc_pix_fmt;
Jan Gerber's avatar
Jan Gerber committed
470
471
472
473
    AVStream *astream = NULL;
    AVStream *vstream = NULL;
    AVCodec *acodec = NULL;
    AVCodec *vcodec = NULL;
474
475
    pp_mode_t *ppMode = NULL;
    pp_context_t *ppContext = NULL;
476
    int sws_flags;
Jan Gerber's avatar
Jan Gerber committed
477
    float frame_aspect = 0;
478
    double fps = 0.0;
479
    AVRational vstream_fps;
480
    int display_width, display_height;
481
482
    char *subtitles_enabled = (char*)alloca(this->context->nb_streams);
    char *subtitles_opened = (char*)alloca(this->context->nb_streams);
483
    int synced = this->start_time == 0.0;
484
    AVRational display_aspect_ratio, sample_aspect_ratio;
Jan Gerber's avatar
Jan Gerber committed
485

Jan Gerber's avatar
Jan Gerber committed
486
    if (this->audiostream >= 0 && this->context->nb_streams > this->audiostream) {
487
        AVCodecContext *enc = this->context->streams[this->audiostream]->codec;
488
489
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
            this->audio_index = this->audiostream;
490
            fprintf(stderr, "  Using stream #0.%d as audio input\n",this->audio_index);
491
492
        }
        else {
493
            fprintf(stderr, "  The selected stream is not audio, falling back to automatic selection\n");
494
495
        }
    }
Jan Gerber's avatar
Jan Gerber committed
496
497
498
499
500
501
502
503
504
505
    if (this->videostream >= 0 && this->context->nb_streams > this->videostream) {
        AVCodecContext *enc = this->context->streams[this->videostream]->codec;
        if (enc->codec_type == CODEC_TYPE_VIDEO) {
            this->video_index = this->videostream;
            fprintf(stderr, "  Using stream #0.%d as video input\n",this->video_index);
        }
        else {
            fprintf(stderr, "  The selected stream is not video, falling back to automatic selection\n");
        }
    }
Jan Gerber's avatar
Jan Gerber committed
506

Jan Gerber's avatar
Jan Gerber committed
507
    for (i = 0; i < this->context->nb_streams; i++) {
508
        AVCodecContext *enc = this->context->streams[i]->codec;
Jan Gerber's avatar
Jan Gerber committed
509
        switch (enc->codec_type) {
510
            case CODEC_TYPE_VIDEO:
Jan Gerber's avatar
Jan Gerber committed
511
                if (this->video_index < 0 && !this->disable_video)
512
513
514
515
516
517
518
519
                    this->video_index = i;
                break;
            case CODEC_TYPE_AUDIO:
                if (this->audio_index < 0 && !this->disable_audio)
                    this->audio_index = i;
                break;
            default:
                break;
Jan Gerber's avatar
Jan Gerber committed
520
521
522
        }
    }

Jan Gerber's avatar
Jan Gerber committed
523
    if (this->video_index >= 0) {
Jan Gerber's avatar
Jan Gerber committed
524
        vstream = this->context->streams[this->video_index];
Jan Gerber's avatar
Jan Gerber committed
525
        venc = vstream->codec;
Jan Gerber's avatar
Jan Gerber committed
526
        vcodec = avcodec_find_decoder (venc->codec_id);
527
528
        display_width = venc->width;
        display_height = venc->height;
529
        venc_pix_fmt =  venc->pix_fmt;
Jan Gerber's avatar
Jan Gerber committed
530

531
532
533
        if (this->force_input_fps.num > 0)
            vstream_fps = this->force_input_fps;
        else if (vstream->time_base.den && vstream->time_base.num
534
535
                                  && av_q2d(vstream->time_base) > 0.001) {
            vstream_fps.num = vstream->time_base.den;
536
            vstream_fps.den = vstream->time_base.num;
537
        } else {
538
539
            vstream_fps.num = venc->time_base.den;
            vstream_fps.den = venc->time_base.num * venc->ticks_per_frame;
540
        }
541
542
        if (av_q2d(vstream->r_frame_rate) < av_q2d(vstream_fps)) {
            vstream_fps = vstream->r_frame_rate;
543
        }
544
545
        this->fps = fps = av_q2d(vstream_fps);
            
546
        if (vcodec == NULL || avcodec_open (venc, vcodec) < 0) {
Jan Gerber's avatar
Jan Gerber committed
547
            this->video_index = -1;
548
        }
Jan Gerber's avatar
Jan Gerber committed
549
        this->fps = fps;
550
551
552
553
554
555
556
#if DEBUG
        fprintf(stderr, "FPS1(stream): %f\n", 1/av_q2d(vstream->time_base));
        fprintf(stderr, "FPS2(stream.r_frame_rate): %f\n", av_q2d(vstream->r_frame_rate));
        fprintf(stderr, "FPS3(codec): %f\n", 1/av_q2d(venc->time_base));
        fprintf(stderr, "ticks per frame: %i\n", venc->ticks_per_frame);
        fprintf(stderr, "FPS used: %f\n", fps);
#endif
Jan Gerber's avatar
Jan Gerber committed
557
        if (this->picture_width && !this->picture_height) {
558
            this->picture_height = this->picture_width / ((double)display_width/display_height);
Jan Gerber's avatar
Jan Gerber committed
559
            this->picture_height = this->picture_height - this->picture_height%2;
560
        }
Jan Gerber's avatar
Jan Gerber committed
561
        if (this->picture_height && !this->picture_width) {
562
            this->picture_width = this->picture_height * ((double)display_width/display_height);
Jan Gerber's avatar
Jan Gerber committed
563
            this->picture_width = this->picture_width - this->picture_width%2;
564
        }
565

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

        if (this->picture_height==0 &&
            (this->frame_leftBand || this->frame_rightBand || this->frame_topBand || this->frame_bottomBand) ) {
            this->picture_height=display_height-
                    this->frame_topBand-this->frame_bottomBand;
        }
        if (this->picture_width==0 &&
            (this->frame_leftBand || this->frame_rightBand || this->frame_topBand || this->frame_bottomBand) ) {
            this->picture_width=display_width-
                    this->frame_leftBand-this->frame_rightBand;
        }

        //set display_aspect_ratio from source
        av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
                  venc->width*vstream->sample_aspect_ratio.num,
                  venc->height*vstream->sample_aspect_ratio.den,
                  1024*1024);

        if (vstream->sample_aspect_ratio.num && // default
585
            av_cmp_q(vstream->sample_aspect_ratio, venc->sample_aspect_ratio)) {
586
            sample_aspect_ratio = vstream->sample_aspect_ratio;
587
        } else {
588
            sample_aspect_ratio = venc->sample_aspect_ratio;
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
        }
        if (venc->sample_aspect_ratio.num) {
            av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
                      venc->width*venc->sample_aspect_ratio.num,
                      venc->height*venc->sample_aspect_ratio.den,
                      1024*1024);
        }

        //so frame_aspect is set on the commandline
        if (this->frame_aspect != 0) {
            if (this->picture_height) {
                this->aspect_numerator = 10000*this->frame_aspect*this->picture_height;
                this->aspect_denominator = 10000*this->picture_width;
            }
            else{
                this->aspect_numerator = 10000*this->frame_aspect*display_height;
                this->aspect_denominator = 10000*display_width;
            }
607
608
609
            av_reduce(&this->aspect_numerator,&this->aspect_denominator,
                       this->aspect_numerator,this->aspect_denominator,
                       1024*1024);
610
611
612
            frame_aspect=this->frame_aspect;
        }

Jan Gerber's avatar
Jan Gerber committed
613
614
        if (this->preset == V2V_PRESET_PREVIEW) {
            if (abs(this->fps-30)<1 && (display_width!=NTSC_HALF_WIDTH || display_height!=NTSC_HALF_HEIGHT) ) {
615
616
                this->picture_width=NTSC_HALF_WIDTH;
                this->picture_height=NTSC_HALF_HEIGHT;
Jan Gerber's avatar
Jan Gerber committed
617
            }
Jan Gerber's avatar
Jan Gerber committed
618
619
620
621
            else {
                this->picture_width=PAL_HALF_WIDTH;
                this->picture_height=PAL_HALF_HEIGHT;
            }
Jan Gerber's avatar
Jan Gerber committed
622
        }
Jan Gerber's avatar
Jan Gerber committed
623
624
        else if (this->preset == V2V_PRESET_PRO) {
            if (abs(this->fps-30)<1 && (display_width!=NTSC_FULL_WIDTH || display_height!=NTSC_FULL_HEIGHT) ) {
625
626
                this->picture_width=NTSC_FULL_WIDTH;
                this->picture_height=NTSC_FULL_HEIGHT;
Jan Gerber's avatar
Jan Gerber committed
627
            }
Jan Gerber's avatar
Jan Gerber committed
628
629
630
631
            else {
                this->picture_width=PAL_FULL_WIDTH;
                this->picture_height=PAL_FULL_HEIGHT;
            }
Jan Gerber's avatar
Jan Gerber committed
632
        }
Jan Gerber's avatar
Jan Gerber committed
633
        else if (this->preset == V2V_PRESET_PADMA) {
634
635
            int width=display_width-this->frame_leftBand-this->frame_rightBand;
            int height=display_height-this->frame_topBand-this->frame_bottomBand;
636
637
638
639
            if (sample_aspect_ratio.den!=0 && sample_aspect_ratio.num!=0) {
                height=((float)sample_aspect_ratio.den/sample_aspect_ratio.num) * height;
                sample_aspect_ratio.den = 1;
                sample_aspect_ratio.num = 1;
Jan Gerber's avatar
Jan Gerber committed
640
            }
Jan Gerber's avatar
Jan Gerber committed
641
642
643
644
645
646
647
648
649
650
651
652
            if (this->frame_aspect == 0)
                this->frame_aspect = (float)width/height;
            if (this->frame_aspect <= 1.5) {
                if (width > 640 || height > 480) {
                    //4:3 640 x 480
                    this->picture_width=640;
                    this->picture_height=480;
                }
                else {
                    this->picture_width=width;
                    this->picture_height=height;
                }
Jan Gerber's avatar
Jan Gerber committed
653
654
            }
            else {
Jan Gerber's avatar
Jan Gerber committed
655
656
657
658
659
660
661
662
663
                if (width > 640 || height > 360) {
                    //16:9 640 x 360
                    this->picture_width=640;
                    this->picture_height=360;
                }
                else {
                    this->picture_width=width;
                    this->picture_height=height;
                }
Jan Gerber's avatar
Jan Gerber committed
664
665
            }
        }
Jan Gerber's avatar
Jan Gerber committed
666
667
668
        else if (this->preset == V2V_PRESET_PADMASTREAM) {
            int width=display_width-this->frame_leftBand-this->frame_rightBand;
            int height=display_height-this->frame_topBand-this->frame_bottomBand;
669
670
671
672
            if (sample_aspect_ratio.den!=0 && sample_aspect_ratio.num!=0) {
                height=((float)sample_aspect_ratio.den/sample_aspect_ratio.num) * height;
                sample_aspect_ratio.den = 1;
                sample_aspect_ratio.num = 1;
Jan Gerber's avatar
Jan Gerber committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
            }
            if (this->frame_aspect == 0)
                this->frame_aspect = (float)width/height;
            if (this->frame_aspect <= 1.5) {
                this->picture_width=128;
                this->picture_height=96;
            }
            else {
                this->picture_width=128;
                this->picture_height=72;
            }
        }
        else if (this->preset == V2V_PRESET_VIDEOBIN) {
            int width=display_width-this->frame_leftBand-this->frame_rightBand;
            int height=display_height-this->frame_topBand-this->frame_bottomBand;
688
689
690
691
            if (sample_aspect_ratio.den!=0 && sample_aspect_ratio.num!=0) {
                height=((float)sample_aspect_ratio.den/sample_aspect_ratio.num) * height;
                sample_aspect_ratio.den = 1;
                sample_aspect_ratio.num = 1;
Jan Gerber's avatar
Jan Gerber committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
            }
            if ( ((float)width /height) <= 1.5) {
                if (width > 448) {
                    //4:3 448 x 336
                    this->picture_width=448;
                    this->picture_height=336;
                }
                else {
                    this->picture_width=width;
                    this->picture_height=height;
                }
            }
            else {
                if (width > 512) {
                    //16:9 512 x 288
                    this->picture_width=512;
                    this->picture_height=288;
                }
                else {
                    this->picture_width=width;
                    this->picture_height=height;
                }
            }
        }
716
        if (this->max_x > 0) {
Jan Gerber's avatar
Jan Gerber committed
717
718
            int width = display_width-this->frame_leftBand-this->frame_rightBand;
            int height = display_height-this->frame_topBand-this->frame_bottomBand;
719
720
721
722
            if (sample_aspect_ratio.den!=0 && sample_aspect_ratio.num!=0) {
                height=((float)sample_aspect_ratio.den/sample_aspect_ratio.num) * height;
                sample_aspect_ratio.den = 1;
                sample_aspect_ratio.num = 1;
Jan Gerber's avatar
Jan Gerber committed
723
724
725
            }
            if (this->frame_aspect == 0)
                this->frame_aspect = (float)width/height;
726
727
728
            if (width > height && this->max_x/this->frame_aspect <= this->max_y) {
                this->picture_width = this->max_x;
                this->picture_height = this->max_x / this->frame_aspect;
Jan Gerber's avatar
Jan Gerber committed
729
730
                this->picture_height = this->picture_height + this->picture_height%2;
            } else {
731
732
                this->picture_height = this->max_y;
                this->picture_width = this->max_y * this->frame_aspect;
Jan Gerber's avatar
Jan Gerber committed
733
734
                this->picture_width = this->picture_width + this->picture_width%2;
            }
735
        }
736

Jan Gerber's avatar
Jan Gerber committed
737
        if (this->no_upscaling) {
738
739
            if (this->picture_height && this->picture_height > display_height) {
                this->picture_width = display_height * display_aspect_ratio.num / display_aspect_ratio.den;
740
                this->picture_height = display_height;
741
            }
742
743
744
745
            else if (this->picture_width && this->picture_width > display_width) {
                this->picture_width = display_width;
                this->picture_height = display_width * display_aspect_ratio.den / display_aspect_ratio.num;
            }
746
747
            if (this->fps < av_q2d(this->framerate_new))
                this->framerate_new = vstream_fps;
748
749
        }

750
        if (sample_aspect_ratio.num!=0 && this->frame_aspect==0) {
Jan Gerber's avatar
Jan Gerber committed
751
            // just use the ratio from the input
752
753
            this->aspect_numerator=sample_aspect_ratio.num;
            this->aspect_denominator=sample_aspect_ratio.den;
Jan Gerber's avatar
Jan Gerber committed
754
            // or we use ratio for the output
Jan Gerber's avatar
Jan Gerber committed
755
            if (this->picture_height) {
756
757
                int width=display_width-this->frame_leftBand-this->frame_rightBand;
                int height=display_height-this->frame_topBand-this->frame_bottomBand;
Jan Gerber's avatar
Jan Gerber committed
758
                av_reduce(&this->aspect_numerator,&this->aspect_denominator,
Jan Gerber's avatar
Jan Gerber committed
759
760
                vstream->sample_aspect_ratio.num*width*this->picture_height,
                vstream->sample_aspect_ratio.den*height*this->picture_width,10000);
Jan Gerber's avatar
Jan Gerber committed
761
762
763
764
                frame_aspect=(float)(this->aspect_numerator*this->picture_width)/
                                (this->aspect_denominator*this->picture_height);
            }
            else{
765
766
                frame_aspect=(float)(this->aspect_numerator*display_width)/
                                (this->aspect_denominator*display_height);
Jan Gerber's avatar
Jan Gerber committed
767
768
            }
        }
Jan Gerber's avatar
Jan Gerber committed
769

Jan Gerber's avatar
Jan Gerber committed
770
771
772
773
774
        if ((float)this->aspect_numerator/this->aspect_denominator < 1.09) {
            this->aspect_numerator = 1;
            this->aspect_denominator = 1;
            frame_aspect=(float)(this->aspect_numerator*this->picture_width)/
                                (this->aspect_denominator*this->picture_height);
Jan Gerber's avatar
Jan Gerber committed
775
        }
776
777


778
        if (!(info.twopass==3 && info.passno==2) && !info.frontend && this->aspect_denominator && frame_aspect) {
779
            fprintf(stderr, "  Pixel Aspect Ratio: %.2f/1 ",(float)this->aspect_numerator/this->aspect_denominator);
Jan Gerber's avatar
Jan Gerber committed
780
            fprintf(stderr, "  Frame Aspect Ratio: %.2f/1\n", frame_aspect);
Jan Gerber's avatar
Jan Gerber committed
781
782
        }

783
        if (!(info.twopass==3 && info.passno==2) && !info.frontend && this->deinterlace==1)
784
            fprintf(stderr, "  Deinterlace: on\n");
785
786

        if (strcmp(this->pp_mode, "")) {
Jan Gerber's avatar
Jan Gerber committed
787
788
            ppContext = pp_get_context(display_width, display_height, PP_FORMAT_420);
            ppMode = pp_get_mode_by_name_and_quality(this->pp_mode, PP_QUALITY_MAX);
789
            if(!(info.twopass==3 && info.passno==2) && !info.frontend)
Jan Gerber's avatar
Jan Gerber committed
790
                fprintf(stderr, "  Postprocessing: %s\n", this->pp_mode);
791
792
        }

Jan Gerber's avatar
Jan Gerber committed
793
        if (!this->picture_width)
794
            this->picture_width = display_width;
Jan Gerber's avatar
Jan Gerber committed
795
        if (!this->picture_height)
796
            this->picture_height = display_height;
797

798
799
800
801
        /* Theora has a divisible-by-sixteen restriction for the encoded video size */
        /* scale the frame size up to the nearest /16 and calculate offsets */
        this->frame_width = ((this->picture_width + 15) >>4)<<4;
        this->frame_height = ((this->picture_height + 15) >>4)<<4;
Jan Gerber's avatar
Jan Gerber committed
802

Jan Gerber's avatar
Jan Gerber committed
803
804
805
806
        /*Force the offsets to be even so that chroma samples line up like we
           expect.*/
        this->frame_x_offset = this->frame_width-this->picture_width>>1&~1;
        this->frame_y_offset = this->frame_height-this->picture_height>>1&~1;
Jan Gerber's avatar
Jan Gerber committed
807

808
809
810
811
812
813
814
815
        //Bicubic  (best for upscaling),
        if(display_width - (this->frame_leftBand + this->frame_rightBand) < this->picture_width |
           display_height - (this->frame_topBand + this->frame_bottomBand) < this->picture_height) {
           sws_flags = SWS_BICUBIC;
        } else {        //Bilinear (best for downscaling),
           sws_flags = SWS_BILINEAR;
        }

Jan Gerber's avatar
Jan Gerber committed
816
        if (this->frame_width > 0 || this->frame_height > 0) {
Jan Gerber's avatar
Jan Gerber committed
817
            this->sws_colorspace_ctx = sws_getContext(
818
                            display_width, display_height, venc_pix_fmt,
Jan Gerber's avatar
Jan Gerber committed
819
820
                            display_width, display_height, this->pix_fmt,
                            sws_flags, NULL, NULL, NULL
Jan Gerber's avatar
Jan Gerber committed
821
822
            );
            this->sws_scale_ctx = sws_getContext(
Jan Gerber's avatar
Jan Gerber committed
823
824
825
826
827
                        display_width - (this->frame_leftBand + this->frame_rightBand),
                        display_height - (this->frame_topBand + this->frame_bottomBand),
                        this->pix_fmt,
                        this->picture_width, this->picture_height, this->pix_fmt,
                        sws_flags, NULL, NULL, NULL
Jan Gerber's avatar
Jan Gerber committed
828
            );
Jan Gerber's avatar
Jan Gerber committed
829
            if (!info.frontend && !(info.twopass==3 && info.passno==2)) {
Jan Gerber's avatar
Jan Gerber committed
830
831
832
833
834
                if (this->frame_topBand || this->frame_bottomBand ||
                    this->frame_leftBand || this->frame_rightBand ||
                    this->picture_width != (display_width-this->frame_leftBand - this->frame_rightBand) ||
                    this->picture_height != (display_height-this->frame_topBand-this->frame_bottomBand))
                    fprintf(stderr, "  Resize: %dx%d", display_width, display_height);
Jan Gerber's avatar
Jan Gerber committed
835
836
837
838
839
840
841
842
843
844
                if (this->frame_topBand || this->frame_bottomBand ||
                    this->frame_leftBand || this->frame_rightBand) {
                    fprintf(stderr, " => %dx%d",
                        display_width-this->frame_leftBand-this->frame_rightBand,
                        display_height-this->frame_topBand-this->frame_bottomBand);
                }
                if (this->picture_width != (display_width-this->frame_leftBand - this->frame_rightBand)
                    || this->picture_height != (display_height-this->frame_topBand-this->frame_bottomBand))
                    fprintf(stderr, " => %dx%d",this->picture_width, this->picture_height);
                fprintf(stderr, "\n");
Jan Gerber's avatar
Jan Gerber committed
845
846
            }
        }
847

848
        lut_init(this);
Jan Gerber's avatar
Jan Gerber committed
849
    }
850
    if (!(info.twopass==3 && info.passno==2) && !info.frontend && this->framerate_new.num > 0 && av_cmp_q(vstream_fps, this->framerate_new)) {
Jan Gerber's avatar
Jan Gerber committed
851
        fprintf(stderr, "  Resample Framerate: %0.3f => %0.3f\n",
852
                        this->fps, av_q2d(this->framerate_new));
853
    }
Jan Gerber's avatar
Jan Gerber committed
854
    if (this->audio_index >= 0) {
Jan Gerber's avatar
Jan Gerber committed
855
        astream = this->context->streams[this->audio_index];
856
        aenc = this->context->streams[this->audio_index]->codec;
Jan Gerber's avatar
Jan Gerber committed
857
        acodec = avcodec_find_decoder (aenc->codec_id);
858
        if (this->channels < 1) {
859
            this->channels = aenc->channels;
860
        }
Jan Gerber's avatar
Jan Gerber committed
861
        if (this->sample_rate==-1) {
862
863
            this->sample_rate = aenc->sample_rate;
        }
Jan Gerber's avatar
Jan Gerber committed
864

Jan Gerber's avatar
Jan Gerber committed
865
866
        if (this->no_upscaling) {
            if (this->sample_rate > aenc->sample_rate)
867
                this->sample_rate = aenc->sample_rate;
Jan Gerber's avatar
Jan Gerber committed
868
            if (this->channels > aenc->channels)
869
870
871
                this->channels = aenc->channels;
        }

Jan Gerber's avatar
Jan Gerber committed
872
        if (acodec != NULL && avcodec_open (aenc, acodec) >= 0) {
873
874
875
            if (this->sample_rate != aenc->sample_rate
                || this->channels != aenc->channels
                || aenc->sample_fmt != SAMPLE_FMT_S16) {
876
877
878
879
880
881
882
883
                // values take from libavcodec/resample.c
                this->audio_resample_ctx = av_audio_resample_init(this->channels,    aenc->channels,
                                                                  this->sample_rate, aenc->sample_rate,
                                                                  SAMPLE_FMT_S16,    aenc->sample_fmt,
                                                                  16, 10, 0, 0.8);
                if (!this->audio_resample_ctx) {
                    this->channels = aenc->channels;
                }
Jan Gerber's avatar
Jan Gerber committed
884
                if (!info.frontend && this->sample_rate!=aenc->sample_rate)
885
                    fprintf(stderr, "  Resample: %dHz => %dHz\n",aenc->sample_rate,this->sample_rate);
Jan Gerber's avatar
Jan Gerber committed
886
                if (!info.frontend && this->channels!=aenc->channels)
887
                    fprintf(stderr, "  Channels: %d => %d\n",aenc->channels,this->channels);
Jan Gerber's avatar
Jan Gerber committed
888
889
890
891
892
893
894
895
896
            }
            else{
                this->audio_resample_ctx=NULL;
            }
        }
        else{
            this->audio_index = -1;
        }
    }
897

898
899
900
901
    if (info.passno != 1) {
      for (i = 0; i < this->context->nb_streams; i++) {
        subtitles_enabled[i] = 0;
        subtitles_opened[i] = 0;
902
#ifdef HAVE_KATE
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
        if (!this->disable_subtitles) {
          AVStream *stream = this->context->streams[i];
          AVCodecContext *enc = stream->codec;
          if (enc->codec_type == CODEC_TYPE_SUBTITLE) {
            AVCodec *codec = avcodec_find_decoder (enc->codec_id);
            if (codec && avcodec_open (enc, codec) >= 0) {
              subtitles_opened[i] = 1;
            }
            if (is_supported_subtitle_stream(this, i)) {
              subtitles_enabled[i] = 1;
              add_subtitles_stream(this, i, find_language_for_subtitle_stream(stream), NULL);
            }
            else if(!info.frontend) {
              fprintf(stderr,"Subtitle stream %d codec not supported, ignored\n", i);
            }
918
919
          }
        }
920
#endif
921
      }
922
923
    }

924
#ifdef HAVE_KATE
925
926
    if (info.passno != 1) {
      for (i=0; i<this->n_kate_streams; ++i) {
927
928
        ff2theora_kate_stream *ks=this->kate_streams+i;
        if (ks->stream_index >= 0) {
929
#ifdef DEBUG
930
931
            printf("Muxing Kate stream %d from input stream %d\n",
                i,ks->stream_index);
932
#endif
933
934
935
936
            if (!this->disable_subtitles) {
              info.with_kate=1;
            }
        }
Jan Gerber's avatar
Jan Gerber committed
937
        else if (load_subtitles(ks,this->ignore_non_utf8,info.frontend)>0) {
938
#ifdef DEBUG
939
940
941
942
            printf("Muxing Kate stream %d from %s as %s %s\n",
                i,ks->filename,
                ks->subtitles_language[0]?ks->subtitles_language:"<unknown language>",
                ks->subtitles_category[0]?ks->subtitles_category:"SUB");
943
#endif
944
945
946
947
948
949
950
951
        }
        else {
            if (i!=this->n_kate_streams) {
            memmove(this->kate_streams+i,this->kate_streams+i+1,(this->n_kate_streams-i-1)*sizeof(ff2theora_kate_stream));
            --this->n_kate_streams;
            --i;
          }
        }
952
      }
953
    }
954
#endif
955

956
957
958
    if (info.passno != 1) {
      oggmux_setup_kate_streams(&info, this->n_kate_streams);
    }
959

Jan Gerber's avatar
Jan Gerber committed
960
    if (this->video_index >= 0 || this->audio_index >= 0) {
Jan Gerber's avatar
Jan Gerber committed
961
        AVFrame *frame=NULL;
962
        AVFrame *frame_p=NULL;
Jan Gerber's avatar
Jan Gerber committed
963
        AVFrame *output=NULL;
964
965
        AVFrame *output_p=NULL;
        AVFrame *output_tmp_p=NULL;
Jan Gerber's avatar
Jan Gerber committed
966
        AVFrame *output_tmp=NULL;
967
        AVFrame *output_resized_p=NULL;
Jan Gerber's avatar
Jan Gerber committed
968
        AVFrame *output_resized=NULL;
969
        AVFrame *output_buffered_p=NULL;
Jan Gerber's avatar
Jan Gerber committed
970
        AVFrame *output_buffered=NULL;
971
972
        AVFrame *output_cropped_p=NULL;
        AVFrame *output_cropped=NULL;
Jan Gerber's avatar
Jan Gerber committed
973
974
        AVFrame *output_padded_p=NULL;
        AVFrame *output_padded=NULL;
Jan Gerber's avatar
Jan Gerber committed
975

Jan Gerber's avatar
Jan Gerber committed
976
        AVPacket pkt;
Jan Gerber's avatar
Jan Gerber committed
977
        AVPacket avpkt;
Jan Gerber's avatar
Jan Gerber committed
978
979
980
        int len1;
        int got_picture;
        int first = 1;
981
        int audio_eos = 0, video_eos = 0, audio_done = 0, video_done = 0;
Jan Gerber's avatar
Jan Gerber committed
982
        int ret;
983
984
        int16_t *audio_buf=av_malloc(4*AVCODEC_MAX_AUDIO_FRAME_SIZE);
        int16_t *resampled=av_malloc(4*AVCODEC_MAX_AUDIO_FRAME_SIZE);
Jan Gerber's avatar
Jan Gerber committed
985
        int16_t *audio_p=NULL;
986
        int no_frames;
987
        int no_samples;
Jan Gerber's avatar
Jan Gerber committed
988

Jan Gerber's avatar
Jan Gerber committed
989
        double framerate_add = 0;
990
        double framerate_tmpcount = 0;
Jan Gerber's avatar
Jan Gerber committed
991

Jan Gerber's avatar
Jan Gerber committed
992
        if (this->video_index >= 0)
Jan Gerber's avatar
Jan Gerber committed
993
994
995
            info.audio_only=0;
        else
            info.audio_only=1;
Jan Gerber's avatar
Jan Gerber committed
996

Jan Gerber's avatar
Jan Gerber committed
997
        if (this->audio_index>=0)
Jan Gerber's avatar
Jan Gerber committed
998
999
1000
            info.video_only=0;
        else
            info.video_only=1;
Jan Gerber's avatar
Jan Gerber committed
1001

1002
1003
        if(info.audio_only)
            video_done = 1;
1004
        if(info.video_only || info.passno == 1)
1005
1006
            audio_done = 1;

Jan Gerber's avatar
Jan Gerber committed
1007
        if (!info.audio_only) {
1008
1009
            frame_p = frame = frame_alloc(venc_pix_fmt,
                            venc->width,venc->height);
1010
            output_tmp_p = output_tmp = frame_alloc(this->pix_fmt,
1011
                            venc->width, venc->height);
1012
            output_p = output = frame_alloc(this->pix_fmt,
1013
                            venc->width,venc->height);
1014
            output_resized_p = output_resized = frame_alloc(this->pix_fmt,