ffmpeg2theora.c 124 KB
Newer Older
1
/* -*- tab-width:4;c-file-style:"cc-mode"; -*- */
2
/*
3
 * ffmpeg2theora.c -- Convert ffmpeg supported a/v files to  Ogg Theora / Vorbis
4
 * Copyright (C) 2003-2011 <j@v2v.cc>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 2 of the License, or
9
10
11
12
13
14
15
16
 * (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
17
 * along with This program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
21
22
23
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
24
#include <ctype.h>
25
#include <getopt.h>
26
#include <math.h>
27
#include <errno.h>
28

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

37
38
39
40
41
#include "libavutil/opt.h"
#include "libavutil/channel_layout.h"
#include "libavutil/samplefmt.h"
#include "libswresample_compat.h"

Jan Gerber's avatar
Jan Gerber committed
42
#include "theora/theoraenc.h"
43
44
45
#include "vorbis/codec.h"
#include "vorbis/vorbisenc.h"

46
47
48
49
#ifdef WIN32
#include "fcntl.h"
#endif

50
#include "theorautils.h"
51
#include "iso639.h"
52
53
#include "subtitles.h"
#include "ffmpeg2theora.h"
54
#include "avinfo.h"
55

56
57
58
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio


59
60
#define LENGTH(x) (sizeof(x) / sizeof(*x))

61
enum {
Jan Gerber's avatar
Jan Gerber committed
62
63
    NULL_FLAG,
    DEINTERLACE_FLAG,
Jan Gerber's avatar
Jan Gerber committed
64
    NODEINTERLACE_FLAG,
Jan Gerber's avatar
Jan Gerber committed
65
    SOFTTARGET_FLAG,
Jan Gerber's avatar
Jan Gerber committed
66
67
68
    TWOPASS_FLAG,
    FIRSTPASS_FLAG,
    SECONDPASS_FLAG,
Jan Gerber's avatar
Jan Gerber committed
69
    OPTIMIZE_FLAG,
70
    NOSYNC_FLAG,
Jan Gerber's avatar
Jan Gerber committed
71
72
    NOAUDIO_FLAG,
    NOVIDEO_FLAG,
73
    NOSUBTITLES_FLAG,
74
    SUBTITLETYPES_FLAG,
75
    NOMETADATA_FLAG,
76
    NOOSHASH_FLAG,
Jan Gerber's avatar
Jan Gerber committed
77
78
79
80
81
82
    NOUPSCALING_FLAG,
    CROPTOP_FLAG,
    CROPBOTTOM_FLAG,
    CROPRIGHT_FLAG,
    CROPLEFT_FLAG,
    ASPECT_FLAG,
83
    PIXEL_ASPECT_FLAG,
Jan Gerber's avatar
Jan Gerber committed
84
85
86
    MAXSIZE_FLAG,
    INPUTFPS_FLAG,
    AUDIOSTREAM_FLAG,
Jan Gerber's avatar
Jan Gerber committed
87
    VIDEOSTREAM_FLAG,
Jan Gerber's avatar
Jan Gerber committed
88
89
90
91
92
93
94
95
96
97
    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,
98
    RESIZE_METHOD_FLAG,
99
    NOSKELETON,
100
    SKELETON_3,
101
    INDEX_INTERVAL,
Jan Gerber's avatar
Jan Gerber committed
102
103
    THEORA_INDEX_RESERVE,
    VORBIS_INDEX_RESERVE,
104
    KATE_INDEX_RESERVE,
105
    INFO_FLAG
106
} F2T_FLAGS;
Jan Gerber's avatar
Jan Gerber committed
107

Jan Gerber's avatar
Jan Gerber committed
108
enum {
Jan Gerber's avatar
Jan Gerber committed
109
110
111
112
113
114
    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
115
} F2T_PRESETS;
Jan Gerber's avatar
Jan Gerber committed
116

117

118
119
120
121
122
123
124
125
126
#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
127

128
129
#define INCSUB_TEXT 1
#define INCSUB_SPU 2
Jan Gerber's avatar
Jan Gerber committed
130

Jan Gerber's avatar
Jan Gerber committed
131
oggmux_info info;
132

133
134
static int using_stdin = 0;

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

137

Jan Gerber's avatar
Jan Gerber committed
138
139
140
141
142
143
static int ilog(unsigned _v){
  int ret;
  for(ret=0;_v;ret++)_v>>=1;
  return ret;
}

144
/**
Jan Gerber's avatar
Jan Gerber committed
145
 * Allocate and initialise an AVFrame.
146
 */
Jan Gerber's avatar
Jan Gerber committed
147
static AVFrame *frame_alloc(int pix_fmt, int width, int height) {
Jan Gerber's avatar
Jan Gerber committed
148
149
150
151
    AVFrame *picture;
    uint8_t *picture_buf;
    int size;

Jan Gerber's avatar
Jan Gerber committed
152
    picture = avcodec_alloc_frame();
Jan Gerber's avatar
Jan Gerber committed
153
154
155
156
    if (!picture)
        return NULL;
    size = avpicture_get_size (pix_fmt, width, height);
    picture_buf = av_malloc (size);
Jan Gerber's avatar
Jan Gerber committed
157
    if (!picture_buf) {
Jan Gerber's avatar
Jan Gerber committed
158
159
160
        av_free (picture);
        return NULL;
    }
Jan Gerber's avatar
Jan Gerber committed
161
    avpicture_fill((AVPicture *) picture, picture_buf, pix_fmt, width, height);
Jan Gerber's avatar
Jan Gerber committed
162
    return picture;
163
164
}

165
166
167
/**
 * Frees an AVFrame.
 */
Jan Gerber's avatar
Jan Gerber committed
168
static void frame_dealloc(AVFrame *frame) {
169
    if (frame) {
Jan Gerber's avatar
Jan Gerber committed
170
        avpicture_free((AVPicture*)frame);
171
172
173
174
        av_free(frame);
    }
}

175
176
177
178
/**
 * initialize ff2theora with default values
 * @return ff2theora struct
 */
Jan Gerber's avatar
Jan Gerber committed
179
static ff2theora ff2theora_init() {
Jan Gerber's avatar
Jan Gerber committed
180
    ff2theora this = calloc (1, sizeof (*this));
Jan Gerber's avatar
Jan Gerber committed
181
    if (this != NULL) {
Jan Gerber's avatar
Jan Gerber committed
182
        this->disable_audio=0;
183
        this->disable_video=0;
184
        this->included_subtitles=INCSUB_TEXT;
185
        this->disable_metadata=0;
186
        this->disable_oshash=0;
187
        this->no_upscaling=0;
Jan Gerber's avatar
Jan Gerber committed
188
189
190
191
192
193
        this->video_index = -1;
        this->audio_index = -1;
        this->start_time=0;
        this->end_time=0; /* 0 denotes no end time set */

        // audio
194
195
        this->sample_rate = -1;  // samplerate hmhmhm
        this->channels = -1;
196
        this->audio_quality = 1.00;// audio quality 1
Jan Gerber's avatar
Jan Gerber committed
197
        this->audio_bitrate=0;
198
        this->audiostream = -1;
Jan Gerber's avatar
Jan Gerber committed
199

Jan Gerber's avatar
Jan Gerber committed
200
        // video
Jan Gerber's avatar
Jan Gerber committed
201
        this->videostream = -1;
Jan Gerber's avatar
Jan Gerber committed
202
203
        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
204
        this->video_quality=-1; // defaults set later
Jan Gerber's avatar
Jan Gerber committed
205
        this->video_bitrate=0;
Jan Gerber's avatar
Jan Gerber committed
206
        this->keyint=0;
207
208
        this->force_input_fps.num = -1;
        this->force_input_fps.den = 1;
209
        this->sync = 1;
Jan Gerber's avatar
Jan Gerber committed
210
211
        this->aspect_numerator=0;
        this->aspect_denominator=0;
212
213
214
215
216
        this->colorspace = TH_CS_UNSPECIFIED;
        this->frame_aspect.num=0;
        this->frame_aspect.den=1;
        this->pixel_aspect.num=0;
        this->pixel_aspect.den=0;
217
218
        this->max_x=-1;
        this->max_y=-1;
Jan Gerber's avatar
Jan Gerber committed
219
        this->deinterlace=0; // auto by default, if input is flaged as interlaced it will deinterlace.
Jan Gerber's avatar
Jan Gerber committed
220
221
        this->soft_target=0;
        this->buf_delay=-1;
Jan Gerber's avatar
Jan Gerber committed
222
        this->vhook=0;
223
224
        this->framerate_new.num = -1;
        this->framerate_new.den = 1;
Jan Gerber's avatar
Jan Gerber committed
225

Jan Gerber's avatar
Jan Gerber committed
226
227
228
229
        this->frame_topBand=0;
        this->frame_bottomBand=0;
        this->frame_leftBand=0;
        this->frame_rightBand=0;
Jan Gerber's avatar
Jan Gerber committed
230

231
232
        this->n_kate_streams=0;
        this->kate_streams=NULL;
233
        this->ignore_non_utf8 = 0;
234

235
        this->pix_fmt = PIX_FMT_YUV420P;
236
237
238
239
240
241
242
243
244

        // 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;
245
246
        this->sws_colorspace_ctx = NULL;
        this->sws_scale_ctx = NULL;
247
248

        this->resize_method = -1;
Jan Gerber's avatar
Jan Gerber committed
249
250
    }
    return this;
251
252
}

253
254
255
256
257
258
259
260
261
// 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
262

263
264
265
266
267
268
269
    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
270
    fprintf(stderr, "  Video correction: gamma=%g, contrast=%g, brightness=%g\n", g, c, b);
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

    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
298
    fprintf(stderr, "  Color correction: saturation=%g\n", s);
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

    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
328
static void prepare_ycbcr_buffer(ff2theora this, th_ycbcr_buffer ycbcr, AVFrame *frame) {
Jan Gerber's avatar
Jan Gerber committed
329
    /* pysical pages */
Jan Gerber's avatar
Jan Gerber committed
330
331
332
333
    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
334

Jan Gerber's avatar
Jan Gerber committed
335
336
337
338
    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
339

Jan Gerber's avatar
Jan Gerber committed
340
341
342
343
    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];
344

345
    if (this->y_lut_used) {
Jan Gerber's avatar
Jan Gerber committed
346
        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
347
    }
348
    if (this->uv_lut_used) {
Jan Gerber's avatar
Jan Gerber committed
349
350
        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
351
352
353
    }
}

354
static const char *find_category_for_subtitle_stream (ff2theora this, int idx, int included_subtitles)
355
356
{
  AVCodecContext *enc = this->context->streams[idx]->codec;
357
  if (enc->codec_type != AVMEDIA_TYPE_SUBTITLE) return 0;
358
  switch (enc->codec_id) {
Jan Gerber's avatar
Jan Gerber committed
359
360
361
    case AV_CODEC_ID_TEXT:
    case AV_CODEC_ID_SSA:
    case AV_CODEC_ID_MOV_TEXT:
362
363
364
365
      if (included_subtitles & INCSUB_TEXT)
        return "SUB";
      else
        return NULL;
Jan Gerber's avatar
Jan Gerber committed
366
    case AV_CODEC_ID_DVD_SUBTITLE:
367
368
369
370
      if (included_subtitles & INCSUB_SPU)
        return "K-SPU";
      else
        return NULL;
371
    default:
372
      return NULL;
373
  }
374
375
376
377
378
379
  return NULL;
}

static int is_supported_subtitle_stream(ff2theora this, int idx, int included_subtitles)
{
  return find_category_for_subtitle_stream(this, idx, included_subtitles) != NULL;
380
381
}

382
static char *get_raw_text_from_ssa(const char *ssa)
383
{
384
  int n,intag,inescape;
385
  char *multiblock = NULL, *realloced_mb;
386
387
388
389
  char *allocated;
  const char *dialogue, *ptr, *tag_start;

  if (!ssa) return NULL;
390
391
392
393
394
395
396
397
398
399
400

  /* 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;
401
    }
402
403
404
405
406
407
408
409
410
411
    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;
412
      }
413
414
415
416
417
      else if (*ptr=='}') {
        --intag;
        if (intag == 0) {
          /* tag parsing - none for now */
        }
418
      }
419
420
421
422
423
424
425
      else if (!intag) {
        if (inescape) {
          if (*ptr == 'N' || *ptr == 'n')
            allocated[n++] = '\n';
          else if (*ptr == 'h')
            allocated[n++] = ' ';
          inescape=0;
426
427
        }
        else {
428
429
430
431
432
433
          if (*ptr=='\\') {
            inescape=1;
          }
          else {
            allocated[n++]=*ptr;
          }
434
435
436
        }
      }
    }
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
    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);
    }
455
456
  }

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

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

    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)
{
  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;
}

490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
static void extra_info_from_ssa(AVPacket *pkt, const char **utf8, size_t *utf8len, char **allocated_utf8, float *duration)
{
  char *dupe;

  *allocated_utf8 = NULL;
  dupe = malloc(pkt->size+1); // not zero terminated, so make it so
  if (dupe) {
    memcpy(dupe, pkt->data, pkt->size);
    dupe[pkt->size] = 0;
    *duration = get_duration_from_ssa(dupe);
    *allocated_utf8 = get_raw_text_from_ssa(dupe);
    if (*allocated_utf8) {
      if (*allocated_utf8 == dupe) {
        *allocated_utf8 = NULL;
      }
      else {
        *utf8 = *allocated_utf8;
        *utf8len = strlen(*utf8);
      }
    }
    free(dupe);
  }
}

514
515
static const char *find_language_for_subtitle_stream(const AVStream *s)
{
Jan Gerber's avatar
Jan Gerber committed
516
  AVDictionaryEntry *language = av_dict_get(s->metadata, "language", NULL, 0);
517
  const char *lang=find_iso639_1(language->value);
518
  if (!lang) {
519
520
    fprintf(stderr, "WARNING - unrecognized ISO 639-2 language code: %s\n",
                    language->value);
521
522
523
524
  }
  return lang;
}

525
void ff2theora_output(ff2theora this) {
526
    unsigned int i;
Jan Gerber's avatar
Jan Gerber committed
527
528
    AVCodecContext *aenc = NULL;
    AVCodecContext *venc = NULL;
Jan Gerber's avatar
Jan Gerber committed
529
    int venc_pix_fmt = 0;
Jan Gerber's avatar
Jan Gerber committed
530
531
532
533
    AVStream *astream = NULL;
    AVStream *vstream = NULL;
    AVCodec *acodec = NULL;
    AVCodec *vcodec = NULL;
Jan Gerber's avatar
Jan Gerber committed
534
535
    pp_mode *ppMode = NULL;
    pp_context *ppContext = NULL;
536
    int sws_flags = this->resize_method;
Jan Gerber's avatar
Jan Gerber committed
537
    float frame_aspect = 0;
538
    double fps = 0.0;
539
    AVRational vstream_fps;
Jan Gerber's avatar
Jan Gerber committed
540
    int display_width = -1, display_height = -1;
541
542
    char *subtitles_enabled = (char*)alloca(this->context->nb_streams);
    char *subtitles_opened = (char*)alloca(this->context->nb_streams);
543
    int synced = this->start_time == 0.0;
544
    AVRational display_aspect_ratio, sample_aspect_ratio;
Jan Gerber's avatar
Jan Gerber committed
545

546
547
548
549
550
    struct SwrContext *swr_ctx;
    uint8_t **dst_audio_data = NULL;
    int dst_linesize;
    int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;

Jan Gerber's avatar
Jan Gerber committed
551
    if (this->audiostream >= 0 && this->context->nb_streams > this->audiostream) {
552
        AVCodecContext *enc = this->context->streams[this->audiostream]->codec;
553
        if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
554
            this->audio_index = this->audiostream;
555
            fprintf(stderr, "  Using stream #0.%d as audio input\n",this->audio_index);
556
557
        }
        else {
558
            fprintf(stderr, "  The selected stream is not audio, falling back to automatic selection\n");
559
560
        }
    }
Jan Gerber's avatar
Jan Gerber committed
561
562
    if (this->videostream >= 0 && this->context->nb_streams > this->videostream) {
        AVCodecContext *enc = this->context->streams[this->videostream]->codec;
563
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
Jan Gerber's avatar
Jan Gerber committed
564
565
566
567
568
569
570
            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
571

Jan Gerber's avatar
Jan Gerber committed
572
    for (i = 0; i < this->context->nb_streams; i++) {
573
        AVCodecContext *enc = this->context->streams[i]->codec;
Jan Gerber's avatar
Jan Gerber committed
574
        switch (enc->codec_type) {
575
            case AVMEDIA_TYPE_VIDEO:
Jan Gerber's avatar
Jan Gerber committed
576
                if (this->video_index < 0 && !this->disable_video)
577
578
                    this->video_index = i;
                break;
579
            case AVMEDIA_TYPE_AUDIO:
580
581
582
583
584
                if (this->audio_index < 0 && !this->disable_audio)
                    this->audio_index = i;
                break;
            default:
                break;
Jan Gerber's avatar
Jan Gerber committed
585
586
587
        }
    }

Jan Gerber's avatar
Jan Gerber committed
588
    if (this->video_index >= 0) {
Jan Gerber's avatar
Jan Gerber committed
589
        vstream = this->context->streams[this->video_index];
Jan Gerber's avatar
Jan Gerber committed
590
        venc = vstream->codec;
Jan Gerber's avatar
Jan Gerber committed
591
        vcodec = avcodec_find_decoder (venc->codec_id);
Jan Gerber's avatar
Jan Gerber committed
592

593
594
        display_width = venc->width;
        display_height = venc->height;
595
        venc_pix_fmt =  venc->pix_fmt;
Jan Gerber's avatar
Jan Gerber committed
596

597
598
599
        if (this->force_input_fps.num > 0)
            vstream_fps = this->force_input_fps;
        else if (vstream->time_base.den && vstream->time_base.num
600
601
                                  && av_q2d(vstream->time_base) > 0.001) {
            vstream_fps.num = vstream->time_base.den;
602
            vstream_fps.den = vstream->time_base.num;
603
        } else {
604
605
            vstream_fps.num = venc->time_base.den;
            vstream_fps.den = venc->time_base.num * venc->ticks_per_frame;
606
        }
607
608
        if (av_q2d(vstream->r_frame_rate) < av_q2d(vstream_fps)) {
            vstream_fps = vstream->r_frame_rate;
609
        }
610
        this->fps = fps = av_q2d(vstream_fps);
Jan Gerber's avatar
Jan Gerber committed
611

612
        venc->thread_count = 1;
Jan Gerber's avatar
Jan Gerber committed
613
        if (vcodec == NULL || avcodec_open2 (venc, vcodec, NULL) < 0) {
Jan Gerber's avatar
Jan Gerber committed
614
            this->video_index = -1;
615
        }
Jan Gerber's avatar
Jan Gerber committed
616
        this->fps = fps;
617
618
619
620
621
622
623
#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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
        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
642
            av_cmp_q(vstream->sample_aspect_ratio, venc->sample_aspect_ratio)) {
643
            sample_aspect_ratio = vstream->sample_aspect_ratio;
644
        } else {
645
            sample_aspect_ratio = venc->sample_aspect_ratio;
646
647
648
649
650
651
652
653
        }
        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);
        }

Jan Gerber's avatar
Jan Gerber committed
654
655
        if (this->preset == V2V_PRESET_PREVIEW) {
            if (abs(this->fps-30)<1 && (display_width!=NTSC_HALF_WIDTH || display_height!=NTSC_HALF_HEIGHT) ) {
656
657
                this->picture_width=NTSC_HALF_WIDTH;
                this->picture_height=NTSC_HALF_HEIGHT;
Jan Gerber's avatar
Jan Gerber committed
658
            }
Jan Gerber's avatar
Jan Gerber committed
659
660
661
662
            else {
                this->picture_width=PAL_HALF_WIDTH;
                this->picture_height=PAL_HALF_HEIGHT;
            }
Jan Gerber's avatar
Jan Gerber committed
663
        }
Jan Gerber's avatar
Jan Gerber committed
664
665
        else if (this->preset == V2V_PRESET_PRO) {
            if (abs(this->fps-30)<1 && (display_width!=NTSC_FULL_WIDTH || display_height!=NTSC_FULL_HEIGHT) ) {
666
667
                this->picture_width=NTSC_FULL_WIDTH;
                this->picture_height=NTSC_FULL_HEIGHT;
Jan Gerber's avatar
Jan Gerber committed
668
            }
Jan Gerber's avatar
Jan Gerber committed
669
670
671
672
            else {
                this->picture_width=PAL_FULL_WIDTH;
                this->picture_height=PAL_FULL_HEIGHT;
            }
Jan Gerber's avatar
Jan Gerber committed
673
        }
Jan Gerber's avatar
Jan Gerber committed
674
        else if (this->preset == V2V_PRESET_PADMA) {
675
676
            int width=display_width-this->frame_leftBand-this->frame_rightBand;
            int height=display_height-this->frame_topBand-this->frame_bottomBand;
677
678
679
680
            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
681
            }
682
683
684
685
686
            if (this->frame_aspect.num == 0) {
                this->frame_aspect.num = width;
                this->frame_aspect.den = height;
            }
            if (av_q2d(this->frame_aspect) <= 1.5) {
Jan Gerber's avatar
Jan Gerber committed
687
688
689
690
691
692
693
694
695
                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
696
697
            }
            else {
Jan Gerber's avatar
Jan Gerber committed
698
699
700
701
702
703
704
705
706
                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
707
            }
708
709
            this->frame_aspect.num = this->picture_width;
            this->frame_aspect.den = this->picture_height;
Jan Gerber's avatar
Jan Gerber committed
710
        }
Jan Gerber's avatar
Jan Gerber committed
711
712
713
        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;
714
715
716
717
            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
718
            }
719
720
721
722
            if (this->frame_aspect.num == 0) {
                this->frame_aspect.num = width;
                this->frame_aspect.den = height;
            }
723
724
725
726

            this->picture_width=128;
            this->picture_height=128/av_q2d(this->frame_aspect);

Jan Gerber's avatar
Jan Gerber committed
727
728
            this->frame_aspect.num = this->picture_width;
            this->frame_aspect.den = this->picture_height;
Jan Gerber's avatar
Jan Gerber committed
729
730
731
732
        }
        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;
733
734
735
736
            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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
            }
            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;
                }
            }
Jan Gerber's avatar
Jan Gerber committed
760
761
            this->frame_aspect.num = this->picture_width;
            this->frame_aspect.den = this->picture_height;
Jan Gerber's avatar
Jan Gerber committed
762
        }
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
        //so frame_aspect is set on the commandline

        if (this->frame_aspect.num != 0) {
            if (this->picture_height) {
                this->aspect_numerator = this->frame_aspect.num*this->picture_height;
                this->aspect_denominator = this->frame_aspect.den*this->picture_width;
            }
            else{
                this->aspect_numerator = this->frame_aspect.num*display_height;
                this->aspect_denominator = this->frame_aspect.den*display_width;
            }
            av_reduce(&this->aspect_numerator,&this->aspect_denominator,
                       this->aspect_numerator,this->aspect_denominator,
                       1024*1024);
            frame_aspect=av_q2d(this->frame_aspect);
        }
779
780
781
        if ((this->picture_width && !this->picture_height) ||
            (this->picture_height && !this->picture_width) ||
            this->max_x > 0) {
782

Jan Gerber's avatar
Jan Gerber committed
783
784
            int width = display_width-this->frame_leftBand-this->frame_rightBand;
            int height = display_height-this->frame_topBand-this->frame_bottomBand;
785
786
787
788
            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
789
            }
790
791
792
793
            if (this->frame_aspect.num == 0) {
                this->frame_aspect.num = width;
                this->frame_aspect.den = height;
            }
794
795
796

            if (this->picture_width && !this->picture_height) {
                this->picture_height = this->picture_width / av_q2d(this->frame_aspect);
Jan Gerber's avatar
Jan Gerber committed
797
                this->picture_height = this->picture_height + this->picture_height%2;
798
799
800
            }
            else if (this->picture_height && !this->picture_width) {
                this->picture_width = this->picture_height * av_q2d(this->frame_aspect);
Jan Gerber's avatar
Jan Gerber committed
801
802
                this->picture_width = this->picture_width + this->picture_width%2;
            }
803
804
805

            if (this->max_x > 0) {
                if (width > height &&
Jan Gerber's avatar
Jan Gerber committed
806
807
                    this->max_x/av_q2d(this->frame_aspect) <= this->max_y) {
                    this->picture_width = this->max_x;
808
809
810
                    this->picture_height = this->max_x / av_q2d(this->frame_aspect);
                    this->picture_height = this->picture_height + this->picture_height%2;
                } else {
Jan Gerber's avatar
Jan Gerber committed
811
812
                    this->picture_height = this->max_y;
                    this->picture_width = this->max_y * av_q2d(this->frame_aspect);
813
814
815
                    this->picture_width = this->picture_width + this->picture_width%2;
                }
            }
816
        }
817

Jan Gerber's avatar
Jan Gerber committed
818
        if (this->no_upscaling) {
819
820
            if (this->picture_height && this->picture_height > display_height) {
                this->picture_width = display_height * display_aspect_ratio.num / display_aspect_ratio.den;
821
                this->picture_height = display_height;
822
            }
823
824
825
826
            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;
            }
827
828
            if (this->fps < av_q2d(this->framerate_new))
                this->framerate_new = vstream_fps;
829
830
        }

831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
        if (info.twopass!=3 || info.passno==1) {
            if (sample_aspect_ratio.num!=0 && this->frame_aspect.num==0) {

                // just use the ratio from the input
                this->aspect_numerator=sample_aspect_ratio.num;
                this->aspect_denominator=sample_aspect_ratio.den;
                // or we use ratio for the output
                if (this->picture_height) {
                    int width=display_width-this->frame_leftBand-this->frame_rightBand;
                    int height=display_height-this->frame_topBand-this->frame_bottomBand;
                    av_reduce(&this->aspect_numerator,&this->aspect_denominator,
                    vstream->sample_aspect_ratio.num*width*this->picture_height,
                    vstream->sample_aspect_ratio.den*height*this->picture_width,10000);
                    frame_aspect=(float)(this->aspect_numerator*this->picture_width)/
                                    (this->aspect_denominator*this->picture_height);
                }
                else{
                    frame_aspect=(float)(this->aspect_numerator*display_width)/
                                    (this->aspect_denominator*display_height);
                }
Jan Gerber's avatar
Jan Gerber committed
851
852
            }
        }
853

854
855
856
857
858
859
860
861
862
863
864
865
866
        //pixel aspect ratio set, use that
        if (this->pixel_aspect.num>0) {
            this->aspect_numerator = this->pixel_aspect.num;
            this->aspect_denominator = this->pixel_aspect.den;
            if (this->picture_height) {
                frame_aspect=(float)(this->aspect_numerator*this->picture_width)/
                                (this->aspect_denominator*this->picture_height);
            }
            else{
                frame_aspect=(float)(this->aspect_numerator*display_width)/
                                (this->aspect_denominator*display_height);
            }
        }
867
        if (!(info.twopass==3 && info.passno==2) && !info.frontend && this->aspect_denominator && frame_aspect) {
868
            fprintf(stderr, "  Pixel Aspect Ratio: %.2f/1 ",(float)this->aspect_numerator/this->aspect_denominator);
Jan Gerber's avatar
Jan Gerber committed
869
            fprintf(stderr, "  Frame Aspect Ratio: %.2f/1\n", frame_aspect);
Jan Gerber's avatar
Jan Gerber committed
870
871
        }

Jan Gerber's avatar
Jan Gerber committed
872
873
        if (!(info.twopass==3 && info.passno==2) && !info.frontend &&
            this->deinterlace==1)
874
            fprintf(stderr, "  Deinterlace: on\n");
Jan Gerber's avatar
Jan Gerber committed
875
876
877
        if (!(info.twopass==3 && info.passno==2) && !info.frontend &&
            this->deinterlace==-1)
            fprintf(stderr, "  Deinterlace: off\n");
878
879

        if (strcmp(this->pp_mode, "")) {
Jan Gerber's avatar
Jan Gerber committed
880
881
            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);
882
            if(!(info.twopass==3 && info.passno==2) && !info.frontend)
Jan Gerber's avatar
Jan Gerber committed
883
                fprintf(stderr, "  Postprocessing: %s\n", this->pp_mode);
884
885
        }

886
887
888
889
890
        if (venc->color_primaries == AVCOL_PRI_BT470M)
            this->colorspace = TH_CS_ITU_REC_470M;
        else if (venc->color_primaries == AVCOL_PRI_BT470BG)
            this->colorspace = TH_CS_ITU_REC_470BG;

Jan Gerber's avatar
Jan Gerber committed
891
        if (!this->picture_width)
892
            this->picture_width = display_width;
Jan Gerber's avatar
Jan Gerber committed
893
        if (!this->picture_height)
894
            this->picture_height = display_height;
895

896
897
898
899
        /* 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
900

Jan Gerber's avatar
Jan Gerber committed
901
902
        /*Force the offsets to be even so that chroma samples line up like we
           expect.*/
903
904
        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
905

906
        //Bicubic  (best for upscaling),
907
908
909
910
911
912
913
        if (sws_flags < 0) {
          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;
          }
914
915
        }

Jan Gerber's avatar
Jan Gerber committed
916
        if (this->frame_width > 0 || this->frame_height > 0) {
Jan Gerber's avatar
Jan Gerber committed
917
            this->sws_colorspace_ctx = sws_getContext(
918
                            display_width, display_height, venc_pix_fmt,
Jan Gerber's avatar
Jan Gerber committed
919
920
                            display_width, display_height, this->pix_fmt,
                            sws_flags, NULL, NULL, NULL
Jan Gerber's avatar
Jan Gerber committed
921
922
            );
            this->sws_scale_ctx = sws_getContext(
Jan Gerber's avatar
Jan Gerber committed
923
924
925
926
927
                        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
928
            );
Jan Gerber's avatar
Jan Gerber committed
929
            if (!info.frontend && !(info.twopass==3 && info.passno==2)) {
Jan Gerber's avatar
Jan Gerber committed
930
931
932
933
934
                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
935
936
937
938
939
940
941
942
943
944
                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
945
946
            }
        }
947

948
        lut_init(this);
Jan Gerber's avatar
Jan Gerber committed
949
    }
950
    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
951
        fprintf(stderr, "  Resample Framerate: %0.3f => %0.3f\n",
952
                        this->fps, av_q2d(this->framerate_new));
953
    }
Jan Gerber's avatar
Jan Gerber committed
954
    if (this->audio_index >= 0) {
Jan Gerber's avatar
Jan Gerber committed
955
        astream = this->context->streams[this->audio_index];
956
        aenc = this->context->streams[this->audio_index]->codec;
Jan Gerber's avatar
Jan Gerber committed
957
        acodec = avcodec_find_decoder (aenc->codec_id);
958
        int sample_rate = aenc->sample_rate;
959
        if (this->channels < 1) {
960
            this->channels = aenc->channels;
961
        }
Jan Gerber's avatar
Jan Gerber committed
962
        if (this->sample_rate==-1) {
963
964
            this->sample_rate = aenc->sample_rate;
        }
Jan Gerber's avatar
Jan Gerber committed
965

Jan Gerber's avatar
Jan Gerber committed
966
967
        if (this->no_upscaling) {
            if (this->sample_rate > aenc->sample_rate)
968
                this->sample_rate = aenc->sample_rate;
Jan Gerber's avatar
Jan Gerber committed
969
            if (this->channels > aenc->channels)
970
971
                this->channels = aenc->channels;
        }
972
        aenc->thread_count = 1;
Jan Gerber's avatar
Jan Gerber committed
973
        if (acodec != NULL && avcodec_open2 (aenc, acodec, NULL) >= 0) {
974
            if (this->sample_rate != sample_rate
975
                || this->channels != aenc->channels
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
                || aenc->sample_fmt != AV_SAMPLE_FMT_FLTP) {
                swr_ctx = swr_alloc();
                /* set options */
                if (aenc->channel_layout) {
                    av_opt_set_int(swr_ctx, "in_channel_layout",    aenc->channel_layout, 0);
                } else {
                    av_opt_set_int(swr_ctx, "in_channel_layout", av_get_default_channel_layout(aenc->channels), 0);
                }
                av_opt_set_int(swr_ctx, "in_sample_rate",       aenc->sample_rate, 0);
                av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", aenc->sample_fmt, 0);

                av_opt_set_int(swr_ctx, "out_channel_layout", av_get_default_channel_layout(this->channels), 0);
                av_opt_set_int(swr_ctx, "out_sample_rate",       this->sample_rate, 0);
                av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);

                /* initialize the resampling context */
                if (swr_init(swr_ctx) < 0) {
                    fprintf(stderr, "Failed to initialize the resampling context\n");
                    exit(1);
995
                }
996
997
998
999
1000
1001
1002
1003
1004
1005

                max_dst_nb_samples = dst_nb_samples =
                    av_rescale_rnd(src_nb_samples, this->sample_rate, sample_rate, AV_ROUND_UP);

                if (av_samples_alloc_array_and_samples(&dst_audio_data, &dst_linesize, this->channels,
                                                         dst_nb_samples, AV_SAMPLE_FMT_FLTP, 0) < 0) {
                    fprintf(stderr, "Could not allocate destination samples\n");
                    exit(1);
                }

1006
1007
                if (!info.frontend && this->sample_rate!=sample_rate)
                    fprintf(stderr, "  Resample: %dHz => %dHz\n", sample_rate,this->sample_rate);
Jan Gerber's avatar
Jan Gerber committed
1008
                if (!info.frontend && this->channels!=aenc->channels)
1009
                    fprintf(stderr, "  Channels: %d => %d\n",aenc->channels,this->channels);
Jan Gerber's avatar
Jan Gerber committed
1010
1011
            }
            else{
1012
                swr_ctx = NULL;
Jan Gerber's avatar
Jan Gerber committed
1013
1014
1015
1016
1017
1018
            }
        }
        else{
            this->audio_index = -1;
        }
    }
1019

1020
1021
1022
1023
    if (info.passno != 1) {
      for (i = 0; i < this->context->nb_streams; i++) {
        subtitles_enabled[i] = 0;
        subtitles_opened[i] = 0;
1024
#ifdef HAVE_KATE
1025
        if (this->included_subtitles) {
1026
1027
          AVStream *stream = this->context->streams[i];
          AVCodecContext *enc = stream->codec;
1028
          const char *category;
1029
          if (enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1030
            AVCodec *codec = avcodec_find_decoder (enc->codec_id);
Jan Gerber's avatar
Jan Gerber committed
1031
            if (codec && avcodec_open2 (enc, codec, NULL) >= 0) {
1032
1033
              subtitles_opened[i] = 1;
            }
1034
1035
            category = find_category_for_subtitle_stream(this, i, this->included_subtitles);
            if (category) {
1036
              subtitles_enabled[i] = 1;
1037
              add_subtitles_stream(this, i, find_language_for_subtitle_stream(stream), category);
1038
1039
            }
            else if(!info.frontend) {
1040
              fprintf(stderr,"Subtitle stream %d, ignored\n", i);
1041
            }
1042
1043
          }
        }
1044
#endif
1045
      }
1046
1047
    }

1048
#ifdef HAVE_KATE
1049
1050
    if (info.passno != 1) {
      for (i=0; i<this->n_kate_streams; ++i) {
1051
1052
        ff2theora_kate_stream *ks=this->kate_streams+i;
        if (ks->stream_index >= 0) {