ffmpeg2theora.c 87.5 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

38
39
40
41
#include "theora/theora.h"
#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
enum {
Jan Gerber's avatar
Jan Gerber committed
53
54
55
56
57
58
    NULL_FLAG,
    DEINTERLACE_FLAG,
    OPTIMIZE_FLAG,
    SYNC_FLAG,
    NOAUDIO_FLAG,
    NOVIDEO_FLAG,
59
    NOSUBTITLES_FLAG,
Jan Gerber's avatar
Jan Gerber committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    NOUPSCALING_FLAG,
    CROPTOP_FLAG,
    CROPBOTTOM_FLAG,
    CROPRIGHT_FLAG,
    CROPLEFT_FLAG,
    ASPECT_FLAG,
    MAXSIZE_FLAG,
    INPUTFPS_FLAG,
    AUDIOSTREAM_FLAG,
    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,
79
80
    NOSKELETON,
    INFO_FLAG
81
} F2T_FLAGS;
Jan Gerber's avatar
Jan Gerber committed
82

Jan Gerber's avatar
Jan Gerber committed
83
enum {
Jan Gerber's avatar
Jan Gerber committed
84
85
86
87
88
89
    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
90
} F2T_PRESETS;
Jan Gerber's avatar
Jan Gerber committed
91

92

93
94
95
96
97
98
99
100
101
#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
102

Jan Gerber's avatar
Jan Gerber committed
103
104
105

static int sws_flags = SWS_BICUBIC;

Jan Gerber's avatar
Jan Gerber committed
106
oggmux_info info;
107

108
109
static int using_stdin = 0;

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

112

113
/**
Jan Gerber's avatar
Jan Gerber committed
114
 * Allocate and initialise an AVFrame.
115
 */
Jan Gerber's avatar
Jan Gerber committed
116
static AVFrame *frame_alloc(int pix_fmt, int width, int height) {
Jan Gerber's avatar
Jan Gerber committed
117
118
119
120
    AVFrame *picture;
    uint8_t *picture_buf;
    int size;

Jan Gerber's avatar
Jan Gerber committed
121
    picture = avcodec_alloc_frame();
Jan Gerber's avatar
Jan Gerber committed
122
123
124
125
    if (!picture)
        return NULL;
    size = avpicture_get_size (pix_fmt, width, height);
    picture_buf = av_malloc (size);
Jan Gerber's avatar
Jan Gerber committed
126
    if (!picture_buf) {
Jan Gerber's avatar
Jan Gerber committed
127
128
129
        av_free (picture);
        return NULL;
    }
Jan Gerber's avatar
Jan Gerber committed
130
    avpicture_fill((AVPicture *) picture, picture_buf, pix_fmt, width, height);
Jan Gerber's avatar
Jan Gerber committed
131
    return picture;
132
133
}

134
135
136
/**
 * Frees an AVFrame.
 */
Jan Gerber's avatar
Jan Gerber committed
137
static void frame_dealloc(AVFrame *frame) {
138
    if (frame) {
Jan Gerber's avatar
Jan Gerber committed
139
        avpicture_free((AVPicture*)frame);
140
141
142
143
        av_free(frame);
    }
}

144
145
146
147
/**
 * initialize ff2theora with default values
 * @return ff2theora struct
 */
Jan Gerber's avatar
Jan Gerber committed
148
static ff2theora ff2theora_init() {
Jan Gerber's avatar
Jan Gerber committed
149
    ff2theora this = calloc (1, sizeof (*this));
Jan Gerber's avatar
Jan Gerber committed
150
    if (this != NULL) {
Jan Gerber's avatar
Jan Gerber committed
151
        this->disable_audio=0;
152
        this->disable_video=0;
153
        this->disable_subtitles=0;
154
        this->no_upscaling=0;
Jan Gerber's avatar
Jan Gerber committed
155
156
157
158
159
160
        this->video_index = -1;
        this->audio_index = -1;
        this->start_time=0;
        this->end_time=0; /* 0 denotes no end time set */

        // audio
161
162
        this->sample_rate = -1;  // samplerate hmhmhm
        this->channels = -1;
163
        this->audio_quality = 1.00;// audio quality 1
Jan Gerber's avatar
Jan Gerber committed
164
        this->audio_bitrate=0;
165
        this->audiostream = -1;
Jan Gerber's avatar
Jan Gerber committed
166

Jan Gerber's avatar
Jan Gerber committed
167
168
169
        // video
        this->picture_width=0;      // set to 0 to not resize the output
        this->picture_height=0;      // set to 0 to not resize the output
170
        this->video_quality=rint(5*6.3); // video quality 5
Jan Gerber's avatar
Jan Gerber committed
171
        this->video_bitrate=0;
Jan Gerber's avatar
Jan Gerber committed
172
        this->sharpness=0;
173
        this->keyint=64;
174
175
        this->force_input_fps.num = -1;
        this->force_input_fps.den = 1;
176
        this->sync=0;
Jan Gerber's avatar
Jan Gerber committed
177
178
179
        this->aspect_numerator=0;
        this->aspect_denominator=0;
        this->frame_aspect=0;
180
        this->max_size=-1;
Jan Gerber's avatar
Jan Gerber committed
181
        this->deinterlace=0; // auto by default, if input is flaged as interlaced it will deinterlace.
Jan Gerber's avatar
Jan Gerber committed
182
        this->vhook=0;
183
184
        this->framerate_new.num = -1;
        this->framerate_new.den = 1;
Jan Gerber's avatar
Jan Gerber committed
185

Jan Gerber's avatar
Jan Gerber committed
186
187
188
189
        this->frame_topBand=0;
        this->frame_bottomBand=0;
        this->frame_leftBand=0;
        this->frame_rightBand=0;
Jan Gerber's avatar
Jan Gerber committed
190

191
192
        this->n_kate_streams=0;
        this->kate_streams=NULL;
193
        this->ignore_non_utf8 = 0;
194

Jan Gerber's avatar
Jan Gerber committed
195
        this->pix_fmt = PIX_FMT_YUV420P;
196
197
198
199
200
201
202
203
204

        // 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;
Jan Gerber's avatar
Jan Gerber committed
205
206
    }
    return this;
207
208
}

209
210
211
212
213
214
215
216
217
// 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
218

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    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;

    printf("  Video correction: gamma=%g, contrast=%g, brightness=%g\n", g, c, b);

    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;

    printf("  Color correction: saturation=%g\n", s);

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

284
static void prepare_yuv_buffer(ff2theora this, yuv_buffer *yuv, AVFrame *frame) {
285
    int i;
Jan Gerber's avatar
Jan Gerber committed
286
287
288
    /* pysical pages */
    yuv->y_width = this->frame_width;
    yuv->y_height = this->frame_height;
289
    yuv->y_stride = frame->linesize[0];
Jan Gerber's avatar
Jan Gerber committed
290
291
292

    yuv->uv_width = this->frame_width / 2;
    yuv->uv_height = this->frame_height / 2;
293
    yuv->uv_stride = frame->linesize[1];
Jan Gerber's avatar
Jan Gerber committed
294

295
296
297
    yuv->y = frame->data[0];
    yuv->u = frame->data[1];
    yuv->v = frame->data[2];
298

299
300
    if (this->y_lut_used) {
        lut_apply(this->y_lut, yuv->y, yuv->y, yuv->y_width, yuv->y_height, yuv->y_stride);
Jan Gerber's avatar
Jan Gerber committed
301
    }
302
303
304
    if (this->uv_lut_used) {
        lut_apply(this->uv_lut, yuv->u, yuv->u, yuv->uv_width, yuv->uv_height, yuv->uv_stride);
        lut_apply(this->uv_lut, yuv->v, yuv->v, yuv->uv_width, yuv->uv_height, yuv->uv_stride);
Jan Gerber's avatar
Jan Gerber committed
305
306
307
    }
}

308
309
310
311
312
313
314
315
316
317
318
319
320
321
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:
      return 1;
    default:
      return 0;
  }
  return 0;
}

322
static char *get_raw_text_from_ssa(const char *ssa)
323
{
324
325
326
327
328
329
330
331
332
  int n,intag,inescape;
  char *allocated;
  const char *dialogue, *ptr, *tag_start;

  if (!ssa) return NULL;
  dialogue=strstr(ssa, "Dialogue:");
  if (!dialogue) return NULL;

  ptr = dialogue;
333
334
335
336
337
  for (n=0;n<9;++n) {
    ptr=strchr(ptr,',');
    if (!ptr) return NULL;
    ++ptr;
  }
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  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) {
    if (*ptr=='{') {
      if (intag==0) tag_start = ptr;
      ++intag;
    }
    else if (*ptr=='}') {
      --intag;
      if (intag == 0) {
        /* tag parsing - none for now */
      }
    }
    else if (!intag) {
      if (inescape) {
        if (*ptr == 'N' || *ptr == 'n')
          allocated[n++] = '\n';
        else if (*ptr == 'h')
          allocated[n++] = ' ';
        inescape=0;
      }
      else {
        if (*ptr=='\\') {
          inescape=1;
        }
        else {
          allocated[n++]=*ptr;
        }
      }
    }
  }
  allocated[n]=0;

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

  return allocated;
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
}

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

424
void ff2theora_output(ff2theora this) {
425
    unsigned int i;
Jan Gerber's avatar
Jan Gerber committed
426
427
428
429
430
431
    AVCodecContext *aenc = NULL;
    AVCodecContext *venc = NULL;
    AVStream *astream = NULL;
    AVStream *vstream = NULL;
    AVCodec *acodec = NULL;
    AVCodec *vcodec = NULL;
432
433
    pp_mode_t *ppMode = NULL;
    pp_context_t *ppContext = NULL;
Jan Gerber's avatar
Jan Gerber committed
434
    float frame_aspect = 0;
435
    double fps = 0.0;
436
    AVRational vstream_fps;
437
    int display_width, display_height;
438
439
    char *subtitles_enabled = (char*)alloca(this->context->nb_streams);
    char *subtitles_opened = (char*)alloca(this->context->nb_streams);
440
    int synced = this->start_time == 0.0;
Jan Gerber's avatar
Jan Gerber committed
441

Jan Gerber's avatar
Jan Gerber committed
442
    if (this->audiostream >= 0 && this->context->nb_streams > this->audiostream) {
443
        AVCodecContext *enc = this->context->streams[this->audiostream]->codec;
444
445
        if (enc->codec_type == CODEC_TYPE_AUDIO) {
            this->audio_index = this->audiostream;
446
            fprintf(stderr, "  Using stream #0.%d as audio input\n",this->audio_index);
447
448
        }
        else {
449
            fprintf(stderr, "  The selected stream is not audio, falling back to automatic selection\n");
450
451
        }
    }
Jan Gerber's avatar
Jan Gerber committed
452

Jan Gerber's avatar
Jan Gerber committed
453
    for (i = 0; i < this->context->nb_streams; i++) {
454
        AVCodecContext *enc = this->context->streams[i]->codec;
Jan Gerber's avatar
Jan Gerber committed
455
        switch (enc->codec_type) {
456
            case CODEC_TYPE_VIDEO:
Jan Gerber's avatar
Jan Gerber committed
457
                if (this->video_index < 0 && !this->disable_video)
458
459
460
461
462
463
464
465
                    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
466
467
468
        }
    }

Jan Gerber's avatar
Jan Gerber committed
469
    if (this->video_index >= 0) {
Jan Gerber's avatar
Jan Gerber committed
470
        vstream = this->context->streams[this->video_index];
471
        venc = this->context->streams[this->video_index]->codec;
Jan Gerber's avatar
Jan Gerber committed
472
        vcodec = avcodec_find_decoder (venc->codec_id);
473
474
        display_width = venc->width;
        display_height = venc->height;
Jan Gerber's avatar
Jan Gerber committed
475

Jan Gerber's avatar
Jan Gerber committed
476
        if (vstream->time_base.den && vstream->time_base.num
477
478
479
480
481
482
483
484
485
486
                                  && av_q2d(vstream->time_base) > 0.001) {
            vstream_fps.num = vstream->time_base.den;
            vstream_fps.den =  vstream->time_base.num;
            fps = 1/av_q2d(vstream->time_base);
        } else {
            fps = (double) vstream->r_frame_rate.num / vstream->r_frame_rate.den;
            vstream_fps.num = vstream->r_frame_rate.num;
            vstream_fps.den = vstream->r_frame_rate.den;
        }

Jan Gerber's avatar
Jan Gerber committed
487
488
489
        if (fps > 10000)
            fps /= 1000;

Jan Gerber's avatar
Jan Gerber committed
490
        if (this->force_input_fps.num > 0)
491
            fps=(double)this->force_input_fps.num / this->force_input_fps.den;
492
        if (vcodec == NULL || avcodec_open (venc, vcodec) < 0) {
Jan Gerber's avatar
Jan Gerber committed
493
            this->video_index = -1;
494
        }
Jan Gerber's avatar
Jan Gerber committed
495
        this->fps = fps;
496

Jan Gerber's avatar
Jan Gerber committed
497
        if (this->picture_width && !this->picture_height) {
498
            this->picture_height = this->picture_width / ((double)display_width/display_height);
Jan Gerber's avatar
Jan Gerber committed
499
            this->picture_height = this->picture_height - this->picture_height%2;
500
        }
Jan Gerber's avatar
Jan Gerber committed
501
        if (this->picture_height && !this->picture_width) {
502
            this->picture_width = this->picture_height * ((double)display_width/display_height);
Jan Gerber's avatar
Jan Gerber committed
503
            this->picture_width = this->picture_width - this->picture_width%2;
504
        }
505

Jan Gerber's avatar
Jan Gerber committed
506
507
        if (this->preset == V2V_PRESET_PREVIEW) {
            if (abs(this->fps-30)<1 && (display_width!=NTSC_HALF_WIDTH || display_height!=NTSC_HALF_HEIGHT) ) {
508
509
                this->picture_width=NTSC_HALF_WIDTH;
                this->picture_height=NTSC_HALF_HEIGHT;
Jan Gerber's avatar
Jan Gerber committed
510
            }
Jan Gerber's avatar
Jan Gerber committed
511
512
513
514
            else {
                this->picture_width=PAL_HALF_WIDTH;
                this->picture_height=PAL_HALF_HEIGHT;
            }
Jan Gerber's avatar
Jan Gerber committed
515
        }
Jan Gerber's avatar
Jan Gerber committed
516
517
        else if (this->preset == V2V_PRESET_PRO) {
            if (abs(this->fps-30)<1 && (display_width!=NTSC_FULL_WIDTH || display_height!=NTSC_FULL_HEIGHT) ) {
518
519
                this->picture_width=NTSC_FULL_WIDTH;
                this->picture_height=NTSC_FULL_HEIGHT;
Jan Gerber's avatar
Jan Gerber committed
520
            }
Jan Gerber's avatar
Jan Gerber committed
521
522
523
524
            else {
                this->picture_width=PAL_FULL_WIDTH;
                this->picture_height=PAL_FULL_HEIGHT;
            }
Jan Gerber's avatar
Jan Gerber committed
525
        }
Jan Gerber's avatar
Jan Gerber committed
526
        else if (this->preset == V2V_PRESET_PADMA) {
527
528
            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
529
530
            if (venc->sample_aspect_ratio.den!=0 && venc->sample_aspect_ratio.num!=0) {
                height=((float)venc->sample_aspect_ratio.den/venc->sample_aspect_ratio.num) * height;
Jan Gerber's avatar
Jan Gerber committed
531
            }
Jan Gerber's avatar
Jan Gerber committed
532
533
534
535
536
537
538
539
540
541
542
543
            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
544
545
            }
            else {
Jan Gerber's avatar
Jan Gerber committed
546
547
548
549
550
551
552
553
554
                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
555
556
            }
        }
Jan Gerber's avatar
Jan Gerber committed
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
        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;
            if (venc->sample_aspect_ratio.den!=0 && venc->sample_aspect_ratio.num!=0) {
                height=((float)venc->sample_aspect_ratio.den/venc->sample_aspect_ratio.num) * height;
            }
            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;
            if (venc->sample_aspect_ratio.den!=0 && venc->sample_aspect_ratio.num!=0) {
                height=((float)venc->sample_aspect_ratio.den/venc->sample_aspect_ratio.num) * height;
            }
            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;
                }
            }
        }
        if (this->max_size > 0) {
            int width = display_width-this->frame_leftBand-this->frame_rightBand;
            int height = display_height-this->frame_topBand-this->frame_bottomBand;
            if (venc->sample_aspect_ratio.den!=0 && venc->sample_aspect_ratio.num!=0) {
                height = ((float)venc->sample_aspect_ratio.den/venc->sample_aspect_ratio.num) * height;
            }
            if (this->frame_aspect == 0)
                this->frame_aspect = (float)width/height;
            if (width > height) {
                this->picture_width = this->max_size;
                this->picture_height = this->max_size / this->frame_aspect;
                this->picture_height = this->picture_height + this->picture_height%2;
            } else {
                this->picture_height = this->max_size;
                this->picture_width = this->max_size * this->frame_aspect;
                this->picture_width = this->picture_width + this->picture_width%2;
            }
620
        }
621

Jan Gerber's avatar
Jan Gerber committed
622
623
        if (this->no_upscaling) {
            if (this->picture_width && this->picture_width > display_width) {
624
625
                this->picture_width = display_width;
                this->picture_height = display_height;
626
            }
Jan Gerber's avatar
Jan Gerber committed
627
            if (this->fps < (double)this->framerate_new.num / this->framerate_new.den) {
628
629
                this->framerate_new.num = vstream_fps.num;
                this->framerate_new.den = vstream_fps.den;
630
631
632
            }
        }

Jan Gerber's avatar
Jan Gerber committed
633
634
        if (this->picture_height==0 &&
            (this->frame_leftBand || this->frame_rightBand || this->frame_topBand || this->frame_bottomBand) ) {
635
            this->picture_height=display_height-
Jan Gerber's avatar
Jan Gerber committed
636
637
                    this->frame_topBand-this->frame_bottomBand;
        }
Jan Gerber's avatar
Jan Gerber committed
638
639
        if (this->picture_width==0 &&
            (this->frame_leftBand || this->frame_rightBand || this->frame_topBand || this->frame_bottomBand) ) {
640
            this->picture_width=display_width-
Jan Gerber's avatar
Jan Gerber committed
641
642
643
                    this->frame_leftBand-this->frame_rightBand;
        }
        //so frame_aspect is set on the commandline
Jan Gerber's avatar
Jan Gerber committed
644
645
        if (this->frame_aspect != 0) {
            if (this->picture_height) {
646
647
648
649
                this->aspect_numerator = 10000*this->frame_aspect*this->picture_height;
                this->aspect_denominator = 10000*this->picture_width;
            }
            else{
650
651
                this->aspect_numerator = 10000*this->frame_aspect*display_height;
                this->aspect_denominator = 10000*display_width;
652
653
654
            }
            av_reduce(&this->aspect_numerator,&this->aspect_denominator,this->aspect_numerator,this->aspect_denominator,10000);
            frame_aspect=this->frame_aspect;
Jan Gerber's avatar
Jan Gerber committed
655
        }
Jan Gerber's avatar
Jan Gerber committed
656
        if (venc->sample_aspect_ratio.num!=0 && this->frame_aspect==0) {
Jan Gerber's avatar
Jan Gerber committed
657
658
659
660
            // just use the ratio from the input
            this->aspect_numerator=venc->sample_aspect_ratio.num;
            this->aspect_denominator=venc->sample_aspect_ratio.den;
            // or we use ratio for the output
Jan Gerber's avatar
Jan Gerber committed
661
            if (this->picture_height) {
662
663
                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
664
665
666
667
668
669
670
                av_reduce(&this->aspect_numerator,&this->aspect_denominator,
                venc->sample_aspect_ratio.num*width*this->picture_height,
                venc->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{
671
672
                frame_aspect=(float)(this->aspect_numerator*display_width)/
                                (this->aspect_denominator*display_height);
Jan Gerber's avatar
Jan Gerber committed
673
674
            }
        }
Jan Gerber's avatar
Jan Gerber committed
675
676
677
678
679
        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
680
        }
Jan Gerber's avatar
Jan Gerber committed
681
        if (this->aspect_denominator && frame_aspect) {
682
683
            fprintf(stderr, "  Pixel Aspect Ratio: %.2f/1 ",(float)this->aspect_numerator/this->aspect_denominator);
            fprintf(stderr, "  Frame Aspect Ratio: %.2f/1\n",frame_aspect);
Jan Gerber's avatar
Jan Gerber committed
684
685
        }

686
        if (this->deinterlace==1)
687
            fprintf(stderr, "  Deinterlace: on\n");
688
689

        if (strcmp(this->pp_mode, "")) {
Jan Gerber's avatar
Jan Gerber committed
690
691
692
            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);
            fprintf(stderr, "  Postprocessing: %s\n", this->pp_mode);
693
694
        }

Jan Gerber's avatar
Jan Gerber committed
695
        if (!this->picture_width)
696
            this->picture_width = display_width;
Jan Gerber's avatar
Jan Gerber committed
697
        if (!this->picture_height)
698
            this->picture_height = display_height;
699

700
701
702
703
        /* 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
704

705

Jan Gerber's avatar
Jan Gerber committed
706
707
        this->frame_x_offset = 0;
        this->frame_y_offset = 0;
Jan Gerber's avatar
Jan Gerber committed
708

Jan Gerber's avatar
Jan Gerber committed
709
        if (this->frame_width > 0 || this->frame_height > 0) {
Jan Gerber's avatar
Jan Gerber committed
710
            this->sws_colorspace_ctx = sws_getContext(
Jan Gerber's avatar
Jan Gerber committed
711
712
713
                            display_width, display_height, venc->pix_fmt,
                            display_width, display_height, this->pix_fmt,
                            sws_flags, NULL, NULL, NULL
Jan Gerber's avatar
Jan Gerber committed
714
715
            );
            this->sws_scale_ctx = sws_getContext(
Jan Gerber's avatar
Jan Gerber committed
716
717
718
719
720
                        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
721
            );
722
            fprintf(stderr, "  Resize: %dx%d",display_width,display_height);
Jan Gerber's avatar
Jan Gerber committed
723
724
            if (this->frame_topBand || this->frame_bottomBand ||
                this->frame_leftBand || this->frame_rightBand) {
725
                fprintf(stderr, " => %dx%d",
726
727
                    display_width-this->frame_leftBand-this->frame_rightBand,
                    display_height-this->frame_topBand-this->frame_bottomBand);
Jan Gerber's avatar
Jan Gerber committed
728
            }
Jan Gerber's avatar
Jan Gerber committed
729
            if (this->picture_width != (display_width-this->frame_leftBand - this->frame_rightBand)
730
                || this->picture_height != (display_height-this->frame_topBand-this->frame_bottomBand))
731
732
                fprintf(stderr, " => %dx%d",this->picture_width, this->picture_height);
            fprintf(stderr, "\n");
Jan Gerber's avatar
Jan Gerber committed
733
        }
734

735
        lut_init(this);
Jan Gerber's avatar
Jan Gerber committed
736
    }
737
    if (this->framerate_new.num > 0 && this->fps != (double)this->framerate_new.num / this->framerate_new.den) {
738
        fprintf(stderr, "  Resample Framerate: %0.2f => %0.2f\n",
739
                        this->fps, (double)this->framerate_new.num / this->framerate_new.den);
740
    }
Jan Gerber's avatar
Jan Gerber committed
741
    if (this->audio_index >= 0) {
Jan Gerber's avatar
Jan Gerber committed
742
        astream = this->context->streams[this->audio_index];
743
        aenc = this->context->streams[this->audio_index]->codec;
Jan Gerber's avatar
Jan Gerber committed
744
        acodec = avcodec_find_decoder (aenc->codec_id);
745
        if (this->channels < 1) {
746
            this->channels = aenc->channels;
747
        }
Jan Gerber's avatar
Jan Gerber committed
748
        if (this->sample_rate==-1) {
749
750
            this->sample_rate = aenc->sample_rate;
        }
Jan Gerber's avatar
Jan Gerber committed
751

Jan Gerber's avatar
Jan Gerber committed
752
753
        if (this->no_upscaling) {
            if (this->sample_rate > aenc->sample_rate)
754
                this->sample_rate = aenc->sample_rate;
Jan Gerber's avatar
Jan Gerber committed
755
            if (this->channels > aenc->channels)
756
757
758
                this->channels = aenc->channels;
        }

Jan Gerber's avatar
Jan Gerber committed
759
760
        if (acodec != NULL && avcodec_open (aenc, acodec) >= 0) {
            if (this->sample_rate != aenc->sample_rate || this->channels != aenc->channels) {
761
762
763
764
765
766
767
768
                // 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
769
                if (this->sample_rate!=aenc->sample_rate)
770
                    fprintf(stderr, "  Resample: %dHz => %dHz\n",aenc->sample_rate,this->sample_rate);
Jan Gerber's avatar
Jan Gerber committed
771
                if (this->channels!=aenc->channels)
772
                    fprintf(stderr, "  Channels: %d => %d\n",aenc->channels,this->channels);
Jan Gerber's avatar
Jan Gerber committed
773
774
775
776
777
778
779
780
781
            }
            else{
                this->audio_resample_ctx=NULL;
            }
        }
        else{
            this->audio_index = -1;
        }
    }
782

783
784
785
    for (i = 0; i < this->context->nb_streams; i++) {
      subtitles_enabled[i] = 0;
      subtitles_opened[i] = 0;
786
#ifdef HAVE_KATE
787
788
789
790
791
792
793
794
      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;
          }
795
          if (enc->codec_id == CODEC_ID_TEXT || enc->codec_id == CODEC_ID_SSA /*|| subtitles_opened[i]*/) {
796
797
798
799
800
801
802
803
            subtitles_enabled[i] = 1;
            add_subtitles_stream(this, i, find_language_for_subtitle_stream(stream), NULL);
          }
          else {
            fprintf(stderr,"Subtitle stream %d codec not supported, ignored\n", i);
          }
        }
      }
804
#endif
805
806
    }

807
#ifdef HAVE_KATE
808
809
810
    for (i=0; i<this->n_kate_streams; ++i) {
        ff2theora_kate_stream *ks=this->kate_streams+i;
        if (ks->stream_index >= 0) {
811
#ifdef DEBUG
812
813
            printf("Muxing Kate stream %d from input stream %d\n",
                i,ks->stream_index);
814
#endif
815
816
817
818
819
            if (!this->disable_subtitles) {
              info.with_kate=1;
            }
        }
        else if (load_subtitles(ks,this->ignore_non_utf8)>0) {
820
#ifdef DEBUG
821
822
823
824
            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");
825
#endif
826
827
828
829
830
831
832
833
834
        }
        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;
          }
        }
    }
835
#endif
836
837
838

    oggmux_setup_kate_streams(&info, this->n_kate_streams);

Jan Gerber's avatar
Jan Gerber committed
839
    if (this->video_index >= 0 || this->audio_index >= 0) {
Jan Gerber's avatar
Jan Gerber committed
840
        AVFrame *frame=NULL;
841
        AVFrame *frame_p=NULL;
Jan Gerber's avatar
Jan Gerber committed
842
        AVFrame *output=NULL;
843
844
        AVFrame *output_p=NULL;
        AVFrame *output_tmp_p=NULL;
Jan Gerber's avatar
Jan Gerber committed
845
        AVFrame *output_tmp=NULL;
846
        AVFrame *output_resized_p=NULL;
Jan Gerber's avatar
Jan Gerber committed
847
        AVFrame *output_resized=NULL;
848
        AVFrame *output_buffered_p=NULL;
Jan Gerber's avatar
Jan Gerber committed
849
        AVFrame *output_buffered=NULL;
850
851
        AVFrame *output_cropped_p=NULL;
        AVFrame *output_cropped=NULL;
Jan Gerber's avatar
Jan Gerber committed
852
853
        AVFrame *output_padded_p=NULL;
        AVFrame *output_padded=NULL;
Jan Gerber's avatar
Jan Gerber committed
854

Jan Gerber's avatar
Jan Gerber committed
855
856
857
858
859
        AVPacket pkt;
        int len;
        int len1;
        int got_picture;
        int first = 1;
860
        int audio_eos = 0, video_eos = 0, audio_done = 0, video_done = 0;
Jan Gerber's avatar
Jan Gerber committed
861
862
        int ret;
        uint8_t *ptr;
863
864
        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
865
        int16_t *audio_p=NULL;
866
        int no_frames;
867
        int no_samples;
Jan Gerber's avatar
Jan Gerber committed
868

Jan Gerber's avatar
Jan Gerber committed
869
        double framerate_add = 0;
870
        double framerate_tmpcount = 0;
Jan Gerber's avatar
Jan Gerber committed
871

Jan Gerber's avatar
Jan Gerber committed
872
        if (this->video_index >= 0)
Jan Gerber's avatar
Jan Gerber committed
873
874
875
            info.audio_only=0;
        else
            info.audio_only=1;
Jan Gerber's avatar
Jan Gerber committed
876

Jan Gerber's avatar
Jan Gerber committed
877
        if (this->audio_index>=0)
Jan Gerber's avatar
Jan Gerber committed
878
879
880
            info.video_only=0;
        else
            info.video_only=1;
Jan Gerber's avatar
Jan Gerber committed
881

882
883
884
885
886
        if(info.audio_only)
            video_done = 1;
        if(info.video_only)
            audio_done = 1;

Jan Gerber's avatar
Jan Gerber committed
887
        if (!info.audio_only) {
888
            frame_p = frame = frame_alloc(vstream->codec->pix_fmt,
889
                            vstream->codec->width,vstream->codec->height);
890
            output_tmp_p = output_tmp = frame_alloc(this->pix_fmt,
891
                            vstream->codec->width, vstream->codec->height);
892
            output_p = output = frame_alloc(this->pix_fmt,
893
                            vstream->codec->width,vstream->codec->height);
894
            output_resized_p = output_resized = frame_alloc(this->pix_fmt,
Jan Gerber's avatar
Jan Gerber committed
895
                            this->picture_width, this->picture_height);
896
897
898
            output_cropped_p = output_cropped = frame_alloc(this->pix_fmt,
                            vstream->codec->width-this->frame_leftBand,
                            vstream->codec->height-this->frame_topBand);
899
            output_buffered_p = output_buffered = frame_alloc(this->pix_fmt,
Jan Gerber's avatar
Jan Gerber committed
900
                            this->frame_width, this->frame_height);
Jan Gerber's avatar
Jan Gerber committed
901
902
            output_padded_p = output_padded = frame_alloc(this->pix_fmt,
                            this->frame_width, this->frame_height);
Jan Gerber's avatar
Jan Gerber committed
903
904
905

            /* video settings here */
            /* config file? commandline options? v2v presets? */
Jan Gerber's avatar
Jan Gerber committed
906

Jan Gerber's avatar
Jan Gerber committed
907
            theora_info_init(&info.ti);
Jan Gerber's avatar
Jan Gerber committed
908

Jan Gerber's avatar
Jan Gerber committed
909
910
911
912
913
914
            info.ti.width = this->frame_width;
            info.ti.height = this->frame_height;
            info.ti.frame_width = this->picture_width;
            info.ti.frame_height = this->picture_height;
            info.ti.offset_x = this->frame_x_offset;
            info.ti.offset_y = this->frame_y_offset;
915
916
917
918
919
            if (this->framerate_new.num > 0) {
                // new framerate is interger only right now,
                // so denominator is always 1
                info.ti.fps_numerator = this->framerate_new.num;
                info.ti.fps_denominator = this->framerate_new.den;
Jan Gerber's avatar
Jan Gerber committed
920
921
            }
            else {
922
923
                info.ti.fps_numerator = vstream_fps.num;
                info.ti.fps_denominator = vstream_fps.den;
Jan Gerber's avatar
Jan Gerber committed
924
925
926
927
928
929
            }
            /* this is pixel aspect ratio */
            info.ti.aspect_numerator=this->aspect_numerator;
            info.ti.aspect_denominator=this->aspect_denominator;
            // FIXME: is all input material with fps==25 OC_CS_ITU_REC_470BG?
            // guess not, commandline option to select colorspace would be the best.
Jan Gerber's avatar
Jan Gerber committed
930
            if ((this->fps-25)<1)
Jan Gerber's avatar
Jan Gerber committed
931
                info.ti.colorspace = OC_CS_ITU_REC_470BG;
Jan Gerber's avatar
Jan Gerber committed
932
            else if (abs(this->fps-30)<1)
Jan Gerber's avatar
Jan Gerber committed
933
934
935
936
                info.ti.colorspace = OC_CS_ITU_REC_470M;
            else
                info.ti.colorspace = OC_CS_UNSPECIFIED;

Jan Gerber's avatar
Jan Gerber committed
937
            info.ti.target_bitrate = this->video_bitrate;
Jan Gerber's avatar
Jan Gerber committed
938
            info.ti.quality = this->video_quality;
939
            info.ti.dropframes_p = 0;
Jan Gerber's avatar
Jan Gerber committed
940
            info.ti.keyframe_auto_p = 1;
941
942
            info.ti.keyframe_frequency = this->keyint;
            info.ti.keyframe_frequency_force = this->keyint;
943
            info.ti.keyframe_data_target_bitrate = info.ti.target_bitrate * 5;
Jan Gerber's avatar
Jan Gerber committed
944
945
946
947
948
            info.ti.keyframe_auto_threshold = 80;
            info.ti.keyframe_mindistance = 8;
            info.ti.noise_sensitivity = 1;
            // range 0-2, 0 sharp, 2 less sharp,less bandwidth
            info.ti.sharpness = this->sharpness;
Jan Gerber's avatar
Jan Gerber committed
949

Jan Gerber's avatar
Jan Gerber committed
950
951
952
953
        }
        /* audio settings here */
        info.channels = this->channels;
        info.sample_rate = this->sample_rate;
954
        info.vorbis_quality = this->audio_quality * 0.1;
Jan Gerber's avatar
Jan Gerber committed
955
        info.vorbis_bitrate = this->audio_bitrate;
956
957
958
959
960
        /* subtitles */
#ifdef HAVE_KATE
        for (i=0; i<this->n_kate_streams; ++i) {
            ff2theora_kate_stream *ks = this->kate_streams+i;
            kate_info *ki = &info.kate_streams[i].ki;
Jan Gerber's avatar
Jan Gerber committed
961
            kate_info_init(ki);
962
            if (ks->stream_index >= 0 || ks->num_subtitles > 0) {
963
                if (!ks->subtitles_language[0]) {
Jan Gerber's avatar
Jan Gerber committed
964
                    fprintf(stderr, "WARNING - Subtitles language not set for input file %d\n",i);
965
                }
966
                kate_info_set_language(ki, ks->subtitles_language);
967
                kate_info_set_category(ki, ks->subtitles_category[0]?ks->subtitles_category:"SUB");
Jan Gerber's avatar
Jan Gerber committed
968
                if (this->force_input_fps.num > 0) {
969
970
                    ki->gps_numerator = this->force_input_fps.num;    /* fps= numerator/denominator */
                    ki->gps_denominator = this->force_input_fps.den;
971
972
973
                }
                else {
                    if (this->framerate_new.num > 0) {
Jan Gerber's avatar
Jan Gerber committed
974
                        // new framerate is interger only right now,
975
976
977
                        // so denominator is always 1
                        ki->gps_numerator = this->framerate_new.num;
                        ki->gps_denominator = this->framerate_new.den;
Jan Gerber's avatar
Jan Gerber committed
978
                    }
979
                    else {
980
981
                        ki->gps_numerator = vstream_fps.num;
                        ki->gps_denominator = vstream_fps.den;
982
983
984
985
986
987
                    }
                }
                ki->granule_shift = 32;
            }
        }
#endif
Jan Gerber's avatar
Jan Gerber committed
988
        oggmux_init(&info);
Jan Gerber's avatar
Jan Gerber committed
989
        /*seek to start time*/
Jan Gerber's avatar
Jan Gerber committed
990
991
992
        if (this->start_time) {
            av_seek_frame( this->context, -1, (int64_t)AV_TIME_BASE*this->start_time, 1);
            /* discard subtitles by their end time, so we still have those that start before the start time,
993
             but end after it */
Jan Gerber's avatar
Jan Gerber committed
994
995
996
997
998
999
1000
            for (i=0; i<this->n_kate_streams; ++i) {
                ff2theora_kate_stream *ks=this->kate_streams+i;
                while (ks->subtitles_count < ks->num_subtitles && ks->subtitles[ks->subtitles_count].t1 <= this->start_time) {
                    /* printf("skipping subtitle %u\n", ks->subtitles_count); */
                    ks->subtitles_count++;
                }
            }
For faster browsing, not all history is shown. View entire blame