forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg_player_decoding.cpp
1343 lines (1118 loc) · 37.6 KB
/
ffmpeg_player_decoding.cpp
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ffmpeg player decoding
// part of MusicPlayer, https://github.com/albertz/music-player
// Copyright (c) 2012, Albert Zeyer, www.az2000.de
// All rights reserved.
// This code is under the 2-clause BSD license, see License.txt in the root directory of this project.
#include "ffmpeg.h"
extern "C" {
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
}
#include <math.h>
#include <unistd.h>
#include <vector>
#define PROCESS_SIZE (BUFFER_CHUNK_SIZE * 10) // how much data to proceed in processInStream()
#define BUFFER_FILL_SIZE (48000 * 2 * 2 * 10) // 10secs for 48kHz,stereo - around 2MB
#define PEEKSTREAM_NUM 3
int initPlayerDecoder() {
av_log_set_level(0);
avcodec_register_all();
av_register_all();
return 0;
}
/*
For values y < 0, mirror.
For values y in [0,x1], this is just y (i.e. identity function).
For values y >= x2, this is just 1 (i.e. constant 1 function).
For y in [x1,x2], we use a cubic spline interpolation to just make it smooth.
Use smoothClip_setX() to set the spline factors.
*/
double SmoothClipCalc::get(double y) {
SmoothClipCalc* s = this;
if(y < 0) return -get(-y);
if(y <= s->x1) return y;
if(y >= s->x2) return 1;
y = s->a * y*y*y + s->b * y*y + s->c * y + s->d;
if(y <= s->x1) return s->x1;
if(y >= 1) return 1;
return y;
}
void SmoothClipCalc::setX(float x1, float x2) {
SmoothClipCalc* s = this;
if(x1 < 0) x1 = 0;
if(x1 > 1) x1 = 1;
if(x2 < x1) x2 = x1;
s->x1 = x1;
s->x2 = x2;
if(x1 == x2) {
s->a = 0;
s->b = 0;
s->c = 1;
s->d = 0;
return;
}
s->a = ((x1 + x2 - 2.) / pow(x2 - x1, 3.));
s->b = ((- (((x1 + x2 - 2.) * pow(x1, 2.)) / pow(x2 - x1, 3.)) - ((4. * x2 * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) + ((6. * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) - ((7. * pow(x2, 2.) * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) + ((6. * x2 * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) - 1.) / (4. * x2 - 4.));
s->c = (1. / 2.) * ((((x1 + x2 - 2.) * pow(x1, 2.)) / pow(x2 - x1, 3.)) + ((4. * x2 * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) - ((6. * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) + ((pow(x2, 2.) * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) - ((6. * x2 * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) - ((4. * (- (((x1 + x2 - 2.) * pow(x1, 2.)) / pow(x2 - x1, 3.)) - ((4. * x2 * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) + ((6. * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) - ((7. * pow(x2, 2.) * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) + ((6. * x2 * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) - 1.)) / (4. * x2 - 4.)) + 1.);
s->d = (1. / 4.) * ((((x1 + x2 - 2.) * pow(x1, 3.)) / pow(x2 - x1, 3.)) - ((4. * x2 * (x1 + x2 - 2.) * pow(x1, 2.)) / pow(x2 - x1, 3.)) - (((x1 + x2 - 2.) * pow(x1, 2.)) / pow(x2 - x1, 3.)) - ((pow(x2, 2.) * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) + ((2. * x2 * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) + ((6. * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) + x1 - ((pow(x2, 2.) * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) + ((6. * x2 * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) + ((4. * (- (((x1 + x2 - 2.) * pow(x1, 2.)) / pow(x2 - x1, 3.)) - ((4. * x2 * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) + ((6. * (x1 + x2 - 2.) * x1) / pow(x2 - x1, 3.)) - ((7. * pow(x2, 2.) * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) + ((6. * x2 * (x1 + x2 - 2.)) / pow(x2 - x1, 3.)) - 1.)) / (4. * x2 - 4.)) + 1.);
}
struct AudioParams {
int freq;
int channels;
int64_t channel_layout;
enum AVSampleFormat fmt;
};
struct InStreamRawPOD {
PlayerObject* player;
PyObject* song;
PyObject* metadata;
double playerTimePos;
double readerTimePos;
double timeLen;
float gainFactor;
AVFormatContext* ctx;
int audio_stream;
double audio_clock;
AVStream *audio_st;
DECLARE_ALIGNED(16,uint8_t,audio_buf2)[AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
uint8_t *audio_buf;
AVPacket audio_pkt_temp;
AVPacket audio_pkt;
bool do_flush;
struct AudioParams audio_tgt;
struct AudioParams audio_src;
struct SwrContext *swr_ctx;
// int audio_write_buf_size;
// double audio_current_pts;
// double audio_current_pts_drift;
AVFrame *frame;
};
struct PlayerObject::InStream : InStreamRawPOD {
PyMutex lock;
std::string debugName;
Buffer outBuffer;
bool readerHitEnd; // this will be set by audio_decode_frame()
bool playerStartedPlaying; // this would be set by readOutStream()
bool playerHitEnd; // this would be set by readOutStream()
InStream() {
memset(this, 0, sizeof(InStreamRawPOD));
timeLen = -1;
readerHitEnd = false;
playerStartedPlaying = playerHitEnd = false;
}
~InStream();
bool open(PlayerObject* player, PyObject* song);
void resetBuffers();
void seekToStart();
};
static int player_read_packet(PlayerObject::InStream* is, uint8_t* buf, int buf_size) {
// We assume that we don't have the PlayerObject lock at this point and not the Python GIL.
//printf("player_read_packet %i\n", buf_size);
if(is->player == NULL) return -1;
PyObject* song = NULL;
bool skipPyExceptions = false;;
{
PyScopedLock lock(is->player->lock);
PyScopedGIL gstate;
song = is->song;
if(song == NULL) return -1;
Py_INCREF(song);
skipPyExceptions = is->player->skipPyExceptions;
}
PyScopedGIL gstate;
Py_ssize_t ret = -1;
PyObject *readPacketFunc = NULL, *args = NULL, *retObj = NULL;
readPacketFunc = PyObject_GetAttrString(song, "readPacket");
if(readPacketFunc == NULL) goto final;
args = PyTuple_New(1);
PyTuple_SetItem(args, 0, PyInt_FromLong(buf_size));
retObj = PyObject_CallObject(readPacketFunc, args);
if(retObj == NULL) goto final;
if(!PyString_Check(retObj)) {
printf("song.readPacket didn't returned a string but a %s\n", retObj->ob_type->tp_name);
goto final;
}
ret = PyString_Size(retObj);
if(ret > buf_size) {
printf("song.readPacket returned more than buf_size\n");
ret = buf_size;
}
if(ret < 0) {
ret = -1;
goto final;
}
memcpy(buf, PyString_AsString(retObj), ret);
final:
Py_XDECREF(retObj);
Py_XDECREF(args);
Py_XDECREF(readPacketFunc);
Py_XDECREF(song);
if(skipPyExceptions && PyErr_Occurred())
PyErr_Print();
return (int) ret;
}
static int64_t player_seek(PlayerObject::InStream* is, int64_t offset, int whence) {
// We assume that we don't have the PlayerObject lock at this point and not the Python GIL.
//printf("player_seek %lli %i\n", offset, whence);
if(is->player == NULL) return -1;
PyObject* song = NULL;
bool skipPyExceptions = false;;
{
PyScopedLock lock(is->player->lock);
PyScopedGIL gstate;
song = is->song;
if(song == NULL) return -1;
Py_INCREF(song);
skipPyExceptions = is->player->skipPyExceptions;
}
PyScopedGIL gstate;
int64_t ret = -1;
PyObject *seekRawFunc = NULL, *args = NULL, *retObj = NULL;
if(whence < 0 || whence > 2) goto final; // AVSEEK_SIZE and others not supported atm
seekRawFunc = PyObject_GetAttrString(song, "seekRaw");
if(seekRawFunc == NULL) goto final;
args = PyTuple_New(2);
if(args == NULL) goto final;
PyTuple_SetItem(args, 0, PyLong_FromLongLong(offset));
PyTuple_SetItem(args, 1, PyInt_FromLong(whence));
retObj = PyObject_CallObject(seekRawFunc, args);
if(retObj == NULL) goto final; // pass through any Python exception
// NOTE: I don't really know what would be the best strategy in case of overflow...
if(PyInt_Check(retObj))
ret = (int) PyInt_AsLong(retObj);
else if(PyLong_Check(retObj))
ret = (int) PyLong_AsLong(retObj);
else {
printf("song.seekRaw didn't returned an int but a %s\n", retObj->ob_type->tp_name);
goto final;
}
final:
Py_XDECREF(retObj);
Py_XDECREF(args);
Py_XDECREF(seekRawFunc);
Py_XDECREF(song);
if(skipPyExceptions && PyErr_Occurred())
PyErr_Print();
return ret;
}
static int _player_av_read_packet(void *opaque, uint8_t *buf, int buf_size) {
return player_read_packet((PlayerObject::InStream*)opaque, buf, buf_size);
}
static int64_t _player_av_seek(void *opaque, int64_t offset, int whence) {
return player_seek((PlayerObject::InStream*)opaque, offset, whence);
}
static
AVIOContext* initIoCtx(PlayerObject::InStream* is) {
int buffer_size = 1024 * 4;
unsigned char* buffer = (unsigned char*)av_malloc(buffer_size);
AVIOContext* io = avio_alloc_context(
buffer,
buffer_size,
0, // writeflag
is, // opaque
_player_av_read_packet,
NULL, // write_packet
_player_av_seek
);
return io;
}
static
AVFormatContext* initFormatCtx(PlayerObject::InStream* is) {
AVFormatContext* fmt = avformat_alloc_context();
if(!fmt) return NULL;
fmt->pb = initIoCtx(is);
if(!fmt->pb) {
printf("initIoCtx failed\n");
}
fmt->flags |= AVFMT_FLAG_CUSTOM_IO;
return fmt;
}
static void player_resetStreamPackets(PlayerObject::InStream* player) {
av_free_packet(&player->audio_pkt);
memset(&player->audio_pkt, 0, sizeof(player->audio_pkt));
memset(&player->audio_pkt_temp, 0, sizeof(player->audio_pkt_temp));
}
void PlayerObject::InStream::resetBuffers() {
this->do_flush = true;
this->readerHitEnd = false;
this->outBuffer.clear();
player_resetStreamPackets(this);
}
void PlayerObject::InStream::seekToStart() {
if(!playerStartedPlaying && playerTimePos == 0) return;
resetBuffers();
playerStartedPlaying = false;
playerTimePos = readerTimePos = 0;
int ret = avformat_seek_file(
this->ctx, /*player->audio_stream*/ -1,
INT64_MIN,
0, // pos
INT64_MAX,
0 // flags
);
if(ret < 0)
printf("(%s) seekToStart: seek failed\n", debugName.c_str());
}
void PlayerObject::resetBuffers() {
std::list<boost::shared_ptr<PlayerObject::InStream> > instreams;
{
if(this->inStream.get())
instreams.push_back(this->inStream);
instreams.insert(instreams.end(), this->peekInStreams.begin(), this->peekInStreams.end());
}
PyScopedUnlock unlock(this->lock);
for(auto& it : instreams) {
PyScopedLock lock(it->lock);
it->resetBuffers();
}
instreams.clear(); // must be in unlocked scope
}
int PlayerObject::seekRel(double incr) {
PlayerObject* pl = this;
boost::shared_ptr<PlayerObject::InStream> is(pl->inStream);
if(!is.get()) return -1;
int ret = -1;
PyScopedUnlock unlock(pl->lock);
{
PyScopedLock lock(is->lock);
is->resetBuffers();
double pos = 0;
/*
int seek_by_bytes = 0;
if(seek_by_bytes) {
if (player->audio_stream >= 0 && player->audio_pkt.pos >= 0) {
pos = player->audio_pkt.pos;
} else
pos = avio_tell(player->ctx->pb);
if (player->ctx->bit_rate)
incr *= player->ctx->bit_rate / 8.0;
else
incr *= 180000.0;
pos += incr;
}
else*/ {
pos = is->playerTimePos;
pos += incr;
is->playerTimePos = is->readerTimePos = pos;
pos *= AV_TIME_BASE;
incr *= AV_TIME_BASE;
}
int64_t seek_target = pos;
int64_t seek_min = incr > 0 ? seek_target - incr + 2: INT64_MIN;
int64_t seek_max = incr < 0 ? seek_target - incr - 2: INT64_MAX;
int seek_flags = 0;
//if(seek_by_bytes) seek_flags |= AVSEEK_FLAG_BYTE;
ret =
avformat_seek_file(
is->ctx, /*player->audio_stream*/ -1,
seek_min,
seek_target,
seek_max,
seek_flags
);
}
is.reset(); // must be reset in unlocked scope
return ret;
}
int PlayerObject::seekAbs(double pos) {
PlayerObject* pl = this;
boost::shared_ptr<PlayerObject::InStream> is(pl->inStream);
if(!is.get()) return -1;
int ret = -1;
PyScopedUnlock unlock(pl->lock);
{
PyScopedLock lock(is->lock);
is->resetBuffers();
int seek_by_bytes = 0;
if(is->timeLen <= 0)
seek_by_bytes = 1;
is->playerTimePos = is->readerTimePos = pos;
int seek_flags = 0;
if(seek_by_bytes) seek_flags |= AVSEEK_FLAG_BYTE;
if(seek_by_bytes) {
if (is->ctx->bit_rate)
pos *= is->ctx->bit_rate / 8.0;
else
pos *= 180000.0;
}
else {
pos *= AV_TIME_BASE;
}
ret =
avformat_seek_file(
is->ctx, /*player->audio_stream*/ -1,
INT64_MIN,
(int64_t) pos,
INT64_MAX,
seek_flags
);
}
is.reset(); // must be reset in unlocked scope
return ret;
}
PyObject* PlayerObject::curSongMetadata() {
if(inStream.get()) return inStream->metadata;
return NULL;
}
double PlayerObject::curSongPos() {
if(inStream.get()) return inStream->playerTimePos;
return 0;
}
double PlayerObject::curSongLen() {
if(inStream.get()) return inStream->timeLen;
return -1;
}
float PlayerObject::curSongGainFactor() {
if(inStream.get()) return inStream->gainFactor;
return 1;
}
/* open a given stream. Return 0 if OK */
// called by player_openInputStream()
static int stream_component_open(PlayerObject::InStream *is, AVFormatContext* ic, int stream_index)
{
AVCodecContext *avctx;
AVCodec *codec;
// AVDictionaryEntry *t = NULL;
if (stream_index < 0 || stream_index >= ic->nb_streams)
return -1;
avctx = ic->streams[stream_index]->codec;
codec = avcodec_find_decoder(avctx->codec_id);
if (!codec) {
printf("(%s) avcodec_find_decoder failed (%s)\n", is->debugName.c_str(), ic->iformat->name);
return -1;
}
//avctx->workaround_bugs = workaround_bugs;
//avctx->lowres = lowres;
if(avctx->lowres > codec->max_lowres){
av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
codec->max_lowres);
avctx->lowres= codec->max_lowres;
}
//avctx->idct_algo = idct;
//avctx->skip_frame = skip_frame;
//avctx->skip_idct = skip_idct;
//avctx->skip_loop_filter = skip_loop_filter;
//avctx->error_concealment = error_concealment;
if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
//if (fast) avctx->flags2 |= CODEC_FLAG2_FAST;
if(codec->capabilities & CODEC_CAP_DR1)
avctx->flags |= CODEC_FLAG_EMU_EDGE;
if (avcodec_open2(avctx, codec, NULL /*opts*/) < 0) {
printf("(%s) avcodec_open2 failed (%s) (%s)\n", is->debugName.c_str(), ic->iformat->name, codec->name);
return -1;
}
/* prepare audio output */
//if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
// is->audio_tgt = is->audio_src;
//}
ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
switch (avctx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
is->audio_stream = stream_index;
is->audio_st = ic->streams[stream_index];
/* init averaging filter */
//is->audio_diff_avg_coef = exp(log(0.01) / AUDIO_DIFF_AVG_NB);
//is->audio_diff_avg_count = 0;
/* since we do not have a precise anough audio fifo fullness,
we correct audio sync only if larger than this threshold */
//is->audio_diff_threshold = 2.0 * is->audio_hw_buf_size / av_samples_get_buffer_size(NULL, is->audio_tgt.channels, is->audio_tgt.freq, is->audio_tgt.fmt, 1);
player_resetStreamPackets(is);
//packet_queue_start(&is->audioq);
//SDL_PauseAudio(0);
break;
default:
printf("(%s) stream_component_open: not an audio stream\n", is->debugName.c_str());
return -1;
}
return 0;
}
static void player_setSongMetadata(PlayerObject::InStream* player) {
Py_XDECREF(player->metadata);
player->metadata = NULL;
if(!player->ctx) return;
if(!player->ctx->metadata) return;
AVDictionary* m = player->ctx->metadata;
player->metadata = PyDict_New();
assert(player->metadata);
AVDictionaryEntry *tag=NULL;
while((tag=av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX))) {
if(strcmp("language", tag->key) == 0)
continue;
PyDict_SetItemString_retain(player->metadata, tag->key, PyString_FromString(tag->value));
}
if(player->timeLen > 0) {
PyDict_SetItemString_retain(player->metadata, "duration", PyFloat_FromDouble(player->timeLen));
}
else if(PyDict_GetItemString(player->metadata, "duration")) {
// we have an earlier duration metadata which is a string now.
// convert it to float.
PyObject* floatObj = PyFloat_FromString(PyDict_GetItemString(player->metadata, "duration"), NULL);
if(!floatObj) {
PyErr_Clear();
PyDict_DelItemString(player->metadata, "duration");
}
else {
PyDict_SetItemString_retain(player->metadata, "duration", floatObj);
}
}
}
static void closeInputStream(AVFormatContext* formatCtx) {
if(formatCtx->pb) {
if(formatCtx->pb->buffer) {
av_free(formatCtx->pb->buffer);
formatCtx->pb->buffer = NULL;
}
// avformat_close_input freeing this indirectly? I got a crash here in avio_close
//av_free(formatCtx->pb);
//formatCtx->pb = NULL;
}
for(int i = 0; i < formatCtx->nb_streams; ++i) {
avcodec_close(formatCtx->streams[i]->codec);
}
avformat_close_input(&formatCtx);
}
PlayerObject::InStream::~InStream() {
PlayerObject::InStream* is = this;
player_resetStreamPackets(is);
if(is->ctx) {
closeInputStream(is->ctx);
is->ctx = NULL;
}
if(is->frame) {
av_free(is->frame);
is->frame = NULL;
}
if(is->swr_ctx) {
swr_free(&is->swr_ctx);
is->swr_ctx = NULL;
}
{
PyScopedGIL gstate;
Py_XDECREF(song);
song = NULL;
Py_XDECREF(metadata);
metadata = NULL;
}
}
bool PlayerObject::InStream::open(PlayerObject* pl, PyObject* song) {
// We assume to not have the PlayerObject lock and neither the GIL.
assert(song != NULL);
if(this->player == NULL)
this->player = pl;
else {
assert(this->player == pl);
}
{
PyScopedLock lock(pl->lock);
while(pl->openStreamLock) {
PyScopedUnlock unlock(pl->lock);
usleep(100);
}
pl->openStreamLock = true;
}
{
PyScopedGIL glock;
Py_XDECREF(this->song); // if there is any old song
Py_INCREF(song);
}
this->song = song;
InStream* player = this;
int ret = 0;
AVFormatContext* formatCtx = NULL;
debugName = objAttrStr(song, "url"); // the url is just for debugging, the song object provides its own IO
{
size_t f = debugName.rfind('/');
if(f != std::string::npos)
debugName = debugName.substr(f + 1);
}
const char* fileExt = NULL;
{
size_t f = debugName.rfind('.');
if(f != std::string::npos)
fileExt = &debugName[f+1];
}
AVInputFormat* fmts[] = {
fileExt ? av_find_input_format(fileExt) : NULL,
NULL,
av_find_input_format("mp3")
};
for(size_t i = 0; i < sizeof(fmts)/sizeof(fmts[0]); ++i) {
if(i == 1 && fmts[0] == NULL) continue; // we already tried NULL
AVInputFormat* fmt = fmts[i];
if(formatCtx)
closeInputStream(formatCtx);
player_seek(this, 0, SEEK_SET);
formatCtx = initFormatCtx(this);
if(!formatCtx) {
printf("(%s) initFormatCtx failed\n", debugName.c_str());
goto final;
}
ret = av_probe_input_buffer(formatCtx->pb, &fmt, debugName.c_str(), NULL, 0, formatCtx->probesize);
if(ret < 0) {
printf("(%s) av_probe_input_buffer failed (%s)\n", debugName.c_str(), fmt ? fmt->name : "<NULL>");
continue;
}
ret = avformat_open_input(&formatCtx, debugName.c_str(), fmt, NULL);
if(ret != 0) {
printf("(%s) avformat_open_input (%s) failed\n", debugName.c_str(), fmt->name);
continue;
}
ret = avformat_find_stream_info(formatCtx, NULL);
if(ret < 0) {
printf("(%s) avformat_find_stream_info (%s) failed\n", debugName.c_str(), fmt->name);
continue;
}
#ifdef DEBUG
av_dump_format(formatCtx, 0, debugName.c_str(), 0);
#endif
if(formatCtx->nb_streams == 0) {
printf("(%s) no streams found in song (%s)\n", debugName.c_str(), fmt->name);
continue;
}
ret = av_find_best_stream(formatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, 0, 0);
if(ret < 0) {
const char* errorMsg = "unkown error";
if(ret == AVERROR_STREAM_NOT_FOUND) errorMsg = "stream not found";
else if(ret == AVERROR_DECODER_NOT_FOUND) errorMsg = "decoder not found";
printf("(%s) no audio stream found in song (%s): %s\n", debugName.c_str(), fmt->name, errorMsg);
int oldloglevel = av_log_get_level();
av_log_set_level(AV_LOG_INFO);
av_dump_format(formatCtx, 0, debugName.c_str(), 0);
av_log_set_level(oldloglevel);
continue;
}
player->audio_stream = ret;
ret = stream_component_open(player, formatCtx, player->audio_stream);
if(ret < 0) {
printf("(%s) cannot open audio stream (%s)\n", debugName.c_str(), fmt->name);
continue;
}
if(i > 0)
printf("(%s) fallback open succeeded (%s)\n", debugName.c_str(), fmt->name);
goto success;
}
printf("(%s) opening failed\n", debugName.c_str());
goto final;
success:
player->ctx = formatCtx;
formatCtx = NULL;
// Get the song len: There is formatCtx.duration in AV_TIME_BASE
// and there is stream.duration in stream time base.
assert(player->audio_st);
this->timeLen = av_q2d(player->audio_st->time_base) * player->audio_st->duration;
//if(player->timeLen < 0) { // happens in some cases, e.g. some flac files
// player->timeLen = av_q2d(AV_TIME_BASE_Q) * formatCtx->duration; // doesnt make it better though...
//}
if(this->timeLen < 0)
this->timeLen = -1;
{
PyScopedGIL glock;
player_setSongMetadata(this);
this->gainFactor = 1;
if(PyObject_HasAttrString(song, "gain")) {
PyObject* gainObj = PyObject_GetAttrString(song, "gain");
if(gainObj) {
float gain = 0;
if(!PyArg_Parse(gainObj, "f", &gain))
printf("(%s) song.gain is not a float\n", debugName.c_str());
else
this->gainFactor = pow(10, gain / 20);
Py_DECREF(gainObj);
}
else { // !gainObj
// strange. reset any errors...
if(PyErr_Occurred())
PyErr_Print();
}
}
// TODO: maybe alternatively try to read from metatags?
}
final:
if(formatCtx) closeInputStream(formatCtx);
{
PyScopedLock lock(pl->lock);
pl->openStreamLock = false;
}
if(this->ctx) return true;
return false;
}
bool PlayerObject::openInStream() {
assert(this->curSong != NULL);
PyScopedGIUnlock gunlock;
boost::shared_ptr<PlayerObject::InStream> is;
{
PyScopedUnlock unlock(this->lock);
is.reset(new PlayerObject::InStream());
if(!is->open(this, this->curSong)) {
PyScopedLock lock(this->lock);
if(!this->nextSongOnEof) {
PyScopedGIL gstate;
// This means that the failure of opening is fatal because we wont skip to the next song.
// This mode is also only used in the calc* specific functions.
if(!PyErr_Occurred())
PyErr_SetString(PyExc_RuntimeError, "failed to open file");
}
return false;
}
}
{
boost::shared_ptr<PlayerObject::InStream> inStreamOld(this->inStream);
this->inStream = is;
PyScopedUnlock unlock(this->lock);
inStreamOld.reset(); // inStream, if it gets freed, must be freed while the POL is not held!
}
return true;
}
/* return the wanted number of samples to get better sync if sync_type is video
* or external master clock */
static int synchronize_audio(PlayerObject::InStream *is, int nb_samples)
{
int wanted_nb_samples = nb_samples;
return wanted_nb_samples;
}
bool PlayerObject::volumeAdjustNeeded() const {
if(this->volume != 1) return true;
if(this->volumeSmoothClip.x1 != this->volumeSmoothClip.x2) return true;
if(inStream.get() && inStream->gainFactor != 1) return true;
return false;
}
static const enum AVSampleFormat outFormat = AV_SAMPLE_FMT_S16;
// called from PlayerObject::workerProc()
// decode one audio frame and returns its uncompressed size
// return <0 means that we must change some state for this function to work again. e.g. we could have EOF, the song is not correctly opened, the player is in stopped-state or so. an invalid frame will not cause this!
// note that even with <0, there might have been some data written to outBuffer.
// tries to return at least len bytes. but might return more. if something fails, also less.
static long audio_decode_frame(PlayerObject* player, PlayerObject::InStream *is, long len)
{
// We assume that we don't have the PlayerObject lock at this point and neither the Python GIL.
if(is->ctx == NULL) return -1;
if(is->audio_st == NULL) return -1;
if(is->readerHitEnd) return -1;
PyScopedGIUnlock gunlock; // be sure that we don't have it. the av-callbacks (read/seek) must not have it.
AVPacket *pkt_temp = &is->audio_pkt_temp;
AVPacket *pkt = &is->audio_pkt;
AVCodecContext *dec = is->audio_st->codec;
int len2, data_size, resampled_data_size;
int64_t dec_channel_layout;
int flush_complete = 0;
int wanted_nb_samples;
long count = 0;
for (;;) {
int outSamplerate = 0, outNumChannels = 0;
{
PyScopedLock lock(player->lock);
if(is->do_flush) {
avcodec_flush_buffers(dec);
flush_complete = 0;
is->do_flush = false;
count = 0;
}
outSamplerate = player->outSamplerate;
outNumChannels = player->outNumChannels;
}
/* NOTE: the audio packet can contain several frames */
while (pkt_temp->size > 0) {
if (!is->frame) {
if (!(is->frame = avcodec_alloc_frame()))
return AVERROR(ENOMEM);
} else
avcodec_get_frame_defaults(is->frame);
if (flush_complete)
break;
int got_frame = 0;
int len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
if (len1 < 0) {
pkt_temp->size = 0;
// warning only at pos 0. this seems too common and i don't like a spammy log...
if(is->readerTimePos == 0)
printf("(%s) avcodec_decode_audio4 error at pos 0\n", is->debugName.c_str());
// earlier, we just breaked. but I encountered some audio files
// which only have garbage following and the user should never
// listen to that.
// other musicplayers also skip that part.
is->readerHitEnd = true;
return count;
break;
}
//printf("avcodec_decode_audio4: %i\n", len1);
pkt_temp->data += len1;
pkt_temp->size -= len1;
if (!got_frame) {
/* stop sending empty packets if the decoder is finished */
if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
flush_complete = 1;
continue;
}
data_size = av_samples_get_buffer_size(NULL, dec->channels,
is->frame->nb_samples,
dec->sample_fmt, 1);
dec_channel_layout =
(dec->channel_layout && dec->channels == av_get_channel_layout_nb_channels(dec->channel_layout)) ?
dec->channel_layout : av_get_default_channel_layout(dec->channels);
wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
if (dec->sample_fmt != is->audio_src.fmt ||
dec_channel_layout != is->audio_src.channel_layout ||
dec->sample_rate != is->audio_src.freq ||
(wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx) ||
is->audio_tgt.freq != outSamplerate ||
is->audio_tgt.channels != outNumChannels) {
swr_free(&is->swr_ctx);
is->audio_tgt.fmt = outFormat;
is->audio_tgt.freq = outSamplerate;
is->audio_tgt.channels = outNumChannels;
is->audio_tgt.channel_layout = av_get_default_channel_layout(player->outNumChannels);
is->swr_ctx = swr_alloc_set_opts
(
NULL,
is->audio_tgt.channel_layout, outFormat, outSamplerate,
dec_channel_layout, dec->sample_fmt, dec->sample_rate,
0, NULL);
if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), dec->channels,
outSamplerate, av_get_sample_fmt_name(outFormat), outNumChannels);
break;
}
is->audio_src.channel_layout = dec_channel_layout;
is->audio_src.channels = dec->channels;
is->audio_src.freq = dec->sample_rate;
is->audio_src.fmt = dec->sample_fmt;
/*printf("conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), dec->channels,
is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);*/
}
if (is->swr_ctx) {
const uint8_t **in = (const uint8_t **)is->frame->extended_data;
uint8_t *out[] = {is->audio_buf2};
int out_count = sizeof(is->audio_buf2) / outNumChannels / av_get_bytes_per_sample(outFormat);
if (wanted_nb_samples != is->frame->nb_samples) {
if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * outSamplerate / dec->sample_rate,
wanted_nb_samples * outSamplerate / dec->sample_rate) < 0) {
fprintf(stderr, "swr_set_compensation() failed\n");
break;
}
}
len2 = swr_convert(is->swr_ctx, out, out_count, in, is->frame->nb_samples);
if (len2 < 0) {
fprintf(stderr, "swr_convert() failed\n");
break;
}
if (len2 == out_count) {
fprintf(stderr, "warning: audio buffer is probably too small\n");
swr_init(is->swr_ctx);
}
is->audio_buf = is->audio_buf2;
resampled_data_size = len2 * outNumChannels * av_get_bytes_per_sample(outFormat);
} else {
is->audio_buf = is->frame->data[0];
resampled_data_size = data_size;
}
{
PyScopedLock lock(player->lock);
is->outBuffer.push(is->audio_buf, resampled_data_size);
}
/* if no pts, then compute it */
is->readerTimePos += (double)data_size /
(dec->channels * dec->sample_rate * av_get_bytes_per_sample(dec->sample_fmt));
/*{
static double last_clock;
printf("audio: delay=%0.3f clock=%0.3f pts=%0.3f\n",
is->audio_clock - last_clock,
is->audio_clock, pts);
last_clock = is->audio_clock;
}*/
count += resampled_data_size;
if(count >= len)
return count;
}
/* free the current packet */
av_free_packet(pkt);
memset(pkt_temp, 0, sizeof(*pkt_temp));
while(1) {
int ret = av_read_frame(is->ctx, pkt);
if (ret < 0) {
if(is->readerTimePos == 0)
printf("(%s) av_read_frame error at pos 0\n", is->debugName.c_str());
//if (ic->pb && ic->pb->error)