summaryrefslogtreecommitdiff
path: root/plugins/ffmpeg/ffmpeg.c
blob: 89917c83aede288cda8ad5945a894c20dcaa70b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>

    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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <alloca.h>
#include <errno.h>

#include "../../deadbeef.h"

#if !FFMPEG_OLD

#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/avstring.h>

#else

#include <ffmpeg/avformat.h>
#include <ffmpeg/avcodec.h>
#include <ffmpeg/avutil.h>
#include <ffmpeg/avstring.h>
#define AVERROR_EOF AVERROR(EPIPE)

#if LIBAVFORMAT_VERSION_MAJOR < 53
#define av_register_protocol register_protocol
#endif

#endif

//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)

#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))

static DB_decoder_t plugin;
static DB_functions_t *deadbeef;

static const char * exts[] = { "m4a", "wma", "aa3", "oma", "ac3", "vqf", "amr", NULL };

enum {
    FT_ALAC = 0,
    FT_WMA = 1,
    FT_ATRAC3 = 2,
    FT_VQF = 3,
    FT_AC3 = 4,
    FT_AMR = 5,
    FT_UNKNOWN = 5
};

static const char *filetypes[] = { "ALAC", "WMA", "ATRAC3", "VQF", "AC3", "AMR", "FFMPEG (unknown)", NULL };

#define FF_PROTOCOL_NAME "deadbeef"

typedef struct {
    DB_fileinfo_t info;
    AVCodec *codec;
    AVCodecContext *ctx;
    AVFormatContext *fctx;
    AVPacket pkt;
    int stream_id;

    int left_in_packet;
    int have_packet;

    char *buffer; // must be AVCODEC_MAX_AUDIO_FRAME_SIZE
    int left_in_buffer;

    int startsample;
    int endsample;
    int currentsample;
} ffmpeg_info_t;

static DB_playItem_t *current_track;
static DB_fileinfo_t *current_info;

static DB_fileinfo_t *
ffmpeg_open (void) {
    DB_fileinfo_t *_info = malloc (sizeof (ffmpeg_info_t));
    memset (_info, 0, sizeof (ffmpeg_info_t));
    return _info;
}

static int
ffmpeg_init (DB_fileinfo_t *_info, DB_playItem_t *it) {
    ffmpeg_info_t *info = (ffmpeg_info_t *)_info;
    trace ("ffmpeg init %s\n", it->fname);
    // prepare to decode the track
    // return -1 on failure

    int ret;
    int l = strlen (it->fname);
    char *uri = alloca (l + sizeof (FF_PROTOCOL_NAME) + 1);
    int i;

    // construct uri
    memcpy (uri, FF_PROTOCOL_NAME, sizeof (FF_PROTOCOL_NAME)-1);
    memcpy (uri + sizeof (FF_PROTOCOL_NAME)-1, ":", 1);
    memcpy (uri + sizeof (FF_PROTOCOL_NAME), it->fname, l);
    uri[sizeof (FF_PROTOCOL_NAME) + l] = 0;
    trace ("ffmpeg: uri: %s\n", uri);

    // open file
    trace ("\033[0;31mffmpeg av_open_input_file\033[37;0m\n");
    current_track = it;
    current_info = _info;
    if ((ret = av_open_input_file(&info->fctx, uri, NULL, 0, NULL)) < 0) {
        current_track = NULL;
        trace ("\033[0;31minfo->fctx is %p, ret %d/%s\033[0;31m\n", info->fctx, ret, strerror(-ret));
        return -1;
    }
    trace ("\033[0;31mav_open_input_file done, ret=%d\033[0;31m\n", ret);
    current_track = NULL;
    current_info = NULL;

    trace ("\033[0;31mffmpeg av_find_stream_info\033[37;0m\n");
    info->stream_id = -1;
    av_find_stream_info(info->fctx);
    for (i = 0; i < info->fctx->nb_streams; i++)
    {
        info->ctx = info->fctx->streams[i]->codec;
        if (info->ctx->codec_type == CODEC_TYPE_AUDIO)
        {
            info->codec = avcodec_find_decoder (info->ctx->codec_id);
            if (info->codec != NULL) {
                info->stream_id = i;
                break;
            }
        }
    }

    if (info->codec == NULL)
    {
        trace ("ffmpeg can't decode %s\n", it->fname);
        return -1;
    }
    trace ("ffmpeg can decode %s\n", it->fname);
    trace ("ffmpeg: codec=%s, stream=%d\n", info->codec->name, i);

    if (avcodec_open (info->ctx, info->codec) < 0) {
        trace ("ffmpeg: avcodec_open failed\n");
        return -1;
    }

    if (!strcasecmp (info->codec->name, "alac")) {
        it->filetype = filetypes[FT_ALAC];
    }
    else if (strcasestr (info->codec->name, "wma")) {
        it->filetype = filetypes[FT_WMA];
    }
    else if (strcasestr (info->codec->name, "ac3")) {
        it->filetype = filetypes[FT_AC3];
    }
    else if (strcasestr (info->codec->name, "amr")) {
        it->filetype = filetypes[FT_AMR];
    }
    else {
        it->filetype = filetypes[FT_UNKNOWN];
    }

    int bps = av_get_bits_per_sample_format (info->ctx->sample_fmt);
    int samplerate = info->ctx->sample_rate;
    float duration = info->fctx->duration / (float)AV_TIME_BASE;
    trace ("ffmpeg: bits per sample is %d\n", bps);
    trace ("ffmpeg: samplerate is %d\n", samplerate);
    trace ("ffmpeg: duration is %lld/%fsec\n", info->fctx->duration, duration);

    int totalsamples = info->fctx->duration * samplerate / AV_TIME_BASE;
    info->left_in_packet = 0;
    info->left_in_buffer = 0;

    memset (&info->pkt, 0, sizeof (info->pkt));
    info->have_packet = 0;

    int err = posix_memalign ((void **)&info->buffer, 16, AVCODEC_MAX_AUDIO_FRAME_SIZE);
    if (err) {
        fprintf (stderr, "ffmpeg: failed to allocate buffer memory\n");
        return -1;
    }

    // fill in mandatory plugin fields
    _info->plugin = &plugin;
    _info->readpos = 0;
    _info->bps = bps;
    _info->channels = info->ctx->channels;
    _info->samplerate = samplerate;

    // subtrack info
    info->currentsample = 0;
    if (it->endsample > 0) {
        info->startsample = it->startsample;
        info->endsample = it->endsample;
        plugin.seek_sample (_info, 0);
    }
    else {
        info->startsample = 0;
        info->endsample = totalsamples - 1;
    }
    return 0;
}

static void
ffmpeg_free (DB_fileinfo_t *_info) {
    trace ("ffmpeg: free\n");
    ffmpeg_info_t *info = (ffmpeg_info_t*)_info;
    if (info) {
        if (info->buffer) {
            free (info->buffer);
        }
        // free everything allocated in _init and _read_int16
        if (info->have_packet) {
            av_free_packet (&info->pkt);
        }
        if (info->ctx) {
            avcodec_close (info->ctx);
        }
        if (info->fctx) {
            av_close_input_file (info->fctx);
        }
        free (info);
    }
}

static int
ffmpeg_read_int16 (DB_fileinfo_t *_info, char *bytes, int size) {
    trace ("ffmpeg_read_int16 %d\n", size);
    ffmpeg_info_t *info = (ffmpeg_info_t*)_info;

    int out_ch = min (2, _info->channels);

    if (info->endsample >= 0 && info->currentsample + size / (2 * out_ch) > info->endsample) {
        size = (info->endsample - info->currentsample + 1) * 2 * out_ch;
        if (size <= 0) {
            return 0;
        }
    }

    int initsize = size;

    int encsize = 0;
    int decsize = 0;

    while (size > 0) {

        if (info->left_in_buffer > 0) {
//            int sz = min (size, info->left_in_buffer);
            int nsamples = size / out_ch / 2;
            int nsamples_buf = info->left_in_buffer / 2 / _info->channels;
            nsamples = min (nsamples, nsamples_buf);
            int sz = nsamples * 2 * _info->channels;
            for (int i = 0; i < nsamples; i++) {
                *((int16_t*)bytes) = ((int16_t*)info->buffer)[i * _info->channels + 0];
                bytes += 2;
                size -= 2;
                if (out_ch > 1) {
                    *((int16_t*)bytes) = ((int16_t*)info->buffer)[i * _info->channels + 1];
                    bytes += 2;
                    size -= 2;
                }
            }
            if (sz != info->left_in_buffer) {
                memmove (info->buffer, info->buffer+sz, info->left_in_buffer-sz);
            }
            info->left_in_buffer -= sz;
        }

        while (info->left_in_packet > 0 && size > 0) {
            int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
            int len;
            //trace ("in: out_size=%d(%d), size=%d\n", out_size, AVCODEC_MAX_AUDIO_FRAME_SIZE, size);
#if (LIBAVCODEC_VERSION_MAJOR <= 52) && (LIBAVCODEC_VERSION_MINOR <= 25)
            len = avcodec_decode_audio2 (info->ctx, (int16_t *)info->buffer, &out_size, info->pkt.data, info->pkt.size);
#else
            len = avcodec_decode_audio3 (info->ctx, (int16_t *)info->buffer, &out_size, &info->pkt);
#endif
            trace ("out: out_size=%d, len=%d\n", out_size, len);
            if (len <= 0) {
                break;
            }
            encsize += len;
            decsize += out_size;
            info->left_in_packet -= len;
            info->left_in_buffer = out_size;
        }
        if (size == 0) {
            break;
        }

        // read next packet
        if (info->have_packet) {
            av_free_packet (&info->pkt);
            info->have_packet = 0;
        }
        int errcount = 0;
        for (;;) {
            int ret;
            if ((ret = av_read_frame (info->fctx, &info->pkt)) < 0) {
                trace ("ffmpeg: error %d\n", ret);
                if (ret == AVERROR_EOF || ret == -1) {
                    ret = -1;
                    break;
                }
                else {
                    if (++errcount > 4) {
                        trace ("ffmpeg: too many errors in a row (last is %d); interrupting stream\n", ret);
                        ret = -1;
                        break;
                    }
                    else {
                        continue;
                    }
                }
            }
            else {
                trace ("av packet size: %d, numframes: %d\n", info->pkt.size, ret);
                errcount = 0;
            }
            if (ret == -1) {
                break;
            }
            //trace ("idx:%d, stream:%d\n", info->pkt.stream_index, info->stream_id);
            if (info->pkt.stream_index != info->stream_id) {
                av_free_packet (&info->pkt);
                continue;
            }
            //trace ("got packet: size=%d\n", info->pkt.size);
            info->have_packet = 1;
            info->left_in_packet = info->pkt.size;

            if (info->pkt.duration > 0) {
                AVRational *time_base = &info->fctx->streams[info->stream_id]->time_base;
                float sec = (float)info->pkt.duration * time_base->num / time_base->den;
                int bitrate = info->pkt.size/sec;
                if (bitrate > 0) {
                    // FIXME: seems like duration translation is wrong
                    deadbeef->streamer_set_bitrate (bitrate / 100);
                }
            }

            break;
        }
        if (!info->have_packet) {
            break;
        }
    }

    info->currentsample += (initsize-size) / (2 * out_ch);
    _info->readpos = (float)info->currentsample / _info->samplerate;

    return initsize-size;
}

static int
ffmpeg_seek_sample (DB_fileinfo_t *_info, int sample) {
    ffmpeg_info_t *info = (ffmpeg_info_t*)_info;
    // seek to specified sample (frame)
    // return 0 on success
    // return -1 on failure
    if (info->have_packet) {
        av_free_packet (&info->pkt);
        info->have_packet = 0;
    }
    sample += info->startsample;
    int64_t tm = (int64_t)sample/ _info->samplerate * AV_TIME_BASE;
    trace ("ffmpeg: seek to sample: %d, t: %d\n", sample, (int)tm);
    info->left_in_packet = 0;
    info->left_in_buffer = 0;
    if (av_seek_frame (info->fctx, -1, tm, AVSEEK_FLAG_ANY) < 0) {
        trace ("ffmpeg: seek error\n");
        return -1;
    }
    
    // update readpos
    info->currentsample = sample;
    _info->readpos = (float)(sample - info->startsample) / _info->samplerate;
    return 0;
}

static int
ffmpeg_seek (DB_fileinfo_t *_info, float time) {
    ffmpeg_info_t *info = (ffmpeg_info_t*)_info;
    // seek to specified time in seconds
    // return 0 on success
    // return -1 on failure
    return ffmpeg_seek_sample (_info, time * _info->samplerate);
}

static const char *map[] = {
    "artist", "artist",
    "title", "title",
    "album", "album",
    "track", "track",
    "date", "year",
    "genre", "genre",
    "comment", "comment",
    "performer", "performer",
    "album_artist", "band",
    "composer", "composer",
    "encoder", "encoder",
    "encoded_by", "vendor",
    "disc", "disc",
    "copyright", "copyright",
    "tracktotal", "numtracks",
    "publisher", "publisher",
    NULL
};

static int
ffmpeg_read_metadata_internal (DB_playItem_t *it, AVFormatContext *fctx) {
#if LIBAVFORMAT_VERSION_MAJOR <= 52 && LIBAVFORMAT_VERSION_MINOR < 43
    if (!strlen (fctx->title)) {
        // title is empty, this call will set track title to filename without extension
        deadbeef->pl_add_meta (it, "title", NULL);
    }
    else {
        deadbeef->pl_add_meta (it, "title", fctx->title);
    }
    deadbeef->pl_add_meta (it, "artist", fctx->author);
    deadbeef->pl_add_meta (it, "album", fctx->album);
    deadbeef->pl_add_meta (it, "copyright", fctx->copyright);
    deadbeef->pl_add_meta (it, "comment", fctx->comment);
    deadbeef->pl_add_meta (it, "genre", fctx->genre);

    char tmp[10];
    snprintf (tmp, sizeof (tmp), "%d", fctx->year);
    deadbeef->pl_add_meta (it, "year", tmp);
    snprintf (tmp, sizeof (tmp), "%d", fctx->track);
    deadbeef->pl_add_meta (it, "track", tmp);
#else
// read using other means?
// av_metadata_get?
    AVMetadata *md = fctx->metadata;

    for (int m = 0; map[m]; m += 2) {
        AVMetadataTag *tag = NULL;
        do {
            tag = av_metadata_get (md, map[m], tag, AV_METADATA_DONT_STRDUP_KEY | AV_METADATA_DONT_STRDUP_VAL);
            if (tag) {
                deadbeef->pl_append_meta (it, map[m+1], tag->value);
            }
        } while (tag);
    }
    deadbeef->pl_add_meta (it, "title", NULL);
#endif
    return 0;
}

static DB_playItem_t *
ffmpeg_insert (DB_playItem_t *after, const char *fname) {
    trace ("ffmpeg_insert %s\n", fname);
    // read information from the track
    // load/process cuesheet if exists
    // insert track into playlist
    // return track pointer on success
    // return NULL on failure

    AVCodec *codec = NULL;
    AVCodecContext *ctx = NULL;
    AVFormatContext *fctx = NULL;
    int ret;
    int l = strlen (fname);
    char *uri = alloca (l + sizeof (FF_PROTOCOL_NAME) + 1);
    int i;

    // construct uri
    memcpy (uri, FF_PROTOCOL_NAME, sizeof (FF_PROTOCOL_NAME)-1);
    memcpy (uri + sizeof (FF_PROTOCOL_NAME)-1, ":", 1);
    memcpy (uri + sizeof (FF_PROTOCOL_NAME), fname, l);
    uri[sizeof (FF_PROTOCOL_NAME) + l] = 0;
    trace ("ffmpeg: uri: %s\n", uri);

    // open file
    if ((ret = av_open_input_file(&fctx, uri, NULL, 0, NULL)) < 0) {
        trace ("fctx is %p, ret %d/%s", fctx, ret, strerror(-ret));
        return NULL;
    }

    av_find_stream_info(fctx);
    for (i = 0; i < fctx->nb_streams; i++)
    {
        ctx = fctx->streams[i]->codec;
        if (ctx->codec_type == CODEC_TYPE_AUDIO)
        {
            codec = avcodec_find_decoder(ctx->codec_id);
            if (codec != NULL && !strcasecmp (codec->name, "alac")) { // only open alac streams
                break;
            }
        }
    }
//    AVStream *stream = fctx->streams[i];

    if (codec == NULL)
    {
        trace ("ffmpeg can't decode %s\n", fname);
        av_close_input_file(fctx);
        return NULL;
    }
    trace ("ffmpeg can decode %s\n", fname);
    trace ("ffmpeg: codec=%s, stream=%d\n", codec->name, i);

    if (avcodec_open (ctx, codec) < 0) {
        trace ("ffmpeg: avcodec_open failed\n");
        av_close_input_file(fctx);
        return NULL;
    }

    int bps = av_get_bits_per_sample_format (ctx->sample_fmt);
    int samplerate = ctx->sample_rate;
    float duration = fctx->duration / (float)AV_TIME_BASE;
//    float duration = stream->duration * stream->time_base.num / (float)stream->time_base.den;
    trace ("ffmpeg: bits per sample is %d\n", bps);
    trace ("ffmpeg: samplerate is %d\n", samplerate);
    trace ("ffmpeg: duration is %f\n", duration);

    int totalsamples = fctx->duration * samplerate / AV_TIME_BASE;

    DB_playItem_t *it = deadbeef->pl_item_alloc ();
    it->decoder_id = deadbeef->plug_get_decoder_id (plugin.plugin.id);
    it->fname = strdup (fname);
    // FIXME: get proper codec
    it->filetype = filetypes[FT_UNKNOWN];

    if (!deadbeef->is_local_file (it->fname)) {
        deadbeef->pl_set_item_duration (it, -1);
    }
    else {
        deadbeef->pl_set_item_duration (it, duration);
    }

    // add metainfo
    ffmpeg_read_metadata_internal (it, fctx);
    // free decoder
    avcodec_close (ctx);
    av_close_input_file(fctx);

    // external cuesheet
    DB_playItem_t *cue = deadbeef->pl_insert_cue (after, it, totalsamples, samplerate);
    if (cue) {
        deadbeef->pl_item_unref (it);
        deadbeef->pl_item_unref (cue);
        return cue;
    }
    // now the track is ready, insert into playlist
    after = deadbeef->pl_insert_item (after, it);
    deadbeef->pl_item_unref (it);
    return after;
}

// vfs wrapper for ffmpeg
static int
ffmpeg_vfs_open(URLContext *h, const char *filename, int flags)
{
    DB_FILE *f;
    av_strstart(filename, FF_PROTOCOL_NAME ":", &filename);
    if (flags & URL_WRONLY) {
        return -ENOENT;
    } else {
        f = deadbeef->fopen (filename);
    }

    if (f == NULL)
        return -ENOENT;

    if (f->vfs->streaming) {
        deadbeef->fset_track (f, current_track);
        if (current_info) {
            current_info->file = f;
        }
    }

    h->priv_data = f;
    return 0;
}

static int
ffmpeg_vfs_read(URLContext *h, unsigned char *buf, int size)
{
    trace ("ffmpeg_vfs_read %d\n", size);
    int res = deadbeef->fread (buf, 1, size, h->priv_data);
    return res;
}

static int
ffmpeg_vfs_write(URLContext *h, const unsigned char *buf, int size)
{
    return -1;
}

static int64_t
ffmpeg_vfs_seek(URLContext *h, int64_t pos, int whence)
{
    trace ("ffmpeg_vfs_seek %d %d\n", pos, whence);
    DB_FILE *f = h->priv_data;

    if (whence == AVSEEK_SIZE) {
        return f->vfs->streaming ? -1 : deadbeef->fgetlength (h->priv_data);
    }
    else if (f->vfs->streaming) {
        return -1;
    }
    else {
        int ret = deadbeef->fseek (h->priv_data, pos, whence);
        return ret;
    }
}

static int
ffmpeg_vfs_close(URLContext *h)
{
    trace ("ffmpeg_vfs_close\n");
    deadbeef->fclose (h->priv_data);
    return 0;
}

static URLProtocol vfswrapper = {
    .name = FF_PROTOCOL_NAME,
    .url_open = ffmpeg_vfs_open,
    .url_read = ffmpeg_vfs_read,
    .url_write = ffmpeg_vfs_write,
    .url_seek = ffmpeg_vfs_seek,
    .url_close = ffmpeg_vfs_close,
};

static int
ffmpeg_start (void) {
    // do one-time plugin initialization here
    // e.g. starting threads for background processing, subscribing to events, etc
    // return 0 on success
    // return -1 on failure
    avcodec_init ();
    av_register_all ();
    av_register_protocol (&vfswrapper);
    return 0;
}

static int
ffmpeg_stop (void) {
    // undo everything done in _start here
    // return 0 on success
    // return -1 on failure
    trace ("ffmpeg stop\n");
    return 0;
}

int
ffmpeg_read_metadata (DB_playItem_t *it) {
    trace ("ffmpeg_read_metadata: fname %s\n", it->fname);
    AVCodec *codec = NULL;
    AVCodecContext *ctx = NULL;
    AVFormatContext *fctx = NULL;
    int ret;
    int l = strlen (it->fname);
    char *uri = alloca (l + sizeof (FF_PROTOCOL_NAME) + 1);
    int i;

    // construct uri
    memcpy (uri, FF_PROTOCOL_NAME, sizeof (FF_PROTOCOL_NAME)-1);
    memcpy (uri + sizeof (FF_PROTOCOL_NAME)-1, ":", 1);
    memcpy (uri + sizeof (FF_PROTOCOL_NAME), it->fname, l);
    uri[sizeof (FF_PROTOCOL_NAME) + l] = 0;
    trace ("ffmpeg: uri: %s\n", uri);

    // open file
    if ((ret = av_open_input_file(&fctx, uri, NULL, 0, NULL)) < 0) {
        trace ("fctx is %p, ret %d/%s", fctx, ret, strerror(-ret));
        return -1;
    }

    av_find_stream_info(fctx);
    for (i = 0; i < fctx->nb_streams; i++)
    {
        ctx = fctx->streams[i]->codec;
        if (ctx->codec_type == CODEC_TYPE_AUDIO)
        {
            codec = avcodec_find_decoder(ctx->codec_id);
            if (codec != NULL)
                break;
        }
    }
    if (codec == NULL)
    {
        trace ("ffmpeg can't decode %s\n", it->fname);
        av_close_input_file(fctx);
        return -1;
    }
    if (avcodec_open (ctx, codec) < 0) {
        trace ("ffmpeg: avcodec_open failed\n");
        av_close_input_file(fctx);
        return -1;
    }

    deadbeef->pl_delete_all_meta (it);
    ffmpeg_read_metadata_internal (it, fctx);

    av_close_input_file(fctx);
    return 0;
}

// define plugin interface
static DB_decoder_t plugin = {
    DB_PLUGIN_SET_API_VERSION
    .plugin.version_major = 1,
    .plugin.version_minor = 2,
    .plugin.type = DB_PLUGIN_DECODER,
    .plugin.id = "ffmpeg",
    .plugin.name = "FFMPEG audio player",
    .plugin.descr = "decodes audio formats using FFMPEG libavcodec",
    .plugin.author = "Alexey Yakovenko",
    .plugin.email = "waker@users.sourceforge.net",
    .plugin.website = "http://deadbeef.sf.net",
    .plugin.start = ffmpeg_start,
    .plugin.stop = ffmpeg_stop,
    .open = ffmpeg_open,
    .init = ffmpeg_init,
    .free = ffmpeg_free,
    .read_int16 = ffmpeg_read_int16,
//    .read_float32 = ffmpeg_read_float32,
    .seek = ffmpeg_seek,
    .seek_sample = ffmpeg_seek_sample,
    .insert = ffmpeg_insert,
    .read_metadata = ffmpeg_read_metadata,
    .exts = exts,
    .filetypes = filetypes
};

DB_plugin_t *
ffmpeg_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}