694f18343433d0a4118ac6c3e7f725a7ae157a94
[goodguy/history.git] / cinelerra-5.0 / cinelerra / ffmpeg.C
1
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdarg.h>
8 #include <fcntl.h>
9 #include <limits.h>
10 // work arounds (centos)
11 #include <lzma.h>
12 #ifndef INT64_MAX
13 #define INT64_MAX 9223372036854775807LL
14 #endif
15
16 #include "asset.h"
17 #include "bccmodels.h"
18 #include "fileffmpeg.h"
19 #include "file.h"
20 #include "ffmpeg.h"
21 #include "mainerror.h"
22 #include "mwindow.h"
23 #include "vframe.h"
24
25
26 #define VIDEO_INBUF_SIZE 0x10000
27 #define AUDIO_INBUF_SIZE 0x10000
28 #define VIDEO_REFILL_THRESH 0
29 #define AUDIO_REFILL_THRESH 0x1000
30
31 Mutex FFMPEG::fflock("FFMPEG::fflock");
32
33 static void ff_err(int ret, const char *msg)
34 {
35         char errmsg[BCSTRLEN];  av_strerror(ret, errmsg, sizeof(errmsg));
36         fprintf(stderr,"%s: %s\n",msg, errmsg);
37 }
38
39 FFPacket::FFPacket()
40 {
41         init();
42 }
43
44 FFPacket::~FFPacket()
45 {
46         av_free_packet(&pkt);
47 }
48
49 void FFPacket::init()
50 {
51         av_init_packet(&pkt);
52         pkt.data = 0; pkt.size = 0;
53 }
54
55 FFrame::FFrame(FFStream *fst)
56 {
57         this->fst = fst;
58         frm = av_frame_alloc();
59         init = fst->init_frame(frm);
60 }
61
62 FFrame::~FFrame()
63 {
64         av_frame_free(&frm);
65 }
66
67 void FFrame::queue(int64_t pos)
68 {
69         position = pos;
70         fst->queue(this);
71 }
72
73 void FFrame::dequeue()
74 {
75         fst->dequeue(this);
76 }
77
78 int FFAudioStream::read(float *fp, long len)
79 {
80         long n = len * nch;
81         float *op = outp;
82         while( n > 0 ) {
83                 int k = lmt - op;
84                 if( k > n ) k = n;
85                 n -= k;
86                 while( --k >= 0 ) *fp++ = *op++;
87                 if( op >= lmt ) op = bfr;
88         }
89         return len;
90 }
91
92 void FFAudioStream::realloc(long sz, int nch, long len)
93 {
94         long bsz = sz * nch;
95         float *np = new float[bsz];
96         inp = np + read(np, len) * nch;
97         outp = np;
98         lmt = np + bsz;
99         this->nch = nch;
100         this->sz = sz;
101         delete [] bfr;  bfr = np;
102 }
103
104 void FFAudioStream::realloc(long sz, int nch)
105 {
106         if( sz > this->sz || this->nch != nch ) {
107                 long len = this->nch != nch ? 0 : curr_pos - seek_pos;
108                 if( len > this->sz ) len = this->sz;
109                 iseek(len);
110                 realloc(sz, nch, len);
111         }
112 }
113
114 void FFAudioStream::reserve(long sz, int nch)
115 {
116         long len = (inp - outp) / nch;
117         sz += len;
118         if( sz > this->sz || this->nch != nch ) {
119                 if( this->nch != nch ) len = 0;
120                 realloc(sz, nch, len);
121                 return;
122         }
123         if( (len*=nch) > 0 && bfr != outp )
124                 memmove(bfr, outp, len*sizeof(*bfr));
125         outp = bfr;
126         inp = bfr + len;
127 }
128
129 long FFAudioStream::used()
130 {
131         long len = inp>=outp ? inp-outp : inp-bfr + lmt-outp;
132         return len / nch;
133 }
134 long FFAudioStream::avail()
135 {
136         float *in1 = inp+1;
137         if( in1 >= lmt ) in1 = bfr;
138         long len = outp >= in1 ? outp-in1 : outp-bfr + lmt-in1;
139         return len / nch;
140 }
141 void FFAudioStream::reset() // clear bfr
142 {
143         inp = outp = bfr;
144 }
145
146 void FFAudioStream::iseek(int64_t ofs)
147 {
148         outp = inp - ofs*nch;
149         if( outp < bfr ) outp += sz*nch;
150 }
151
152 float *FFAudioStream::get_outp(int ofs)
153 {
154         float *ret = outp;
155         outp += ofs*nch;
156         return ret;
157 }
158
159 int64_t FFAudioStream::put_inp(int ofs)
160 {
161         inp += ofs*nch;
162         return (inp-outp) / nch;
163 }
164
165 int FFAudioStream::write(const float *fp, long len)
166 {
167         long n = len * nch;
168         float *ip = inp;
169         while( n > 0 ) {
170                 int k = lmt - ip;
171                 if( k > n ) k = n;
172                 n -= k;
173                 while( --k >= 0 ) *ip++ = *fp++;
174                 if( ip >= lmt ) ip = bfr;
175         }
176         inp = ip;
177         return len;
178 }
179
180 int FFAudioStream::zero(long len)
181 {
182         long n = len * nch;
183         float *ip = inp;
184         while( n > 0 ) {
185                 int k = lmt - ip;
186                 if( k > n ) k = n;
187                 n -= k;
188                 while( --k >= 0 ) *ip++ = 0;
189                 if( ip >= lmt ) ip = bfr;
190         }
191         inp = ip;
192         return len;
193 }
194
195 // does not advance outp
196 int FFAudioStream::read(double *dp, long len, int ch)
197 {
198         long n = len;
199         float *op = outp + ch;
200         float *lmt1 = lmt + nch-1;
201         while( n > 0 ) {
202                 int k = (lmt1 - op) / nch;
203                 if( k > n ) k = n;
204                 n -= k;
205                 while( --k >= 0 ) { *dp++ = *op;  op += nch; }
206                 if( op >= lmt ) op -= sz*nch;
207         }
208         return len;
209 }
210
211 // load linear buffer, no wrapping allowed, does not advance inp
212 int FFAudioStream::write(const double *dp, long len, int ch)
213 {
214         long n = len;
215         float *ip = inp + ch;
216         while( --n >= 0 ) { *ip = *dp++;  ip += nch; }
217         return len;
218 }
219
220
221 FFStream::FFStream(FFMPEG *ffmpeg, AVStream *st, int idx)
222 {
223         this->ffmpeg = ffmpeg;
224         this->st = st;
225         this->idx = idx;
226         frm_lock = new Mutex("FFStream::frm_lock");
227         fmt_ctx = 0;
228         filter_graph = 0;
229         buffersrc_ctx = 0;
230         buffersink_ctx = 0;
231         frm_count = 0;
232         nudge = AV_NOPTS_VALUE;
233         eof = 0;
234         reading = writing = 0;
235         need_packet = 1;
236         flushed = 0;
237         frame = fframe = 0;
238 }
239
240 FFStream::~FFStream()
241 {
242         if( reading > 0 || writing > 0 ) avcodec_close(st->codec);
243         if( fmt_ctx ) avformat_close_input(&fmt_ctx);
244         while( frms.first ) frms.remove(frms.first);
245         if( filter_graph ) avfilter_graph_free(&filter_graph);
246         if( frame ) av_frame_free(&frame);
247         if( fframe ) av_frame_free(&fframe);
248         bsfilter.remove_all_objects();
249         delete frm_lock;
250 }
251
252 void FFStream::ff_lock(const char *cp)
253 {
254         FFMPEG::fflock.lock(cp);
255 }
256
257 void FFStream::ff_unlock()
258 {
259         FFMPEG::fflock.unlock();
260 }
261
262 void FFStream::queue(FFrame *frm)
263 {
264         frm_lock->lock("FFStream::queue");
265         frms.append(frm);
266         ++frm_count;
267         frm_lock->unlock();
268         ffmpeg->mux_lock->unlock();
269 }
270
271 void FFStream::dequeue(FFrame *frm)
272 {
273         frm_lock->lock("FFStream::dequeue");
274         --frm_count;
275         frms.remove_pointer(frm);
276         frm_lock->unlock();
277 }
278
279 int FFStream::encode_activate()
280 {
281         if( writing < 0 )
282                 writing = ffmpeg->encode_activate();
283         return writing;
284 }
285
286 int FFStream::decode_activate()
287 {
288         if( reading < 0 && (reading=ffmpeg->decode_activate()) > 0 ) {
289                 ff_lock("FFStream::decode_activate");
290                 reading = 0;
291                 AVDictionary *copts = 0;
292                 av_dict_copy(&copts, ffmpeg->opts, 0);
293                 int ret = 0;
294                 // this should be avformat_copy_context(), but no copy avail
295                 ret = avformat_open_input(&fmt_ctx, ffmpeg->fmt_ctx->filename, NULL, &copts);
296                 if( ret >= 0 ) {
297                         ret = avformat_find_stream_info(fmt_ctx, 0);
298                         st = fmt_ctx->streams[idx];
299                 }
300                 if( ret >= 0 ) {
301                         AVCodecID codec_id = st->codec->codec_id;
302                         AVCodec *decoder = avcodec_find_decoder(codec_id);
303                         ret = avcodec_open2(st->codec, decoder, &copts);
304                         if( ret >= 0 )
305                                 reading = 1;
306                         else
307                                 eprintf("FFStream::decode_activate: open decoder failed\n");
308                 }
309                 else
310                         eprintf("FFStream::decode_activate: can't clone input file\n");
311                 av_dict_free(&copts);
312                 ff_unlock();
313         }
314         return reading;
315 }
316
317 int FFStream::read_packet()
318 {
319         av_packet_unref(ipkt);
320         int ret = av_read_frame(fmt_ctx, ipkt);
321         if( ret >= 0 ) return 1;
322         st_eof(1);
323         if( ret == AVERROR_EOF ) return 0;
324         fprintf(stderr, "FFStream::read_packet: av_read_frame failed\n");
325         flushed = 1;
326         return -1;
327 }
328
329 int FFStream::decode(AVFrame *frame)
330 {
331         int ret = 0;
332         int retries = 100;
333         int got_frame = 0;
334
335         while( ret >= 0 && !flushed && --retries >= 0 && !got_frame ) {
336                 if( need_packet ) {
337                         need_packet = 0;
338                         ret = read_packet();
339                         if( ret < 0 ) break;
340                         if( !ret ) ipkt->stream_index = st->index;
341                 }
342                 if( ipkt->stream_index == st->index ) {
343                         while( (ipkt->size > 0 || !ipkt->data) && !got_frame ) {
344                                 ret = decode_frame(frame, got_frame);
345                                 if( ret < 0 || !ipkt->data ) break;
346                                 ipkt->data += ret;
347                                 ipkt->size -= ret;
348                         }
349                         retries = 100;
350                 }
351                 if( !got_frame ) {
352                         need_packet = 1;
353                         flushed = st_eof();
354                 }
355         }
356
357         if( retries < 0 )
358                 fprintf(stderr, "FFStream::decode: Retry limit\n");
359         if( ret >= 0 )
360                 ret = got_frame;
361         else
362                 fprintf(stderr, "FFStream::decode: failed\n");
363
364         return ret;
365 }
366
367 int FFStream::load_filter(AVFrame *frame)
368 {
369         int ret = av_buffersrc_add_frame_flags(buffersrc_ctx,
370                         frame, AV_BUFFERSRC_FLAG_KEEP_REF);
371         if( ret < 0 ) {
372                 av_frame_unref(frame);
373                 eprintf("FFStream::load_filter: av_buffersrc_add_frame_flags failed\n");
374         }
375         return ret;
376 }
377
378 int FFStream::read_filter(AVFrame *frame)
379 {
380         int ret = av_buffersink_get_frame(buffersink_ctx, frame);
381         if( ret < 0 ) {
382                 if( ret == AVERROR(EAGAIN) ) return 0;
383                 if( ret == AVERROR_EOF ) { st_eof(1); return -1; }
384                 fprintf(stderr, "FFStream::read_filter: av_buffersink_get_frame failed\n");
385                 return ret;
386         }
387         return 1;
388 }
389
390 int FFStream::read_frame(AVFrame *frame)
391 {
392         if( !filter_graph || !buffersrc_ctx || !buffersink_ctx )
393                 return decode(frame);
394         if( !fframe && !(fframe=av_frame_alloc()) ) {
395                 fprintf(stderr, "FFStream::read_frame: av_frame_alloc failed\n");
396                 return -1;
397         }
398         int ret = -1;
399         while( !flushed && !(ret=read_filter(frame)) ) {
400                 if( (ret=decode(fframe)) < 0 ) break;
401                 if( ret > 0 && (ret=load_filter(fframe)) < 0 ) break;
402         }
403         return ret;
404 }
405
406 FFAudioStream::FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx)
407  : FFStream(ffmpeg, strm, idx)
408 {
409         channel0 = channels = 0;
410         sample_rate = 0;
411         mbsz = 0;
412         seek_pos = curr_pos = 0;
413         length = 0;
414         resample_context = 0;
415
416         aud_bfr_sz = 0;
417         aud_bfr = 0;
418
419 // history buffer
420         nch = 2;
421         sz = 0x10000;
422         long bsz = sz * nch;
423         bfr = new float[bsz];
424         inp = outp = bfr;
425         lmt = bfr + bsz;
426 }
427
428 FFAudioStream::~FFAudioStream()
429 {
430         if( resample_context ) swr_free(&resample_context);
431         delete [] aud_bfr;
432         delete [] bfr;
433 }
434
435 int FFAudioStream::load_history(uint8_t **data, int len)
436 {
437         float *samples = *(float **)data;
438         if( resample_context ) {
439                 if( len > aud_bfr_sz ) {        
440                         delete [] aud_bfr;
441                         aud_bfr = 0;
442                 }
443                 if( !aud_bfr ) {
444                         aud_bfr_sz = len;
445                         aud_bfr = new float[aud_bfr_sz*channels];
446                 }
447                 int ret = swr_convert(resample_context,
448                         (uint8_t**)&aud_bfr, aud_bfr_sz, (const uint8_t**)data, len);
449                 if( ret < 0 ) {
450                         fprintf(stderr, "FFAudioStream::load_history: swr_convert failed\n");
451                         return -1;
452                 }
453                 samples = aud_bfr;
454                 len = ret;
455         }
456         // biggest user bfr since seek + frame
457         realloc(mbsz + len + 1, channels);
458         write(samples, len);
459         return len;
460 }
461
462 int FFAudioStream::decode_frame(AVFrame *frame, int &got_frame)
463 {
464         int ret = avcodec_decode_audio4(st->codec, frame, &got_frame, ipkt);
465         if( ret < 0 ) {
466                 fprintf(stderr, "FFAudioStream::decode_frame: Could not read audio frame\n");
467                 return -1;
468         }
469         return ret;
470 }
471
472 int FFAudioStream::encode_activate()
473 {
474         if( writing >= 0 ) return writing;
475         AVCodecContext *ctx = st->codec;
476         frame_sz = ctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ?
477                 10000 : ctx->frame_size;
478         return FFStream::encode_activate();
479 }
480
481 int FFAudioStream::nb_samples()
482 {
483         AVCodecContext *ctx = st->codec;
484         return ctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ?
485                 10000 : ctx->frame_size;
486 }
487
488 int64_t FFAudioStream::load_buffer(double ** const sp, int len)
489 {
490         reserve(len+1, st->codec->channels);
491         for( int ch=0; ch<nch; ++ch )
492                 write(sp[ch], len, ch);
493         return put_inp(len);
494 }
495
496 int FFAudioStream::in_history(int64_t pos)
497 {
498         if( pos > curr_pos ) return 0;
499         int64_t len = curr_pos - seek_pos;
500         if( len > sz ) len = sz;
501         if( pos < curr_pos - len ) return 0;
502         return 1;
503 }
504
505
506 int FFAudioStream::init_frame(AVFrame *frame)
507 {
508         AVCodecContext *ctx = st->codec;
509         frame->nb_samples = frame_sz;
510         frame->format = ctx->sample_fmt;
511         frame->channel_layout = ctx->channel_layout;
512         frame->sample_rate = ctx->sample_rate;
513         int ret = av_frame_get_buffer(frame, 0);
514         if (ret < 0)
515                 fprintf(stderr, "FFAudioStream::init_frame: av_frame_get_buffer failed\n");
516         return ret;
517 }
518
519 int FFAudioStream::load(int64_t pos, int len)
520 {
521         if( audio_seek(pos) < 0 ) return -1;
522         if( mbsz < len ) mbsz = len;
523         int ret = 0;
524         int64_t end_pos = pos + len;
525         if( !frame && !(frame=av_frame_alloc()) ) {
526                 fprintf(stderr, "FFAudioStream::load: av_frame_alloc failed\n");
527                 return -1;
528         }
529         for( int i=0; ret>=0 && !flushed && curr_pos<end_pos && i<1000; ++i ) {
530                 ret = read_frame(frame);
531                 if( ret > 0 ) {
532                         load_history(&frame->extended_data[0], frame->nb_samples);
533                         curr_pos += frame->nb_samples;
534                 }
535         }
536         if( flushed && end_pos > curr_pos ) {
537                 zero(end_pos - curr_pos);
538                 curr_pos = end_pos;
539         }
540         return curr_pos - pos;
541 }
542
543 int FFAudioStream::audio_seek(int64_t pos)
544 {
545         if( decode_activate() < 0 ) return -1;
546         if( in_history(pos) ) {
547                 iseek(curr_pos - pos);
548                 return 0;
549         }
550         if( pos == curr_pos ) return 0;
551         if( !st->codec || !st->codec->codec ) return -1;
552         avcodec_flush_buffers(st->codec);
553         double secs = (double)pos / sample_rate;
554         int64_t tstmp = secs * st->time_base.den / st->time_base.num;
555         if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
556         avformat_seek_file(fmt_ctx, st->index, -INT64_MAX, tstmp, INT64_MAX, 0);
557         seek_pos = curr_pos = pos;
558         reset();  st_eof(0);
559         mbsz = 0; flushed = 0;  need_packet = 1;
560         return 1;
561 }
562
563 int FFAudioStream::encode(double **samples, int len)
564 {
565         if( encode_activate() <= 0 ) return -1;
566         ffmpeg->flow_ctl();
567         int ret = 0;
568         int64_t count = load_buffer(samples, len);
569         FFrame *frm = 0;
570
571         while( ret >= 0 && count >= frame_sz ) {
572                 frm = new FFrame(this);
573                 if( (ret=frm->initted()) < 0 ) break;
574                 AVFrame *frame = *frm;
575                 float *bfrp = get_outp(frame_sz);
576                 ret =  swr_convert(resample_context,
577                         (uint8_t **)frame->extended_data, frame_sz,
578                         (const uint8_t **)&bfrp, frame_sz);
579                 if( ret < 0 ) {
580                         fprintf(stderr, "FFAudioStream::encode: swr_convert failed\n");
581                         break;
582                 }
583                 frm->queue(curr_pos);
584                 frm = 0;
585                 curr_pos += frame_sz;
586                 count -= frame_sz;
587         }
588
589         delete frm;
590         return ret >= 0 ? 0 : 1;
591 }
592
593 FFVideoStream::FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx)
594  : FFStream(ffmpeg, strm, idx)
595 {
596         width = height = 0;
597         frame_rate = 0;
598         aspect_ratio = 0;
599         seek_pos = curr_pos = 0;
600         length = 0;
601         convert_ctx = 0;
602 }
603
604 FFVideoStream::~FFVideoStream()
605 {
606         if( convert_ctx ) sws_freeContext(convert_ctx);
607 }
608
609 int FFVideoStream::decode_frame(AVFrame *frame, int &got_frame)
610 {
611         int ret = avcodec_decode_video2(st->codec, frame, &got_frame, ipkt);
612         if( ret < 0 ) {
613                 fprintf(stderr, "FFVideoStream::decode_frame: Could not read video frame\n");
614                 return -1;
615         }
616         if( got_frame )
617                 ++curr_pos;
618         return ret;
619 }
620
621 int FFVideoStream::load(VFrame *vframe, int64_t pos)
622 {
623         if( video_seek(pos) < 0 ) return -1;
624         if( !frame && !(frame=av_frame_alloc()) ) {
625                 fprintf(stderr, "FFVideoStream::load: av_frame_alloc failed\n");
626                 return -1;
627         }
628         int ret = 0;
629         for( int i=0; ret>=0 && !flushed && curr_pos<=pos && i<1000; ++i ) {
630                 ret = read_frame(frame);
631         }
632         if( ret > 0 ) {
633                 AVCodecContext *ctx = st->codec;
634                 ret = convert_cmodel(vframe, (AVPicture *)frame,
635                         ctx->pix_fmt, ctx->width, ctx->height);
636         }
637         ret = ret > 0 ? 1 : ret < 0 ? -1 : 0;
638         return ret;
639 }
640
641 int FFVideoStream::video_seek(int64_t pos)
642 {
643         if( decode_activate() < 0 ) return -1;
644 // if close enough, just read up to current
645 //   3*gop_size seems excessive, but less causes tears
646         int gop = 3*st->codec->gop_size;
647         if( gop < 4 ) gop = 4;
648         if( gop > 64 ) gop = 64;
649         if( pos >= curr_pos && pos <= curr_pos + gop ) return 0;
650         if( !st->codec || !st->codec->codec ) return -1;
651         avcodec_flush_buffers(st->codec);
652 // back up a few frames to read up to current to help repair damages
653         if( (pos-=gop) < 0 ) pos = 0;
654         double secs = (double)pos / frame_rate;
655         int64_t tstmp = secs * st->time_base.den / st->time_base.num;
656         if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
657         avformat_seek_file(fmt_ctx, st->index, -INT64_MAX, tstmp, INT64_MAX, 0);
658         seek_pos = curr_pos = pos;
659         st_eof(0);
660         flushed = 0;  need_packet = 1;
661         return 1;
662 }
663
664 int FFVideoStream::init_frame(AVFrame *picture)
665 {
666         AVCodecContext *ctx = st->codec;
667         picture->format = ctx->pix_fmt;
668         picture->width  = ctx->width;
669         picture->height = ctx->height;
670         int ret = av_frame_get_buffer(picture, 32);
671         return ret;
672 }
673
674 int FFVideoStream::encode(VFrame *vframe)
675 {
676         if( encode_activate() <= 0 ) return -1;
677         ffmpeg->flow_ctl();
678         FFrame *picture = new FFrame(this);
679         int ret = picture->initted();
680         if( ret >= 0 ) {
681                 AVFrame *frame = *picture;
682                 frame->pts = curr_pos;
683                 AVCodecContext *ctx = st->codec;
684                 ret = convert_pixfmt(vframe, (AVPicture*)frame,
685                         ctx->pix_fmt, ctx->width, ctx->height);
686         }
687         if( ret >= 0 ) {
688                 picture->queue(curr_pos);
689                 ++curr_pos;
690         }
691         else {
692                 fprintf(stderr, "FFVideoStream::encode: encode failed\n");
693                 delete picture;
694         }
695         return ret >= 0 ? 0 : 1;
696 }
697
698
699 PixelFormat FFVideoStream::color_model_to_pix_fmt(int color_model)
700 {
701         switch( color_model ) { 
702         case BC_YUV422:         return AV_PIX_FMT_YUYV422;
703         case BC_RGB888:         return AV_PIX_FMT_RGB24;
704         case BC_RGBA8888:       return AV_PIX_FMT_RGBA;
705         case BC_BGR8888:        return AV_PIX_FMT_BGR0;
706         case BC_BGR888:         return AV_PIX_FMT_BGR24;
707         case BC_YUV420P:        return AV_PIX_FMT_YUV420P;
708         case BC_YUV422P:        return AV_PIX_FMT_YUV422P;
709         case BC_YUV444P:        return AV_PIX_FMT_YUV444P;
710         case BC_YUV411P:        return AV_PIX_FMT_YUV411P;
711         case BC_RGB565:         return AV_PIX_FMT_RGB565;
712         case BC_RGB161616:      return AV_PIX_FMT_RGB48LE;
713         case BC_RGBA16161616:   return AV_PIX_FMT_RGBA64LE;
714         default: break;
715         }
716
717         return AV_PIX_FMT_NB;
718 }
719
720 int FFVideoStream::pix_fmt_to_color_model(PixelFormat pix_fmt)
721 {
722         switch (pix_fmt) { 
723         case AV_PIX_FMT_YUYV422:        return BC_YUV422;
724         case AV_PIX_FMT_RGB24:          return BC_RGB888;
725         case AV_PIX_FMT_RGBA:           return BC_RGBA8888;
726         case AV_PIX_FMT_BGR0:           return BC_BGR8888;
727         case AV_PIX_FMT_BGR24:          return BC_BGR888;
728         case AV_PIX_FMT_YUV420P:        return BC_YUV420P;
729         case AV_PIX_FMT_YUV422P:        return BC_YUV422P;
730         case AV_PIX_FMT_YUV444P:        return BC_YUV444P;
731         case AV_PIX_FMT_YUV411P:        return BC_YUV411P;
732         case AV_PIX_FMT_RGB565:         return BC_RGB565;
733         case AV_PIX_FMT_RGB48LE:        return BC_RGB161616;
734         case AV_PIX_FMT_RGBA64LE:       return BC_RGBA16161616;
735         default: break;
736         }
737
738         return BC_TRANSPARENCY;
739 }
740
741 int FFVideoStream::convert_picture_vframe(VFrame *frame,
742                 AVPicture *ip, PixelFormat ifmt, int iw, int ih)
743 {
744         AVPicture opic;
745         int cmodel = frame->get_color_model();
746         PixelFormat ofmt = color_model_to_pix_fmt(cmodel);
747         if( ofmt == AV_PIX_FMT_NB ) return -1;
748         int size = avpicture_fill(&opic, frame->get_data(), ofmt, 
749                                   frame->get_w(), frame->get_h());
750         if( size < 0 ) return -1;
751
752         // transfer line sizes must match also
753         int planar = BC_CModels::is_planar(cmodel);
754         int packed_width = !planar ? frame->get_bytes_per_line() :
755                  BC_CModels::calculate_pixelsize(cmodel) * frame->get_w();
756         if( packed_width != opic.linesize[0] )  return -1;
757
758         if( planar ) {
759                 // override avpicture_fill() for planar types
760                 opic.data[0] = frame->get_y();
761                 opic.data[1] = frame->get_u();
762                 opic.data[2] = frame->get_v();
763         }
764
765         convert_ctx = sws_getCachedContext(convert_ctx, iw, ih, ifmt,
766                 frame->get_w(), frame->get_h(), ofmt, SWS_BICUBIC, NULL, NULL, NULL);
767         if( !convert_ctx ) {
768                 fprintf(stderr, "FFVideoStream::convert_picture_frame:"
769                                 " sws_getCachedContext() failed\n");
770                 return 1;
771         }
772         if( sws_scale(convert_ctx, ip->data, ip->linesize, 0, ih,
773             opic.data, opic.linesize) < 0 ) {
774                 fprintf(stderr, "FFVideoStream::convert_picture_frame: sws_scale() failed\n");
775                 return 1;
776         }
777         return 0;
778 }
779
780 int FFVideoStream::convert_cmodel(VFrame *frame,
781                  AVPicture *ip, PixelFormat ifmt, int iw, int ih)
782 {
783         // try direct transfer
784         if( !convert_picture_vframe(frame, ip, ifmt, iw, ih) ) return 1;
785         // use indirect transfer
786         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(ifmt);
787         int max_bits = 0;
788         for( int i = 0; i <desc->nb_components; ++i ) {
789                 int bits = desc->comp[i].depth_minus1 + 1;
790                 if( bits > max_bits ) max_bits = bits;
791         }
792 // from libavcodec/pixdesc.c
793 #define pixdesc_has_alpha(pixdesc) ((pixdesc)->nb_components == 2 || \
794  (pixdesc)->nb_components == 4 || (pixdesc)->flags & AV_PIX_FMT_FLAG_PAL)
795         int icolor_model = pixdesc_has_alpha(desc) ?
796                 (max_bits > 8 ? BC_RGBA16161616 : BC_RGBA8888) :
797                 (max_bits > 8 ? BC_RGB161616 : BC_RGB888) ;
798         VFrame vframe(iw, ih, icolor_model);
799         if( convert_picture_vframe(&vframe, ip, ifmt, iw, ih) ) return -1;
800         frame->transfer_from(&vframe);
801         return 1;
802 }
803
804 int FFVideoStream::convert_vframe_picture(VFrame *frame,
805                 AVPicture *op, PixelFormat ofmt, int ow, int oh)
806 {
807         AVPicture opic;
808         int cmodel = frame->get_color_model();
809         PixelFormat ifmt = color_model_to_pix_fmt(cmodel);
810         if( ifmt == AV_PIX_FMT_NB ) return -1;
811         int size = avpicture_fill(&opic, frame->get_data(), ifmt, 
812                                   frame->get_w(), frame->get_h());
813         if( size < 0 ) return -1;
814
815         // transfer line sizes must match also
816         int planar = BC_CModels::is_planar(cmodel);
817         int packed_width = !planar ? frame->get_bytes_per_line() :
818                  BC_CModels::calculate_pixelsize(cmodel) * frame->get_w();
819         if( packed_width != opic.linesize[0] )  return -1;
820
821         if( planar ) {
822                 // override avpicture_fill() for planar types
823                 opic.data[0] = frame->get_y();
824                 opic.data[1] = frame->get_u();
825                 opic.data[2] = frame->get_v();
826         }
827
828         convert_ctx = sws_getCachedContext(convert_ctx, frame->get_w(), frame->get_h(), ifmt,
829                 ow, oh, ofmt, SWS_BICUBIC, NULL, NULL, NULL);
830         if( !convert_ctx ) {
831                 fprintf(stderr, "FFVideoStream::convert_frame_picture:"
832                                 " sws_getCachedContext() failed\n");
833                 return 1;
834         }
835         if( sws_scale(convert_ctx, opic.data, opic.linesize, 0, frame->get_h(),
836                         op->data, op->linesize) < 0 ) {
837                 fprintf(stderr, "FFVideoStream::convert_frame_picture: sws_scale() failed\n");
838                 return 1;
839         }
840         return 0;
841 }
842
843 int FFVideoStream::convert_pixfmt(VFrame *frame,
844                  AVPicture *op, PixelFormat ofmt, int ow, int oh)
845 {
846         // try direct transfer
847         if( !convert_vframe_picture(frame, op, ofmt, ow, oh) ) return 0;
848         // use indirect transfer
849         int colormodel = frame->get_color_model();
850         int bits = BC_CModels::calculate_pixelsize(colormodel) * 8;
851         bits /= BC_CModels::components(colormodel);
852         int icolor_model =  BC_CModels::has_alpha(colormodel) ?
853                 (bits > 8 ? BC_RGBA16161616 : BC_RGBA8888) :
854                 (bits > 8 ? BC_RGB161616: BC_RGB888) ;
855         VFrame vframe(frame->get_w(), frame->get_h(), icolor_model);
856         vframe.transfer_from(frame);
857         if( convert_vframe_picture(&vframe, op, ofmt, ow, oh) ) return 1;
858         return 0;
859 }
860
861
862 FFMPEG::FFMPEG(FileBase *file_base)
863 {
864         fmt_ctx = 0;
865         this->file_base = file_base;
866         memset(file_format,0,sizeof(file_format));
867         mux_lock = new Condition(0,"FFMPEG::mux_lock",0);
868         flow_lock = new Condition(1,"FFStream::flow_lock",0);
869         done = -1;
870         flow = 1;
871         decoding = encoding = 0;
872         has_audio = has_video = 0;
873         opts = 0;
874         opt_duration = -1;
875         opt_video_filter = 0;
876         opt_audio_filter = 0;
877         char option_path[BCTEXTLEN];
878         set_option_path(option_path, "%s", "ffmpeg.opts");
879         read_options(option_path, opts);
880 }
881
882 FFMPEG::~FFMPEG()
883 {
884         ff_lock("FFMPEG::~FFMPEG()");
885         close_encoder();
886         ffaudio.remove_all_objects();
887         ffvideo.remove_all_objects();
888         if( encoding ) avformat_free_context(fmt_ctx);
889         ff_unlock();
890         delete flow_lock;
891         delete mux_lock;
892         av_dict_free(&opts);
893         delete opt_video_filter;
894         delete opt_audio_filter;
895 }
896
897 int FFMPEG::check_sample_rate(AVCodec *codec, int sample_rate)
898 {
899         const int *p = codec->supported_samplerates;
900         if( !p ) return sample_rate;
901         while( *p != 0 ) {
902                 if( *p == sample_rate ) return *p;
903                 ++p;
904         }
905         return 0;
906 }
907
908 static inline AVRational std_frame_rate(int i)
909 {
910         static const int m1 = 1001*12, m2 = 1000*12;
911         static const int freqs[] = {
912                 40*m1, 48*m1, 50*m1, 60*m1, 80*m1,120*m1, 240*m1,
913                 24*m2, 30*m2, 60*m2, 12*m2, 15*m2, 48*m2, 0,
914         };
915         int freq = i<30*12 ? (i+1)*1001 : freqs[i-30*12];
916         return (AVRational) { freq, 1001*12 };
917 }
918
919 AVRational FFMPEG::check_frame_rate(AVCodec *codec, double frame_rate)
920 {
921         const AVRational *p = codec->supported_framerates;
922         AVRational rate, best_rate = (AVRational) { 0, 0 };
923         double max_err = 1.;  int i = 0;
924         while( ((p ? (rate=*p++) : (rate=std_frame_rate(i++))), rate.num) != 0 ) {
925                 double framerate = (double) rate.num / rate.den;
926                 double err = fabs(frame_rate/framerate - 1.);
927                 if( err >= max_err ) continue;
928                 max_err = err;
929                 best_rate = rate;
930         }
931         return max_err < 0.0001 ? best_rate : (AVRational) { 0, 0 };
932 }
933
934 AVRational FFMPEG::to_sample_aspect_ratio(double aspect_ratio)
935 {
936 #if 1
937         int height = 1000000, width = height * aspect_ratio;
938         float w, h;
939         MWindow::create_aspect_ratio(w, h, width, height);
940         return (AVRational){(int)w, (int)h};
941 #else
942 // square pixels
943         return (AVRational){1, 1};
944 #endif
945 }
946
947 AVRational FFMPEG::to_time_base(int sample_rate)
948 {
949         return (AVRational){1, sample_rate};
950 }
951
952 extern void get_exe_path(char *result); // from main.C
953
954 void FFMPEG::set_option_path(char *path, const char *fmt, ...)
955 {
956         get_exe_path(path);
957         strcat(path, "/ffmpeg/");
958         path += strlen(path);
959         va_list ap;
960         va_start(ap, fmt);
961         vsprintf(path, fmt, ap);
962         va_end(ap);
963 }
964
965 void FFMPEG::get_option_path(char *path, const char *type, const char *spec)
966 {
967         if( *spec == '/' )
968                 strcpy(path, spec);
969         else
970                 set_option_path(path, "%s/%s", type, spec);
971 }
972
973 int FFMPEG::get_format(char *format, const char *path, char *spec)
974 {
975         char option_path[BCTEXTLEN], line[BCTEXTLEN], codec[BCTEXTLEN];
976         get_option_path(option_path, path, spec);
977         FILE *fp = fopen(option_path,"r");
978         if( !fp ) return 1;
979         int ret = 0;
980         if( !fgets(line, sizeof(line), fp) ) ret = 1;
981         if( !ret ) {
982                 line[sizeof(line)-1] = 0;
983                 ret = scan_option_line(line, format, codec);
984         }
985         fclose(fp);
986         return ret;
987 }
988
989 int FFMPEG::get_file_format()
990 {
991         int ret = 0;
992         char audio_format[BCSTRLEN], video_format[BCSTRLEN];
993         file_format[0] = audio_format[0] = video_format[0] = 0;
994         Asset *asset = file_base->asset;
995         if( !ret && asset->audio_data )
996                 ret = get_format(audio_format, "audio", asset->acodec);
997         if( !ret && asset->video_data )
998                 ret = get_format(video_format, "video", asset->vcodec);
999         if( !ret && !audio_format[0] && !video_format[0] )
1000                 ret = 1;
1001         if( !ret && audio_format[0] && video_format[0] &&
1002             strcmp(audio_format, video_format) ) ret = -1;
1003         if( !ret )
1004                 strcpy(file_format, audio_format[0] ? audio_format : video_format);
1005         return ret;
1006 }
1007
1008 int FFMPEG::scan_option_line(char *cp, char *tag, char *val)
1009 {
1010         while( *cp == ' ' || *cp == '\t' ) ++cp;
1011         char *bp = cp;
1012         while( *cp && *cp != ' ' && *cp != '\t' && *cp != '=' ) ++cp;
1013         int len = cp - bp;
1014         if( !len || len > BCSTRLEN-1 ) return 1;
1015         while( bp < cp ) *tag++ = *bp++;
1016         *tag = 0;
1017         while( *cp == ' ' || *cp == '\t' ) ++cp;
1018         if( *cp == '=' ) ++cp;
1019         while( *cp == ' ' || *cp == '\t' ) ++cp;
1020         bp = cp;
1021         while( *cp && *cp != '\n' ) ++cp;
1022         len = cp - bp;
1023         if( len > BCTEXTLEN-1 ) return 1;
1024         while( bp < cp ) *val++ = *bp++;
1025         *val = 0;
1026         return 0;
1027 }
1028
1029 int FFMPEG::get_encoder(const char *options,
1030                 char *format, char *codec, char *bsfilter, char *bsargs)
1031 {
1032         FILE *fp = fopen(options,"r");
1033         if( !fp ) {
1034                 eprintf("FFMPEG::get_encoder: options open failed %s\n",options);
1035                 return 1;
1036         }
1037         if( get_encoder(fp, format, codec, bsfilter, bsargs) )
1038                 eprintf("FFMPEG::get_encoder:"
1039                         " err: format/codec not found %s\n", options);
1040         fclose(fp);
1041         return 0;
1042 }
1043
1044 int FFMPEG::get_encoder(FILE *fp,
1045                 char *format, char *codec, char *bsfilter, char *bsargs)
1046 {
1047         format[0] = codec[0] = bsfilter[0] = bsargs[0] = 0;
1048         char line[BCTEXTLEN];
1049         if( !fgets(line, sizeof(line), fp) ) return 1;
1050         line[sizeof(line)-1] = 0;
1051         if( scan_option_line(line, format, codec) ) return 1;
1052         char *cp = codec;
1053         while( *cp && *cp != '|' ) ++cp;
1054         if( !*cp ) return 0;
1055         if( scan_option_line(cp+1, bsfilter, bsargs) ) return 1;
1056         do { *cp-- = 0; } while( cp>=codec && (*cp==' ' || *cp == '\t' ) );
1057         return 0;
1058 }
1059
1060 int FFMPEG::read_options(const char *options, AVDictionary *&opts)
1061 {
1062         FILE *fp = fopen(options,"r");
1063         if( !fp ) return 1;
1064         int ret = read_options(fp, options, opts);
1065         fclose(fp);
1066         return ret;
1067 }
1068
1069 int FFMPEG::scan_options(const char *options, AVDictionary *&opts)
1070 {
1071         FILE *fp = fmemopen((void *)options,strlen(options),"r");
1072         if( !fp ) return 0;
1073         int ret = read_options(fp, options, opts);
1074         fclose(fp);
1075         return ret;
1076 }
1077
1078 int FFMPEG::read_options(FILE *fp, const char *options, AVDictionary *&opts)
1079 {
1080         int ret = 0, no = 0;
1081         char line[BCTEXTLEN];
1082         while( !ret && fgets(line, sizeof(line), fp) ) {
1083                 line[sizeof(line)-1] = 0;
1084                 ++no;
1085                 if( line[0] == '#' ) continue;
1086                 if( line[0] == '\n' ) continue;
1087                 char key[BCSTRLEN], val[BCTEXTLEN];
1088                 if( scan_option_line(line, key, val) ) {
1089                         eprintf("FFMPEG::read_options:"
1090                                 " err reading %s: line %d\n", options, no);
1091                         ret = 1;
1092                 }
1093                 if( !ret ) {
1094                         if( !strcmp(key, "duration") )
1095                                 opt_duration = strtod(val, 0);
1096                         if( !strcmp(key, "video_filter") )
1097                                 opt_video_filter = cstrdup(val);
1098                         if( !strcmp(key, "audio_filter") )
1099                                 opt_audio_filter = cstrdup(val);
1100                         else if( !strcmp(key, "loglevel") )
1101                                 set_loglevel(val);
1102                         else
1103                                 av_dict_set(&opts, key, val, 0);
1104                 }
1105         }
1106         return ret;
1107 }
1108
1109 int FFMPEG::load_options(const char *options, AVDictionary *&opts)
1110 {
1111         char option_path[BCTEXTLEN];
1112         set_option_path(option_path, "%s", options);
1113         return read_options(option_path, opts);
1114 }
1115
1116 int FFMPEG::load_options(const char *path, char *bfr, int len)
1117 {
1118         *bfr = 0;
1119         FILE *fp = fopen(path, "r");
1120         if( !fp ) return 1;
1121         fgets(bfr, len, fp); // skip hdr
1122         len = fread(bfr, 1, len-1, fp);
1123         if( len < 0 ) len = 0;
1124         bfr[len] = 0;
1125         fclose(fp);
1126         return 0;
1127 }
1128
1129 void FFMPEG::set_loglevel(const char *ap)
1130 {
1131         if( !ap || !*ap ) return;
1132         const struct {
1133                 const char *name;
1134                 int level;
1135         } log_levels[] = {
1136                 { "quiet"  , AV_LOG_QUIET   },
1137                 { "panic"  , AV_LOG_PANIC   },
1138                 { "fatal"  , AV_LOG_FATAL   },
1139                 { "error"  , AV_LOG_ERROR   },
1140                 { "warning", AV_LOG_WARNING },
1141                 { "info"   , AV_LOG_INFO    },
1142                 { "verbose", AV_LOG_VERBOSE },
1143                 { "debug"  , AV_LOG_DEBUG   },
1144         };
1145         for( int i=0; i<(int)(sizeof(log_levels)/sizeof(log_levels[0])); ++i ) {
1146                 if( !strcmp(log_levels[i].name, ap) ) {
1147                         av_log_set_level(log_levels[i].level);
1148                         return;
1149                 }
1150         }
1151         av_log_set_level(atoi(ap));
1152 }
1153
1154 double FFMPEG::to_secs(int64_t time, AVRational time_base)
1155 {
1156         double base_time = time == AV_NOPTS_VALUE ? 0 :
1157                 av_rescale_q(time, time_base, AV_TIME_BASE_Q);
1158         return base_time / AV_TIME_BASE; 
1159 }
1160
1161 int FFMPEG::info(char *text, int len)
1162 {
1163         if( len <= 0 ) return 0;
1164 #define report(s...) do { int n = snprintf(cp,len,s); cp += n;  len -= n; } while(0)
1165         char *cp = text;
1166         for( int i=0; i<(int)fmt_ctx->nb_streams; ++i ) {
1167                 AVStream *st = fmt_ctx->streams[i];
1168                 AVCodecContext *avctx = st->codec;
1169                 report("stream %d,  id 0x%06x:\n", i, avctx->codec_id);
1170                 const AVCodecDescriptor *desc = avcodec_descriptor_get(avctx->codec_id);
1171                 if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) {
1172                         AVRational framerate = av_guess_frame_rate(fmt_ctx, st, 0);
1173                         double frame_rate = !framerate.den ? 0 : (double)framerate.num / framerate.den;
1174                         report("  video %s",desc ? desc->name : " (unkn)");
1175                         report(" %dx%d %5.2f", avctx->width, avctx->height, frame_rate);
1176                         const char *pfn = av_get_pix_fmt_name(avctx->pix_fmt);
1177                         report(" pix %s\n", pfn ? pfn : "(unkn)");
1178                         double secs = to_secs(st->duration, st->time_base);
1179                         int64_t length = secs * frame_rate + 0.5;
1180                         report("    %jd frms %0.2f secs", length, secs);
1181                         int hrs = secs/3600;  secs -= hrs*3600;
1182                         int mins = secs/60;  secs -= mins*60;
1183                         report("  %d:%02d:%05.2f\n", hrs, mins, secs);
1184
1185                 }
1186                 else if( avctx->codec_type == AVMEDIA_TYPE_AUDIO ) {
1187                         int sample_rate = avctx->sample_rate;
1188                         const char *fmt = av_get_sample_fmt_name(avctx->sample_fmt);
1189                         report("  audio %s",desc ? desc->name : " (unkn)");
1190                         report(" %dch %s %d",avctx->channels, fmt, sample_rate);
1191                         int sample_bits = av_get_bits_per_sample(avctx->codec_id);
1192                         report(" %dbits\n", sample_bits);
1193                         double secs = to_secs(st->duration, st->time_base);
1194                         int64_t length = secs * sample_rate + 0.5;
1195                         report("    %jd smpl %0.2f secs", length, secs);
1196                         int hrs = secs/3600;  secs -= hrs*3600;
1197                         int mins = secs/60;  secs -= mins*60;
1198                         report("  %d:%02d:%05.2f\n", hrs, mins, secs);
1199                 }
1200                 else
1201                         report("  codec_type unknown\n");
1202         }
1203         report("\n");
1204         for( int i=0; i<(int)fmt_ctx->nb_programs; ++i ) {
1205                 report("program %d", i+1);
1206                 AVProgram *pgrm = fmt_ctx->programs[i];
1207                 for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j )
1208                         report(", %d", pgrm->stream_index[j]);
1209                 report("\n");
1210         }
1211         report("\n");
1212         AVDictionaryEntry *tag = 0;
1213         while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
1214                 report("%s=%s\n", tag->key, tag->value);
1215
1216         if( !len ) --cp;
1217         *cp = 0;
1218         return cp - text;
1219 #undef report
1220 }
1221
1222
1223 int FFMPEG::init_decoder(const char *filename)
1224 {
1225         ff_lock("FFMPEG::init_decoder");
1226         av_register_all();
1227         char file_opts[BCTEXTLEN];
1228         char *bp = strrchr(strcpy(file_opts, filename), '/');
1229         char *sp = strrchr(!bp ? file_opts : bp, '.');
1230         FILE *fp = 0;
1231         if( sp ) {
1232                 strcpy(sp, ".opts");
1233                 fp = fopen(file_opts, "r");
1234         }
1235         if( fp ) {
1236                 read_options(fp, file_opts, opts);
1237                 fclose(fp);
1238         }
1239         else
1240                 load_options("decode.opts", opts);
1241         AVDictionary *fopts = 0;
1242         av_dict_copy(&fopts, opts, 0);
1243         int ret = avformat_open_input(&fmt_ctx, filename, NULL, &fopts);
1244         av_dict_free(&fopts);
1245         if( ret >= 0 )
1246                 ret = avformat_find_stream_info(fmt_ctx, NULL);
1247         if( !ret ) {
1248                 decoding = -1;
1249         }
1250         ff_unlock();
1251         return !ret ? 0 : 1;
1252 }
1253
1254 int FFMPEG::open_decoder()
1255 {
1256         struct stat st;
1257         if( stat(fmt_ctx->filename, &st) < 0 ) {
1258                 eprintf("FFMPEG::open_decoder: can't stat file: %s\n",
1259                         fmt_ctx->filename);
1260                 return 1;
1261         }
1262
1263         int64_t file_bits = 8 * st.st_size;
1264         if( !fmt_ctx->bit_rate && opt_duration > 0 )
1265                 fmt_ctx->bit_rate = file_bits / opt_duration;
1266
1267         int estimated = 0;
1268         if( fmt_ctx->bit_rate > 0 ) {
1269                 for( int i=0; i<(int)fmt_ctx->nb_streams; ++i ) {
1270                         AVStream *st = fmt_ctx->streams[i];
1271                         if( st->duration != AV_NOPTS_VALUE ) continue;
1272                         if( st->time_base.num > INT64_MAX / fmt_ctx->bit_rate ) continue;
1273                         st->duration = av_rescale(file_bits, st->time_base.den,
1274                                 fmt_ctx->bit_rate * (int64_t) st->time_base.num);
1275                         estimated = 1;
1276                 }
1277         }
1278         if( estimated )
1279                 printf("FFMPEG::open_decoder: some stream times estimated\n");
1280
1281         ff_lock("FFMPEG::open_decoder");
1282         int bad_time = 0;
1283         for( int i=0; i<(int)fmt_ctx->nb_streams; ++i ) {
1284                 AVStream *st = fmt_ctx->streams[i];
1285                 if( st->duration == AV_NOPTS_VALUE ) bad_time = 1;
1286                 AVCodecContext *avctx = st->codec;
1287                 if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) {
1288                         has_video = 1;
1289                         FFVideoStream *vid = new FFVideoStream(this, st, i);
1290                         int vidx = ffvideo.size();
1291                         vstrm_index.append(ffidx(vidx, 0));
1292                         ffvideo.append(vid);
1293                         vid->width = avctx->width;
1294                         vid->height = avctx->height;
1295                         AVRational framerate = av_guess_frame_rate(fmt_ctx, st, 0);
1296                         vid->frame_rate = !framerate.den ? 0 : (double)framerate.num / framerate.den;
1297                         double secs = to_secs(st->duration, st->time_base);
1298                         vid->length = secs * vid->frame_rate;
1299                         vid->aspect_ratio = (double)st->sample_aspect_ratio.num / st->sample_aspect_ratio.den;
1300                         vid->nudge = st->start_time;
1301                         vid->reading = -1;
1302                         if( opt_video_filter )
1303                                 vid->create_filter(opt_video_filter, avctx,avctx);
1304                 }
1305                 else if( avctx->codec_type == AVMEDIA_TYPE_AUDIO ) {
1306                         has_audio = 1;
1307                         FFAudioStream *aud = new FFAudioStream(this, st, i);
1308                         int aidx = ffaudio.size();
1309                         ffaudio.append(aud);
1310                         aud->channel0 = astrm_index.size();
1311                         aud->channels = avctx->channels;
1312                         for( int ch=0; ch<aud->channels; ++ch )
1313                                 astrm_index.append(ffidx(aidx, ch));
1314                         aud->sample_rate = avctx->sample_rate;
1315                         double secs = to_secs(st->duration, st->time_base);
1316                         aud->length = secs * aud->sample_rate;
1317                         if( avctx->sample_fmt != AV_SAMPLE_FMT_FLT ) {
1318                                 uint64_t layout = av_get_default_channel_layout(avctx->channels);
1319                                 if( !layout ) layout = ((uint64_t)1<<aud->channels) - 1;
1320                                 aud->resample_context = swr_alloc_set_opts(NULL,
1321                                         layout, AV_SAMPLE_FMT_FLT, avctx->sample_rate,
1322                                         layout, avctx->sample_fmt, avctx->sample_rate,
1323                                         0, NULL);
1324                                 swr_init(aud->resample_context);
1325                         }
1326                         aud->nudge = st->start_time;
1327                         aud->reading = -1;
1328                         if( opt_audio_filter )
1329                                 aud->create_filter(opt_audio_filter, avctx,avctx);
1330                 }
1331         }
1332         if( bad_time )
1333                 printf("FFMPEG::open_decoder: some stream have bad times\n");
1334         ff_unlock();
1335         return 0;
1336 }
1337
1338
1339 int FFMPEG::init_encoder(const char *filename)
1340 {
1341         int fd = ::open(filename,O_WRONLY);
1342         if( fd < 0 ) fd = open(filename,O_WRONLY+O_CREAT,0666);
1343         if( fd < 0 ) {
1344                 eprintf("FFMPEG::init_encoder: bad file path: %s\n", filename);
1345                 return 1;
1346         }
1347         ::close(fd);
1348         int ret = get_file_format();
1349         if( ret > 0 ) {
1350                 eprintf("FFMPEG::init_encoder: bad file format: %s\n", filename);
1351                 return 1;
1352         }
1353         if( ret < 0 ) {
1354                 eprintf("FFMPEG::init_encoder: mismatch audio/video file format: %s\n", filename);
1355                 return 1;
1356         }
1357         ff_lock("FFMPEG::init_encoder");
1358         av_register_all();
1359         avformat_alloc_output_context2(&fmt_ctx, 0, file_format, filename);
1360         if( !fmt_ctx ) {
1361                 eprintf("FFMPEG::init_encoder: failed: %s\n", filename);
1362                 ret = 1;
1363         }
1364         if( !ret ) {
1365                 encoding = -1;
1366                 load_options("encode.opts", opts);
1367         }
1368         ff_unlock();
1369         start_muxer();
1370         return ret;
1371 }
1372
1373 int FFMPEG::open_encoder(const char *type, const char *spec)
1374 {
1375
1376         Asset *asset = file_base->asset;
1377         char *filename = asset->path;
1378         AVDictionary *sopts = 0;
1379         av_dict_copy(&sopts, opts, 0);
1380         char option_path[BCTEXTLEN];
1381         set_option_path(option_path, "%s/%s.opts", type, type);
1382         read_options(option_path, sopts);
1383         get_option_path(option_path, type, spec);
1384         char format_name[BCSTRLEN], codec_name[BCTEXTLEN];
1385         char bsfilter[BCSTRLEN], bsargs[BCTEXTLEN];
1386         if( get_encoder(option_path, format_name, codec_name, bsfilter, bsargs) ) {
1387                 eprintf("FFMPEG::open_encoder: get_encoder failed %s:%s\n",
1388                         option_path, filename);
1389                 return 1;
1390         }
1391
1392         int ret = 0;
1393         ff_lock("FFMPEG::open_encoder");
1394         FFStream *fst = 0;
1395         AVStream *st = 0;
1396
1397         const AVCodecDescriptor *codec_desc = 0;
1398         AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
1399         if( !codec ) {
1400                 eprintf("FFMPEG::open_encoder: cant find codec %s:%s\n",
1401                         codec_name, filename);
1402                 ret = 1;
1403         }
1404         if( !ret ) {
1405                 codec_desc = avcodec_descriptor_get(codec->id);
1406                 if( !codec_desc ) {
1407                         eprintf("FFMPEG::open_encoder: unknown codec %s:%s\n",
1408                                 codec_name, filename);
1409                         ret = 1;
1410                 }
1411         }
1412         if( !ret ) {
1413                 st = avformat_new_stream(fmt_ctx, 0);
1414                 if( !st ) {
1415                         eprintf("FFMPEG::open_encoder: cant create stream %s:%s\n",
1416                                 codec_name, filename);
1417                         ret = 1;
1418                 }
1419         } 
1420         if( !ret ) {
1421                 AVCodecContext *ctx = st->codec;
1422                 switch( codec_desc->type ) {
1423                 case AVMEDIA_TYPE_AUDIO: {
1424                         if( has_audio ) {
1425                                 eprintf("FFMPEG::open_encoder: duplicate audio %s:%s\n",
1426                                         codec_name, filename);
1427                                 ret = 1;
1428                                 break;
1429                         }
1430                         has_audio = 1;
1431                         if( scan_options(asset->ff_audio_options, sopts) ) {
1432                                 eprintf("FFMPEG::open_encoder: bad audio options %s:%s\n",
1433                                         codec_name, filename);
1434                                 ret = 1;
1435                                 break;
1436                         }
1437                         if( asset->ff_audio_bitrate > 0 ) {
1438                                 ctx->bit_rate = asset->ff_audio_bitrate;
1439                                 char arg[BCSTRLEN];
1440                                 sprintf(arg, "%d", asset->ff_audio_bitrate);
1441                                 av_dict_set(&sopts, "b", arg, 0);
1442                         }
1443                         int aidx = ffaudio.size();
1444                         int idx = aidx + ffvideo.size();
1445                         FFAudioStream *aud = new FFAudioStream(this, st, idx);
1446                         ffaudio.append(aud);  fst = aud;
1447                         aud->sample_rate = asset->sample_rate;
1448                         ctx->channels = aud->channels = asset->channels;
1449                         for( int ch=0; ch<aud->channels; ++ch )
1450                                 astrm_index.append(ffidx(aidx, ch));
1451                         ctx->channel_layout =  av_get_default_channel_layout(ctx->channels);
1452                         ctx->sample_rate = check_sample_rate(codec, asset->sample_rate);
1453                         if( !ctx->sample_rate ) {
1454                                 eprintf("FFMPEG::open_audio_encode:"
1455                                         " check_sample_rate failed %s\n", filename);
1456                                 ret = 1;
1457                                 break;
1458                         }
1459                         ctx->time_base = st->time_base = (AVRational){1, aud->sample_rate};
1460                         ctx->sample_fmt = codec->sample_fmts[0];
1461                         uint64_t layout = av_get_default_channel_layout(ctx->channels);
1462                         aud->resample_context = swr_alloc_set_opts(NULL,
1463                                 layout, ctx->sample_fmt, aud->sample_rate,
1464                                 layout, AV_SAMPLE_FMT_FLT, ctx->sample_rate,
1465                                 0, NULL);
1466                         swr_init(aud->resample_context);
1467                         aud->writing = -1;
1468                         break; }
1469                 case AVMEDIA_TYPE_VIDEO: {
1470                         if( has_video ) {
1471                                 eprintf("FFMPEG::open_encoder: duplicate video %s:%s\n",
1472                                         codec_name, filename);
1473                                 ret = 1;
1474                                 break;
1475                         }
1476                         has_video = 1;
1477                         if( scan_options(asset->ff_video_options, sopts) ) {
1478                                 eprintf("FFMPEG::open_encoder: bad video options %s:%s\n",
1479                                         codec_name, filename);
1480                                 ret = 1;
1481                                 break;
1482                         }
1483                         if( asset->ff_video_bitrate > 0 ) {
1484                                 ctx->bit_rate = asset->ff_video_bitrate;
1485                                 char arg[BCSTRLEN];
1486                                 sprintf(arg, "%d", asset->ff_video_bitrate);
1487                                 av_dict_set(&sopts, "b", arg, 0);
1488                         }
1489                         else if( asset->ff_video_quality > 0 ) {
1490                                 ctx->global_quality = asset->ff_video_quality * FF_QP2LAMBDA;
1491                                 ctx->qmin    = ctx->qmax =  asset->ff_video_quality;
1492                                 ctx->mb_lmin = ctx->qmin * FF_QP2LAMBDA;
1493                                 ctx->mb_lmax = ctx->qmax * FF_QP2LAMBDA;
1494                                 ctx->flags |= CODEC_FLAG_QSCALE;
1495                                 char arg[BCSTRLEN];
1496                                 av_dict_set(&sopts, "flags", "+qscale", 0);
1497                                 sprintf(arg, "%d", asset->ff_video_quality);
1498                                 av_dict_set(&sopts, "qscale", arg, 0);
1499                                 sprintf(arg, "%d", ctx->global_quality);
1500                                 av_dict_set(&sopts, "global_quality", arg, 0);
1501                         }
1502                         int vidx = ffvideo.size();
1503                         int idx = vidx + ffaudio.size();
1504                         FFVideoStream *vid = new FFVideoStream(this, st, idx);
1505                         vstrm_index.append(ffidx(vidx, 0));
1506                         ffvideo.append(vid);  fst = vid;
1507                         vid->width = asset->width;
1508                         ctx->width = (vid->width+3) & ~3;
1509                         vid->height = asset->height;
1510                         ctx->height = (vid->height+3) & ~3;
1511                         vid->frame_rate = asset->frame_rate;
1512                         ctx->sample_aspect_ratio = to_sample_aspect_ratio(asset->aspect_ratio);
1513                         ctx->pix_fmt = codec->pix_fmts ? codec->pix_fmts[0] : AV_PIX_FMT_YUV420P;
1514                         AVRational frame_rate = check_frame_rate(codec, vid->frame_rate);
1515                         if( !frame_rate.num || !frame_rate.den ) {
1516                                 eprintf("FFMPEG::open_audio_encode:"
1517                                         " check_frame_rate failed %s\n", filename);
1518                                 ret = 1;
1519                                 break;
1520                         }
1521                         ctx->time_base = (AVRational) { frame_rate.den, frame_rate.num };
1522                         st->time_base = ctx->time_base;
1523                         vid->writing = -1;
1524                         break; }
1525                 default:
1526                         eprintf("FFMPEG::open_encoder: not audio/video, %s:%s\n",
1527                                 codec_name, filename);
1528                         ret = 1;
1529                 }
1530         }
1531         if( !ret ) {
1532                 ret = avcodec_open2(st->codec, codec, &sopts);
1533                 if( ret < 0 ) {
1534                         ff_err(ret,"FFMPEG::open_encoder");
1535                         eprintf("FFMPEG::open_encoder: open failed %s:%s\n",
1536                                 codec_name, filename);
1537                         ret = 1;
1538                 }
1539                 else
1540                         ret = 0;
1541         }
1542         if( !ret ) {
1543                 if( fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER )
1544                         st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
1545                 if( fst && bsfilter[0] )
1546                         fst->add_bsfilter(bsfilter, !bsargs[0] ? 0 : bsargs);
1547         }
1548
1549         ff_unlock();
1550         av_dict_free(&sopts);
1551         return ret;
1552 }
1553
1554 int FFMPEG::close_encoder()
1555 {
1556         stop_muxer();
1557         if( encoding > 0 ) {
1558                 av_write_trailer(fmt_ctx);
1559                 if( !(fmt_ctx->flags & AVFMT_NOFILE) )
1560                         avio_closep(&fmt_ctx->pb);
1561         }
1562         encoding = 0;
1563         return 0;
1564 }
1565
1566 int FFMPEG::decode_activate()
1567 {
1568         if( decoding < 0 ) {
1569                 decoding = 0;
1570                 int npgrms = fmt_ctx->nb_programs;
1571                 for( int i=0; i<npgrms; ++i ) {
1572                         AVProgram *pgrm = fmt_ctx->programs[i];
1573                         // first start time video stream
1574                         int64_t vstart_time = -1;
1575                         for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) {
1576                                 int st_idx = pgrm->stream_index[j];
1577                                 AVStream *st = fmt_ctx->streams[st_idx];
1578                                 AVCodecContext *avctx = st->codec;
1579                                 if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) {
1580                                         if( st->start_time == AV_NOPTS_VALUE ) continue;
1581                                         vstart_time = st->start_time;
1582                                         break;
1583                                 }
1584                         }
1585                         // max start time audio stream
1586                         int64_t astart_time = -1;
1587                         for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) {
1588                                 int st_idx = pgrm->stream_index[j];
1589                                 AVStream *st = fmt_ctx->streams[st_idx];
1590                                 AVCodecContext *avctx = st->codec;
1591                                 if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) {
1592                                         if( st->start_time == AV_NOPTS_VALUE ) continue;
1593                                         if( astart_time > st->start_time ) continue;
1594                                         astart_time = st->start_time;
1595                                 }
1596                         }
1597                         if( astart_time < 0 || vstart_time < 0 ) continue;
1598                         // match program streams to max start_time
1599                         int64_t nudge = vstart_time > astart_time ? vstart_time : astart_time;
1600                         for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) {
1601                                 int st_idx = pgrm->stream_index[j];
1602                                 AVStream *st = fmt_ctx->streams[st_idx];
1603                                 AVCodecContext *avctx = st->codec;
1604                                 if( avctx->codec_type == AVMEDIA_TYPE_AUDIO ) {
1605                                         for( int k=0; k<ffaudio.size(); ++k ) {
1606                                                 if( ffaudio[k]->idx == st_idx )
1607                                                         ffaudio[k]->nudge = nudge;
1608                                         }
1609                                 }
1610                                 else if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) {
1611                                         for( int k=0; k<ffvideo.size(); ++k ) {
1612                                                 if( ffvideo[k]->idx == st_idx )
1613                                                         ffvideo[k]->nudge = nudge;
1614                                         }
1615                                 }
1616                         }
1617                 }
1618                 int64_t vstart_time = 0, astart_time = 0;
1619                 int nstreams = fmt_ctx->nb_streams;
1620                 for( int i=0; i<nstreams; ++i ) {
1621                         AVStream *st = fmt_ctx->streams[i];
1622                         AVCodecContext *avctx = st->codec;
1623                         switch( avctx->codec_type ) {
1624                         case AVMEDIA_TYPE_VIDEO:
1625                                 if( st->start_time == AV_NOPTS_VALUE ) continue;
1626                                 if( vstart_time >= st->start_time ) continue;
1627                                 vstart_time = st->start_time;
1628                                 break;
1629                         case AVMEDIA_TYPE_AUDIO:
1630                                 if( st->start_time == AV_NOPTS_VALUE ) continue;
1631                                 if( astart_time >= st->start_time ) continue;
1632                                 astart_time = st->start_time;
1633                         default: break;
1634                         }
1635                 }
1636                 int64_t nudge = vstart_time > astart_time ? vstart_time : astart_time;
1637                 for( int k=0; k<ffvideo.size(); ++k ) {
1638                         if( ffvideo[k]->nudge != AV_NOPTS_VALUE ) continue;
1639                         ffvideo[k]->nudge = nudge;
1640                 }
1641                 for( int k=0; k<ffaudio.size(); ++k ) {
1642                         if( ffaudio[k]->nudge != AV_NOPTS_VALUE ) continue;
1643                         ffaudio[k]->nudge = nudge;
1644                 }
1645                 decoding = 1;
1646         }
1647         return decoding;
1648 }
1649
1650 int FFMPEG::encode_activate()
1651 {
1652         if( encoding < 0 ) {
1653                 encoding = 0;
1654                 if( !(fmt_ctx->flags & AVFMT_NOFILE) &&
1655                     avio_open(&fmt_ctx->pb, fmt_ctx->filename, AVIO_FLAG_WRITE) < 0 ) {
1656                         fprintf(stderr, "FFMPEG::encode_activate: err opening : %s\n",
1657                                 fmt_ctx->filename);
1658                         return 1;
1659                 }
1660
1661                 AVDictionary *fopts = 0;
1662                 char option_path[BCTEXTLEN];
1663                 set_option_path(option_path, "format/%s", file_format);
1664                 read_options(option_path, fopts);
1665                 int ret = avformat_write_header(fmt_ctx, &fopts);
1666                 av_dict_free(&fopts);
1667                 if( ret < 0 ) {
1668                         fprintf(stderr, "FFMPEG::encode_activate: write header failed %s\n",
1669                                 fmt_ctx->filename);
1670                         return 1;
1671                 }
1672                 encoding = 1;
1673         }
1674         return encoding;
1675 }
1676
1677 int FFMPEG::audio_seek(int stream, int64_t pos)
1678 {
1679         int aidx = astrm_index[stream].st_idx;
1680         FFAudioStream *aud = ffaudio[aidx];
1681         aud->audio_seek(pos);
1682         aud->seek_pos = aud->curr_pos = pos;
1683         return 0;
1684 }
1685
1686 int FFMPEG::video_seek(int stream, int64_t pos)
1687 {
1688         int vidx = vstrm_index[stream].st_idx;
1689         FFVideoStream *vid = ffvideo[vidx];
1690         vid->video_seek(pos);
1691         vid->seek_pos = vid->curr_pos = pos;
1692         return 0;
1693 }
1694
1695
1696 int FFMPEG::decode(int chn, int64_t pos, double *samples, int len)
1697 {
1698         if( !has_audio || chn >= astrm_index.size() ) return -1;
1699         int aidx = astrm_index[chn].st_idx;
1700         FFAudioStream *aud = ffaudio[aidx];
1701         if( aud->load(pos, len) < len ) return -1;
1702         int ch = astrm_index[chn].st_ch;
1703         return aud->read(samples,len,ch);
1704 }
1705
1706 int FFMPEG::decode(int layer, int64_t pos, VFrame *vframe)
1707 {
1708         if( !has_video || layer >= vstrm_index.size() ) return -1;
1709         int vidx = vstrm_index[layer].st_idx;
1710         FFVideoStream *vid = ffvideo[vidx];
1711         return vid->load(vframe, pos);
1712 }
1713
1714 int FFMPEG::encode(int stream, double **samples, int len)
1715 {
1716         FFAudioStream *aud = ffaudio[stream];
1717         return aud->encode(samples, len);
1718 }
1719
1720
1721 int FFMPEG::encode(int stream, VFrame *frame)
1722 {
1723         FFVideoStream *vid = ffvideo[stream];
1724         return vid->encode(frame);
1725 }
1726
1727 void FFMPEG::start_muxer()
1728 {
1729         if( !running() ) {
1730                 done = 0;
1731                 start();
1732         }
1733 }
1734
1735 void FFMPEG::stop_muxer()
1736 {
1737         if( running() ) {
1738                 done = 1;
1739                 mux_lock->unlock();
1740                 join();
1741         }
1742 }
1743
1744 void FFMPEG::flow_off()
1745 {
1746         if( !flow ) return;
1747         flow_lock->lock("FFMPEG::flow_off");
1748         flow = 0;
1749 }
1750
1751 void FFMPEG::flow_on()
1752 {
1753         if( flow ) return;
1754         flow = 1;
1755         flow_lock->unlock();
1756 }
1757
1758 void FFMPEG::flow_ctl()
1759 {
1760         while( !flow ) {
1761                 flow_lock->lock("FFMPEG::flow_ctl");
1762                 flow_lock->unlock();
1763         }
1764 }
1765
1766 int FFMPEG::mux_audio(FFrame *frm)
1767 {
1768         FFPacket pkt;
1769         AVStream *st = frm->fst->st;
1770         AVCodecContext *ctx = st->codec;
1771         AVFrame *frame = *frm;
1772         AVRational tick_rate = {1, ctx->sample_rate};
1773         frame->pts = av_rescale_q(frm->position, tick_rate, ctx->time_base);
1774         int got_packet = 0;
1775         int ret = avcodec_encode_audio2(ctx, pkt, frame, &got_packet);
1776         if( ret >= 0 && got_packet ) {
1777                 frm->fst->bs_filter(pkt);
1778                 av_packet_rescale_ts(pkt, ctx->time_base, st->time_base);
1779                 pkt->stream_index = st->index;
1780                 ret = av_interleaved_write_frame(fmt_ctx, pkt);
1781         }
1782         if( ret < 0 )
1783                 ff_err(ret, "FFMPEG::mux_audio");
1784         return ret >= 0 ? 0 : 1;
1785 }
1786
1787 int FFMPEG::mux_video(FFrame *frm)
1788 {
1789         FFPacket pkt;
1790         AVStream *st = frm->fst->st;
1791         AVFrame *frame = *frm;
1792         frame->pts = frm->position;
1793         int ret = 1, got_packet = 0;
1794         if( fmt_ctx->oformat->flags & AVFMT_RAWPICTURE ) {
1795                 /* a hack to avoid data copy with some raw video muxers */
1796                 pkt->flags |= AV_PKT_FLAG_KEY;
1797                 pkt->stream_index  = st->index;
1798                 AVPicture *picture = (AVPicture *)frame;
1799                 pkt->data = (uint8_t *)picture;
1800                 pkt->size = sizeof(AVPicture);
1801                 pkt->pts = pkt->dts = frame->pts;
1802                 got_packet = 1;
1803         }
1804         else
1805                 ret = avcodec_encode_video2(st->codec, pkt, frame, &got_packet);
1806         if( ret >= 0 && got_packet ) {
1807                 frm->fst->bs_filter(pkt);
1808                 av_packet_rescale_ts(pkt, st->codec->time_base, st->time_base);
1809                 pkt->stream_index = st->index;
1810                 ret = av_interleaved_write_frame(fmt_ctx, pkt);
1811         }
1812         if( ret < 0 )
1813                 ff_err(ret, "FFMPEG::mux_video");
1814         return ret >= 0 ? 0 : 1;
1815 }
1816
1817 void FFMPEG::mux()
1818 {
1819         for(;;) {
1820                 double atm = -1, vtm = -1;
1821                 FFrame *afrm = 0, *vfrm = 0;
1822                 int demand = 0;
1823                 for( int i=0; i<ffaudio.size(); ++i ) {  // earliest audio
1824                         FFStream *fst = ffaudio[i];
1825                         if( fst->frm_count < 3 ) { demand = 1; flow_on(); }
1826                         FFrame *frm = fst->frms.first;
1827                         if( !frm ) { if( !done ) return; continue; }
1828                         double tm = to_secs(frm->position, fst->st->codec->time_base);
1829                         if( atm < 0 || tm < atm ) { atm = tm;  afrm = frm; }
1830                 }
1831                 for( int i=0; i<ffvideo.size(); ++i ) {  // earliest video
1832                         FFStream *fst = ffvideo[i];
1833                         if( fst->frm_count < 2 ) { demand = 1; flow_on(); }
1834                         FFrame *frm = fst->frms.first;
1835                         if( !frm ) { if( !done ) return; continue; }
1836                         double tm = to_secs(frm->position, fst->st->codec->time_base);
1837                         if( vtm < 0 || tm < vtm ) { vtm = tm;  vfrm = frm; }
1838                 }
1839                 if( !demand ) flow_off();
1840                 if( !afrm && !vfrm ) break;
1841                 int v = !afrm ? -1 : !vfrm ? 1 : av_compare_ts(
1842                         vfrm->position, vfrm->fst->st->codec->time_base,
1843                         afrm->position, afrm->fst->st->codec->time_base);
1844                 FFrame *frm = v <= 0 ? vfrm : afrm;
1845                 if( frm == afrm ) mux_audio(frm);
1846                 if( frm == vfrm ) mux_video(frm);
1847                 frm->dequeue();
1848                 delete frm;
1849         }
1850 }
1851
1852 void FFMPEG::run()
1853 {
1854         while( !done ) {
1855                 mux_lock->lock("FFMPEG::run");
1856                 if( !done ) mux();
1857         }
1858         mux();
1859 }
1860
1861
1862 int FFMPEG::ff_total_audio_channels()
1863 {
1864         return astrm_index.size();
1865 }
1866
1867 int FFMPEG::ff_total_astreams()
1868 {
1869         return ffaudio.size();
1870 }
1871
1872 int FFMPEG::ff_audio_channels(int stream)
1873 {
1874         return ffaudio[stream]->channels;
1875 }
1876
1877 int FFMPEG::ff_sample_rate(int stream)
1878 {
1879         return ffaudio[stream]->sample_rate;
1880 }
1881
1882 const char* FFMPEG::ff_audio_format(int stream)
1883 {
1884         AVStream *st = ffaudio[stream]->st;
1885         AVCodecID id = st->codec->codec_id;
1886         const AVCodecDescriptor *desc = avcodec_descriptor_get(id);
1887         return desc ? desc->name : "Unknown";
1888 }
1889
1890 int FFMPEG::ff_audio_pid(int stream)
1891 {
1892         return ffaudio[stream]->st->id;
1893 }
1894
1895 int64_t FFMPEG::ff_audio_samples(int stream)
1896 {
1897         return ffaudio[stream]->length;
1898 }
1899
1900 // find audio astream/channels with this program,
1901 //   or all program audio channels (astream=-1)
1902 int FFMPEG::ff_audio_for_video(int vstream, int astream, int64_t &channel_mask)
1903 {
1904         channel_mask = 0;
1905         int pidx = -1;
1906         int vidx = ffvideo[vstream]->idx;
1907         // find first program with this video stream
1908         for( int i=0; pidx<0 && i<(int)fmt_ctx->nb_programs; ++i ) {
1909                 AVProgram *pgrm = fmt_ctx->programs[i];
1910                 for( int j=0;  pidx<0 && j<(int)pgrm->nb_stream_indexes; ++j ) {
1911                         int st_idx = pgrm->stream_index[j];
1912                         AVStream *st = fmt_ctx->streams[st_idx];
1913                         if( st->codec->codec_type != AVMEDIA_TYPE_VIDEO ) continue;
1914                         if( st_idx == vidx ) pidx = i;
1915                 }
1916         }
1917         if( pidx < 0 ) return -1;
1918         int ret = -1;
1919         int64_t channels = 0;
1920         AVProgram *pgrm = fmt_ctx->programs[pidx];
1921         for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) {
1922                 int aidx = pgrm->stream_index[j];
1923                 AVStream *st = fmt_ctx->streams[aidx];
1924                 if( st->codec->codec_type != AVMEDIA_TYPE_AUDIO ) continue;
1925                 if( astream > 0 ) { --astream;  continue; }
1926                 int astrm = -1;
1927                 for( int i=0; astrm<0 && i<ffaudio.size(); ++i )
1928                         if( ffaudio[i]->idx == aidx ) astrm = i;
1929                 if( astrm >= 0 ) {
1930                         if( ret < 0 ) ret = astrm;
1931                         int64_t mask = (1 << ffaudio[astrm]->channels) - 1;
1932                         channels |= mask << ffaudio[astrm]->channel0;
1933                 }
1934                 if( !astream ) break;
1935         }
1936         channel_mask = channels;
1937         return ret;
1938 }
1939
1940
1941 int FFMPEG::ff_total_video_layers()
1942 {
1943         return vstrm_index.size();
1944 }
1945
1946 int FFMPEG::ff_total_vstreams()
1947 {
1948         return ffvideo.size();
1949 }
1950
1951 int FFMPEG::ff_video_width(int stream)
1952 {
1953         return ffvideo[stream]->width;
1954 }
1955
1956 int FFMPEG::ff_video_height(int stream)
1957 {
1958         return ffvideo[stream]->height;
1959 }
1960
1961 int FFMPEG::ff_set_video_width(int stream, int width)
1962 {
1963         int w = ffvideo[stream]->width;
1964         ffvideo[stream]->width = width;
1965         return w;
1966 }
1967
1968 int FFMPEG::ff_set_video_height(int stream, int height)
1969 {
1970         int h = ffvideo[stream]->height;
1971         ffvideo[stream]->height = height;
1972         return h;
1973 }
1974
1975 int FFMPEG::ff_coded_width(int stream)
1976 {
1977         AVStream *st = ffvideo[stream]->st;
1978         return st->codec->coded_width;
1979 }
1980
1981 int FFMPEG::ff_coded_height(int stream)
1982 {
1983         AVStream *st = ffvideo[stream]->st;
1984         return st->codec->coded_height;
1985 }
1986
1987 float FFMPEG::ff_aspect_ratio(int stream)
1988 {
1989         return ffvideo[stream]->aspect_ratio;
1990 }
1991
1992 const char* FFMPEG::ff_video_format(int stream)
1993 {
1994         AVStream *st = ffvideo[stream]->st;
1995         AVCodecID id = st->codec->codec_id;
1996         const AVCodecDescriptor *desc = avcodec_descriptor_get(id);
1997         return desc ? desc->name : "Unknown";
1998 }
1999
2000 double FFMPEG::ff_frame_rate(int stream)
2001 {
2002         return ffvideo[stream]->frame_rate;
2003 }
2004
2005 int64_t FFMPEG::ff_video_frames(int stream)
2006 {
2007         return ffvideo[stream]->length;
2008 }
2009
2010 int FFMPEG::ff_video_pid(int stream)
2011 {
2012         return ffvideo[stream]->st->id;
2013 }
2014
2015
2016 int FFMPEG::ff_cpus()
2017 {
2018         return file_base->file->cpus;
2019 }
2020
2021 int FFVideoStream::create_filter(const char *filter_spec,
2022                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx)
2023 {
2024         avfilter_register_all();
2025         filter_graph = avfilter_graph_alloc();
2026         AVFilter *buffersrc = avfilter_get_by_name("buffer");
2027         AVFilter *buffersink = avfilter_get_by_name("buffersink");
2028
2029         int ret = 0;  char args[BCTEXTLEN];
2030         snprintf(args, sizeof(args),
2031                 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
2032                 src_ctx->width, src_ctx->height, src_ctx->pix_fmt,
2033                 st->time_base.num, st->time_base.den,
2034                 src_ctx->sample_aspect_ratio.num, src_ctx->sample_aspect_ratio.den);
2035         if( ret >= 0 )
2036                 ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
2037                         args, NULL, filter_graph);
2038         if( ret >= 0 )
2039                 ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
2040                         NULL, NULL, filter_graph);
2041         if( ret >= 0 )
2042                 ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
2043                         (uint8_t*)&sink_ctx->pix_fmt, sizeof(sink_ctx->pix_fmt),
2044                         AV_OPT_SEARCH_CHILDREN);
2045         if( ret < 0 )
2046                 ff_err(ret, "FFVideoStream::create_filter");
2047         else
2048                 ret = FFStream::create_filter(filter_spec);
2049         return ret >= 0 ? 0 : 1;
2050 }
2051
2052 int FFAudioStream::create_filter(const char *filter_spec,
2053                 AVCodecContext *src_ctx, AVCodecContext *sink_ctx)
2054 {
2055         avfilter_register_all();
2056         filter_graph = avfilter_graph_alloc();
2057         AVFilter *buffersrc = avfilter_get_by_name("abuffer");
2058         AVFilter *buffersink = avfilter_get_by_name("abuffersink");
2059         int ret = 0;  char args[BCTEXTLEN];
2060         snprintf(args, sizeof(args),
2061                 "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%jx",
2062                 st->time_base.num, st->time_base.den, src_ctx->sample_rate,
2063                 av_get_sample_fmt_name(src_ctx->sample_fmt), src_ctx->channel_layout);
2064         if( ret >= 0 )
2065                 ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
2066                         args, NULL, filter_graph);
2067         if( ret >= 0 )
2068                 ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
2069                         NULL, NULL, filter_graph);
2070         if( ret >= 0 )
2071                 ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
2072                         (uint8_t*)&sink_ctx->sample_fmt, sizeof(sink_ctx->sample_fmt),
2073                         AV_OPT_SEARCH_CHILDREN);
2074         if( ret >= 0 )
2075                 ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
2076                         (uint8_t*)&sink_ctx->channel_layout,
2077                         sizeof(sink_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
2078         if( ret >= 0 )
2079                 ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
2080                         (uint8_t*)&sink_ctx->sample_rate, sizeof(sink_ctx->sample_rate),
2081                         AV_OPT_SEARCH_CHILDREN);
2082         if( ret < 0 )
2083                 ff_err(ret, "FFAudioStream::create_filter");
2084         else
2085                 ret = FFStream::create_filter(filter_spec);
2086         return ret >= 0 ? 0 : 1;
2087 }
2088
2089 int FFStream::create_filter(const char *filter_spec)
2090 {
2091         /* Endpoints for the filter graph. */
2092         AVFilterInOut *outputs = avfilter_inout_alloc();
2093         outputs->name = av_strdup("in");
2094         outputs->filter_ctx = buffersrc_ctx;
2095         outputs->pad_idx = 0;
2096         outputs->next = 0;
2097
2098         AVFilterInOut *inputs  = avfilter_inout_alloc();
2099         inputs->name = av_strdup("out");
2100         inputs->filter_ctx = buffersink_ctx;
2101         inputs->pad_idx = 0;
2102         inputs->next = 0;
2103
2104         int ret = !outputs->name || !inputs->name ? -1 : 0;
2105         if( ret >= 0 )
2106                 ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
2107                         &inputs, &outputs, NULL);
2108         if( ret >= 0 )
2109                 ret = avfilter_graph_config(filter_graph, NULL);
2110
2111         if( ret < 0 )
2112                 ff_err(ret, "FFStream::create_filter");
2113         avfilter_inout_free(&inputs);
2114         avfilter_inout_free(&outputs);
2115         return ret;
2116 }
2117
2118 void FFStream::add_bsfilter(const char *bsf, const char *ap)
2119 {
2120         bsfilter.append(new BSFilter(bsf,ap));
2121 }
2122
2123 int FFStream::bs_filter(AVPacket *pkt)
2124 {
2125         if( !bsfilter.size() ) return 0;
2126         av_packet_split_side_data(pkt);
2127
2128         int ret = 0;
2129         for( int i=0; i<bsfilter.size(); ++i ) {
2130                 AVPacket bspkt = *pkt;
2131                 ret = av_bitstream_filter_filter(bsfilter[i]->bsfc,
2132                          st->codec, bsfilter[i]->args, &bspkt.data, &bspkt.size,
2133                          pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY);
2134                 if( ret < 0 ) break;
2135                 int size = bspkt.size;
2136                 uint8_t *data = bspkt.data;
2137                 if( !ret && bspkt.data != pkt->data ) {
2138                         size = bspkt.size;
2139                         data = (uint8_t *)av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
2140                         if( !data ) { ret = AVERROR(ENOMEM);  break; }
2141                         memcpy(data, bspkt.data, size);
2142                         memset(data+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2143                         ret = 1;
2144                 }
2145                 if( ret > 0 ) {
2146                         pkt->side_data = 0;  pkt->side_data_elems = 0;
2147                         av_free_packet(pkt);
2148                         ret = av_packet_from_data(&bspkt, data, size);
2149                         if( ret < 0 ) break;
2150                 }
2151                 *pkt = bspkt;
2152         }
2153         if( ret < 0 )
2154                 ff_err(ret,"FFStream::bs_filter");
2155         return ret;
2156 }
2157