forked from sobotka/ffmpeg-php-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg_movie.c
1515 lines (1210 loc) · 40.3 KB
/
ffmpeg_movie.c
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
/*
This file is part of ffmpeg-php
Copyright (C) 2004-2008 Todd Kirby (ffmpeg.php AT gmail.com)
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.
In addition, as a special exception, the copyright holders of ffmpeg-php
give you permission to combine ffmpeg-php with code included in the
standard release of PHP under the PHP license (or modified versions of
such code, with unchanged license). You may copy and distribute such a
system following the terms of the GNU GPL for ffmpeg-php and the licenses
of the other code concerned, provided that you include the source code of
that other code when and as the GNU GPL requires distribution of source code.
You must obey the GNU General Public License in all respects for all of the
code used other than standard release of PHP. If you modify this file, you
may extend this exception to your version of the file, but you are not
obligated to do so. If you do not wish to do so, delete this exception
statement from your version.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_globals.h"
#include "ext/standard/info.h"
#include <libavcodec/avcodec.h>
#include <libavcodec/version.h>
#include <libavformat/avformat.h>
#include <libavutil/pixfmt.h>
#include <libavutil/pixdesc.h>
#include "php_ffmpeg.h"
#include "ffmpeg_frame.h"
#include "ffmpeg_movie.h"
#include "ffmpeg_tools.h"
#define GET_MOVIE_RESOURCE(ff_movie_ctx) {\
zval **_tmp_zval;\
if (zend_hash_find(Z_OBJPROP_P(getThis()), "ffmpeg_movie",\
sizeof("ffmpeg_movie"), (void **)&_tmp_zval) == FAILURE) {\
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid ffmpeg_movie object");\
RETURN_FALSE;\
}\
\
ZEND_FETCH_RESOURCE2(ff_movie_ctx, ff_movie_context*, _tmp_zval, -1,\
"ffmpeg_movie", le_ffmpeg_movie, le_ffmpeg_pmovie);\
}\
#define LRINT(x) ((long) ((x)+0.5))
#if LIBAVFORMAT_BUILD > 4628
#define GET_CODEC_FIELD(codec, field) codec->field
#define GET_CODEC_PTR(codec) codec
#else
#define GET_CODEC_FIELD(codec, field) codec.field
#define GET_CODEC_PTR(codec) &codec
#endif
//hacky fix for now, lets change this to dynamically use nb_streams
#define MAX_STREAMS 16
typedef struct {
AVFormatContext *fmt_ctx;
AVCodecContext *codec_ctx[MAX_STREAMS];
int64_t last_pts;
int frame_number;
long rsrc_id;
AVPacket packet;
int flushing;
uint64_t global_video_pkt_pts;
} ff_movie_context;
static zend_class_entry *ffmpeg_movie_class_entry_ptr;
zend_class_entry ffmpeg_movie_class_entry;
static int le_ffmpeg_movie;
static int le_ffmpeg_pmovie;
//static uint64_t global_video_pkt_pts = AV_NOPTS_VALUE;
/* {{{ ffmpeg_movie methods[]
Methods of the ffmpeg_movie class
*/
zend_function_entry ffmpeg_movie_class_methods[] = {
/* contructor */
FFMPEG_PHP_ME(ffmpeg_movie, __construct, NULL, 0)
/* methods */
FFMPEG_PHP_MALIAS(ffmpeg_movie, getduration, getDuration, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getframecount, getFrameCount, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getframerate, getFrameRate, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getfilename, getFileName, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getcomment, getComment, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, gettitle, getTitle, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getauthor, getAuthor, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getartist, getArtist, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getcopyright, getCopyright, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getalbum, getAlbum, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getgenre, getGenre, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getyear, getYear, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, gettracknumber, getTrackNumber, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getframewidth, getFrameWidth, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getframeheight, getFrameHeight, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getframenumber, getFrameNumber, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getpixelformat, getPixelFormat, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getbitrate, getBitRate, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, hasaudio, hasAudio, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, hasvideo, hasVideo, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getnextkeyframe, getNextKeyFrame, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getframe, getFrame, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getvideocodec, getVideoCodec, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getaudiocodec, getAudioCodec, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getvideostreamid, getVideoStreamId, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getaudiostreamid, getAudioStreamId, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getaudiochannels, getAudioChannels, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getaudiosamplerate, getAudioSampleRate, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getaudiobitrate, getAudioBitRate, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getvideobitrate, getVideoBitRate, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_movie, getpixelaspectratio, getPixelAspectRatio, NULL, 0)
FFMPEG_PHP_END_METHODS
};
/* }}} */
static int our_get_buffer(struct AVCodecContext *c, AVFrame *pic) {
int ret = avcodec_default_get_buffer(c, pic);
uint64_t *pts = av_malloc(sizeof(uint64_t));
//*pts = global_video_pkt_pts;
if(c->opaque)
{
*pts = *(uint64_t *) c->opaque;
pic->opaque = pts;
}
return ret;
}
static void our_release_buffer(struct AVCodecContext *c, AVFrame *pic) {
if(pic) av_freep(&pic->opaque);
avcodec_default_release_buffer(c, pic);
}
/* {{{ _php_get_stream_index()
*/
static int _php_get_stream_index(AVFormatContext *fmt_ctx, int type)
{
int i;
for (i = 0; i < ((int) (fmt_ctx->nb_streams)); i++) {
if (fmt_ctx->streams[i] &&
GET_CODEC_FIELD(fmt_ctx->streams[i]->codec, codec_type) == type) {
return i;
}
}
/* stream not found */
return -1;
}
/* }}} */
/* {{{ _php_get_video_stream()
*/
static AVStream *_php_get_video_stream(AVFormatContext *fmt_ctx)
{
int i = _php_get_stream_index(fmt_ctx, AVMEDIA_TYPE_VIDEO);
return i < 0 ? NULL : fmt_ctx->streams[i];
}
/* }}} */
/* {{{ _php_get_audio_stream()
* TODO: Some containers can have multiple audio streams, so this
* will eventually need to be replaced by something smarter
*/
static AVStream *_php_get_audio_stream(AVFormatContext *fmt_ctx)
{
int i = _php_get_stream_index(fmt_ctx, AVMEDIA_TYPE_AUDIO);
return i < 0 ? NULL : fmt_ctx->streams[i];
}
/* }}} */
static int has_audio(ff_movie_context *ffmovie_ctx) {
return _php_get_audio_stream(ffmovie_ctx->fmt_ctx) != NULL;
}
static int has_video(ff_movie_context *ffmovie_ctx) {
return _php_get_video_stream(ffmovie_ctx->fmt_ctx) != NULL;
}
/* {{{ _php_get_filename()
*/
static char* _php_get_filename(ff_movie_context *ffmovie_ctx)
{
return ffmovie_ctx->fmt_ctx->filename;
}
/* }}} */
/* {{{ _php_alloc_ffmovie_ctx()
*/
static ff_movie_context* _php_alloc_ffmovie_ctx(int persistent)
{
int i;
ff_movie_context *ffmovie_ctx;
ffmovie_ctx = persistent ? malloc(sizeof(ff_movie_context)) :
emalloc(sizeof(ff_movie_context));
ffmovie_ctx->fmt_ctx = NULL;
ffmovie_ctx->frame_number = 0;
for (i = 0; i < MAX_STREAMS; i++) {
ffmovie_ctx->codec_ctx[i] = NULL;
}
ffmovie_ctx->flushing = 0;
ffmovie_ctx->global_video_pkt_pts = AV_NOPTS_VALUE;
return ffmovie_ctx;
}
/* }}} */
/* {{{ _php_print_av_error()
*/
/*
static void _php_print_av_error(const char *filename, int err)
{
switch(err) {
case AVERROR_IO:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: I/O error.\n", filename);
break;
case AVERROR_NOMEM:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: Not enough memory.\n", filename);
break;
case AVERROR_NOTSUPP:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: Operation not supported.\n", filename);
break;
case AVERROR_NUMEXPECTED:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: Incorrect image filename syntax.\n", filename);
break;
case AVERROR_INVALIDDATA:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: Error while parsing header\n", filename);
break;
case AVERROR_NOFMT:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: Unknown format\n", filename);
case AVERROR_UNKNOWN:
// Fall thru to default case
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s: Error while opening file (%d)\n", filename, err);
break;
}
}
*/
/* }}} */
/* {{{ _php_open_movie_file()
*/
static int _php_open_movie_file(ff_movie_context *ffmovie_ctx,
char* filename)
{
AVStream *st;
if (ffmovie_ctx->fmt_ctx) {
avformat_close_input(&ffmovie_ctx->fmt_ctx);
ffmovie_ctx->fmt_ctx = NULL;
}
/* open the file with generic libav function */
if (avformat_open_input(&ffmovie_ctx->fmt_ctx, filename, NULL, NULL) < 0) {
return 1;
}
st = _php_get_video_stream(ffmovie_ctx->fmt_ctx);
st->codec->get_buffer = our_get_buffer;
st->codec->release_buffer = our_release_buffer;
/* decode the first frames to get the stream parameters. */
avformat_find_stream_info(ffmovie_ctx->fmt_ctx, NULL);
return 0;
}
/* }}} */
/* {{{ proto object ffmpeg_movie(string filename)
Constructor for ffmpeg_movie objects
*/
FFMPEG_PHP_CONSTRUCTOR(ffmpeg_movie, __construct)
{
int hashkey_length = 0, filename_len;
char *filename = NULL, *fullpath = NULL, *hashkey = NULL;
zend_bool persistent = 0;
ff_movie_context *ffmovie_ctx = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &filename_len, &persistent) != SUCCESS) {
return;
}
if (persistent && !INI_BOOL("ffmpeg.allow_persistent")) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Persistent movies have been disabled in php.ini");
RETURN_FALSE;
}
if (persistent) {
zend_rsrc_list_entry *le;
/* resolve the fully-qualified path name to use as the hash key */
fullpath = expand_filepath(filename, NULL TSRMLS_CC);
hashkey_length = sizeof("ffmpeg-php_")-1 + filename_len;
hashkey = (char *) emalloc(hashkey_length+1);
snprintf(hashkey, hashkey_length, "ffmpeg-php_%s", filename);
/* do we have an existing persistent movie? */
if (SUCCESS == zend_hash_find(&EG(persistent_list), hashkey, hashkey_length+1, (void**)&le)) {
int type;
if (Z_TYPE_P(le) != le_ffmpeg_pmovie) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to retrieve persistent resource");
efree(hashkey);
RETURN_FALSE;
}
ffmovie_ctx = (ff_movie_context*)le->ptr;
/* sanity check to ensure that the resource is still a valid
* regular resource number */
if (zend_list_find(ffmovie_ctx->rsrc_id, &type) == ffmovie_ctx) {
/* add a reference to the persistent movie */
zend_list_addref(ffmovie_ctx->rsrc_id);
} else {
//php_error_docref(NULL TSRMLS_CC, E_ERROR,
//"Not a valid persistent movie resource");
ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL,
ffmovie_ctx, le_ffmpeg_pmovie);
}
} else { /* no existing persistant movie, create one */
zend_rsrc_list_entry new_le;
ffmovie_ctx = _php_alloc_ffmovie_ctx(1);
if (_php_open_movie_file(ffmovie_ctx, filename)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't open movie file %s", filename);
ZVAL_BOOL(getThis(), 0);
RETURN_FALSE;
}
Z_TYPE(new_le) = le_ffmpeg_pmovie;
new_le.ptr = ffmovie_ctx;
if (FAILURE == zend_hash_update(&EG(persistent_list), hashkey,
hashkey_length+1, (void *)&new_le, sizeof(zend_rsrc_list_entry),
NULL)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to register persistent resource");
RETURN_FALSE;
}
ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ffmovie_ctx, le_ffmpeg_pmovie);
}
} else {
ffmovie_ctx = _php_alloc_ffmovie_ctx(0);
if (_php_open_movie_file(ffmovie_ctx, filename)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't open movie file %s", filename);
ZVAL_BOOL(getThis(), 0);
RETURN_FALSE;
}
/* pass NULL for resource result since we're not returning the resource
directly, but adding it to the returned object. */
ffmovie_ctx->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ffmovie_ctx, le_ffmpeg_movie);
}
object_init_ex(getThis(), ffmpeg_movie_class_entry_ptr);
add_property_resource(getThis(), "ffmpeg_movie", ffmovie_ctx->rsrc_id);
if (fullpath) {
efree(fullpath);
}
if (hashkey) {
efree(hashkey);
}
}
/* }}} */
/* {{{ _php_free_ffmpeg_movie
*/
static void _php_free_ffmpeg_movie(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
int i;
ff_movie_context *ffmovie_ctx = (ff_movie_context*)rsrc->ptr;
if (ffmovie_ctx->codec_ctx) {
for (i = 0; i < MAX_STREAMS; i++) {
if (ffmovie_ctx->codec_ctx[i]) {
avcodec_close(ffmovie_ctx->codec_ctx[i]);
}
ffmovie_ctx->codec_ctx[i] = NULL;
}
}
avformat_close_input(&ffmovie_ctx->fmt_ctx);
efree(ffmovie_ctx);
}
/* }}} */
/* {{{ _php_free_ffmpeg_pmovie
*/
static void _php_free_ffmpeg_pmovie(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
/* TODO: Factor into a single free function for pmovie and movie */
int i;
ff_movie_context *ffmovie_ctx = (ff_movie_context*)rsrc->ptr;
if (ffmovie_ctx->codec_ctx) {
for (i = 0; i < MAX_STREAMS; i++) {
if (ffmovie_ctx->codec_ctx[i]) {
avcodec_close(ffmovie_ctx->codec_ctx[i]);
}
ffmovie_ctx->codec_ctx[i] = NULL;
}
}
avformat_close_input(&ffmovie_ctx->fmt_ctx);
free(ffmovie_ctx);
}
/* }}} */
/* {{{ register_ffmpeg_movie_class()
*/
void register_ffmpeg_movie_class(int module_number)
{
TSRMLS_FETCH();
le_ffmpeg_movie = zend_register_list_destructors_ex(_php_free_ffmpeg_movie,
NULL, "ffmpeg_movie", module_number);
le_ffmpeg_pmovie = zend_register_list_destructors_ex(NULL,
_php_free_ffmpeg_pmovie, "ffmpeg_pmovie", module_number);
INIT_CLASS_ENTRY(ffmpeg_movie_class_entry, "ffmpeg_movie",
ffmpeg_movie_class_methods);
/* register ffmpeg movie class */
ffmpeg_movie_class_entry_ptr =
zend_register_internal_class(&ffmpeg_movie_class_entry TSRMLS_CC);
}
/* }}} */
/* {{{ __php_get_decoder_context()
Opens decoders and gets codec context. Always call this to get a pointer to
the codec context. This allows to postpone codec init until a function that
requires it is called.
*/
static AVCodecContext* _php_get_decoder_context(ff_movie_context *ffmovie_ctx,
int stream_type)
{
AVCodec *decoder;
int stream_index;
TSRMLS_FETCH();
stream_index = _php_get_stream_index(ffmovie_ctx->fmt_ctx, stream_type);
if (stream_index < 0) {
// FIXME: factor out the conditional.
if (stream_type == AVMEDIA_TYPE_VIDEO) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't find video stream in %s",
_php_get_filename(ffmovie_ctx));
return NULL;
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't find audio stream in %s",
_php_get_filename(ffmovie_ctx));
return NULL;
}
}
/* check if the codec for this stream is already open */
if (!ffmovie_ctx->codec_ctx[stream_index]) {
/* find the decoder */
decoder = avcodec_find_decoder(GET_CODEC_FIELD(
ffmovie_ctx->fmt_ctx->streams[stream_index]->codec,
codec_id));
if (!decoder) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find decoder for %s",
_php_get_filename(ffmovie_ctx));
return NULL;
}
ffmovie_ctx->codec_ctx[stream_index] =
GET_CODEC_PTR(ffmovie_ctx->fmt_ctx->streams[stream_index]->codec);
/* open the decoder */
if (avcodec_open2(ffmovie_ctx->codec_ctx[stream_index], decoder, NULL) < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not open codec for %s", _php_get_filename(ffmovie_ctx));
return NULL;
}
}
return ffmovie_ctx->codec_ctx[stream_index];
}
/* }}} */
void php_get_dict_value(INTERNAL_FUNCTION_PARAMETERS, char *entry_name) /* {{{ */
{
ff_movie_context *ffmovie_ctx;
AVDictionaryEntry* m_entry;
GET_MOVIE_RESOURCE(ffmovie_ctx);
m_entry = av_dict_get(ffmovie_ctx->fmt_ctx->metadata, entry_name, NULL, 0);
if (!m_entry) {
RETURN_FALSE;
}
RETURN_STRINGL(m_entry->value, strlen(m_entry->value), 1);
}
/* }}} */
/* {{{ proto string getComment()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getComment)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "comment");
}
/* }}} */
/* {{{ proto string getTitle()
* Return title field from movie or title ID3 tag from an MP3 file.
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getTitle)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "title");
}
/* }}} */
/* {{{ proto string getAuthor()
* Return author field from a movie.
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getAuthor)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "author");
}
/* }}} */
/* {{{ proto string getArtist()
* Return artist field from MP3 files.
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getArtist)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "artist");
}
/* }}} */
/* {{{ proto string getCopyright()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getCopyright)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "copyright");
}
/* }}} */
/* {{{ proto string getAlbum()
* Return ID3 album field from an mp3 file
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getAlbum)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "album");
}
/* }}} */
/* {{{ proto string getGenre()
* Return ID3 genre field from an mp3 file
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getGenre)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "genre");
}
/* }}} */
/* {{{ proto int getTrackNumber()
* Return ID3 track field from an mp3 file
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getTrackNumber)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "track");
}
/* }}} */
/* {{{ proto int getYear()
* Return ID3 year field from an mp3 file
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getYear)
{
php_get_dict_value(INTERNAL_FUNCTION_PARAM_PASSTHRU, "year");
}
/* }}} */
/* {{{ _php_get_duration()
*/
static float _php_get_duration(ff_movie_context *ffmovie_ctx)
{
float duration;
duration = (float)ffmovie_ctx->fmt_ctx->duration / AV_TIME_BASE;
if (duration < 0) {
duration = 0.0f;
}
return duration;
}
/* }}} */
/* {{{ proto int getDuration()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getDuration)
{
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
RETURN_DOUBLE(_php_get_duration(ffmovie_ctx));
}
/* }}} */
/* {{{ _php_get_framerate()
*/
static float _php_get_framerate(ff_movie_context *ffmovie_ctx)
{
AVStream *st = _php_get_video_stream(ffmovie_ctx->fmt_ctx);
double rate = 0.0;
if (!st) {
return ((float) rate);
}
return (float) av_q2d(st->avg_frame_rate);
//#if LIBAVCODEC_BUILD > 4753
// if (GET_CODEC_FIELD(st->codec, codec_type) == AVMEDIA_TYPE_VIDEO){
// if (st->r_frame_rate.den && st->r_frame_rate.num) {
// rate = av_q2d(st->r_frame_rate);
// } else {
// rate = 1 / av_q2d(GET_CODEC_FIELD(st->codec, time_base));
// }
// }
// return (float)rate;
//#else
// return (float)GET_CODEC_FIELD(st->codec, frame_rate) /
// GET_CODEC_FIELD(st->codec, frame_rate_base);
//#endif
}
/* }}} */
/* {{{ _php_get_framecount()
*/
static long _php_get_framecount(ff_movie_context *ffmovie_ctx)
{
AVStream *st = _php_get_video_stream(ffmovie_ctx->fmt_ctx);
/* does this movie have a video stream? */
if (!st) {
return 0;
}
return LRINT(st->nb_frames);
// return LRINT(_php_get_framerate(ffmovie_ctx) *
// _php_get_duration(ffmovie_ctx));
}
/* }}} */
/* {{{ proto int getFrameCount()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getFrameCount)
{
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
RETURN_LONG(_php_get_framecount(ffmovie_ctx));
}
/* }}} */
/* {{{ proto int getFrameRate()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getFrameRate)
{
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
RETURN_DOUBLE(_php_get_framerate(ffmovie_ctx));
}
/* }}} */
/* {{{ proto string getFileName()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getFileName)
{
ff_movie_context *ffmovie_ctx;
char* filename;
GET_MOVIE_RESOURCE(ffmovie_ctx);
filename = _php_get_filename(ffmovie_ctx);
RETURN_STRINGL(filename, strlen(filename), 1);
}
/* }}} */
/* {{{ _php_get_framewidth()
*/
static int _php_get_framewidth(ff_movie_context *ffmovie_ctx)
{
AVStream *st = _php_get_video_stream(ffmovie_ctx->fmt_ctx);
if (!st) {
return 0;
}
return GET_CODEC_FIELD(st->codec, width);
}
/* }}} */
/* {{{ proto int getFrameWidth()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getFrameWidth)
{
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
RETURN_LONG(_php_get_framewidth(ffmovie_ctx));
}
/* }}} */
/* {{{ _php_get_frameheight()
*/
static int _php_get_frameheight(ff_movie_context *ffmovie_ctx)
{
AVStream *st = _php_get_video_stream(ffmovie_ctx->fmt_ctx);
if (!st) {
return 0;
}
return GET_CODEC_FIELD(st->codec, height);
}
/* }}} */
/* {{{ proto int getFrameHeight()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getFrameHeight)
{
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
RETURN_LONG(_php_get_frameheight(ffmovie_ctx));
}
/* }}} */
/* {{{ _php_get_framenumber()
*/
static long _php_get_framenumber(ff_movie_context *ffmovie_ctx)
{
AVCodecContext *decoder_ctx = NULL;
decoder_ctx = _php_get_decoder_context(ffmovie_ctx, AVMEDIA_TYPE_VIDEO);
if (!decoder_ctx) {
return 0;
}
if (ffmovie_ctx->frame_number <= 0) {
return 1; /* no frames read yet so return the first */
} else {
return ffmovie_ctx->frame_number;
}
}
/* }}} */
/* {{{ proto resource getFrameNumber()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getFrameNumber)
{
ff_movie_context *ffmovie_ctx;
int frame_number = 0;
GET_MOVIE_RESOURCE(ffmovie_ctx);
frame_number =_php_get_framenumber(ffmovie_ctx);
if (frame_number) {
RETURN_LONG(frame_number);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ _php_get_pixelformat()
*/
static int _php_get_pixelformat(ff_movie_context *ffmovie_ctx)
{
AVCodecContext *decoder_ctx;
decoder_ctx = _php_get_decoder_context(ffmovie_ctx, AVMEDIA_TYPE_VIDEO);
return decoder_ctx ? decoder_ctx->pix_fmt : 0;
}
/* }}} */
/* {{{ proto int getPixelFormat()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getPixelFormat)
{
int pix_fmt;
const char *fmt;
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
pix_fmt = _php_get_pixelformat(ffmovie_ctx);
fmt = av_get_pix_fmt_name(pix_fmt);
if (fmt) {
/* cast const to non-const to keep compiler from complaining,
RETURN_STRINGL just copies so the string won't get overwritten
*/
RETURN_STRINGL((char *)fmt, strlen(fmt), 1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ _php_get_bitrate()
*/
static int _php_get_bitrate(ff_movie_context *ffmovie_ctx)
{
return ffmovie_ctx->fmt_ctx->bit_rate;
}
/* }}} */
/* {{{ proto int getBitrate()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getBitRate)
{
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
RETURN_LONG(_php_get_bitrate(ffmovie_ctx));
}
/* }}} */
/* {{{ proto int hasAudio()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, hasAudio)
{
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
RETURN_BOOL(has_audio(ffmovie_ctx));
}
/* }}} */
/* {{{ proto int hasVideo()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, hasVideo)
{
ff_movie_context *ffmovie_ctx;
GET_MOVIE_RESOURCE(ffmovie_ctx);
RETURN_BOOL(has_video(ffmovie_ctx));
}
/* }}} */
/* {{{ _php_get_codec_name()
Returns the name of a video or audio codec from a movie
*/
static const char* _php_get_codec_name(ff_movie_context *ffmovie_ctx, int type)
{
AVCodecContext *decoder_ctx = NULL;
AVCodec *p = NULL;
const char *codec_name;
char buf1[32];
decoder_ctx = _php_get_decoder_context(ffmovie_ctx, type);
if (!decoder_ctx) {
return NULL;
}
p = avcodec_find_decoder(decoder_ctx->codec_id);
/* Copied from libavcodec/utils.c::avcodec_string */
if (p) {
codec_name = p->name;
#ifdef FF_API_SUB_ID
if (decoder_ctx->codec_id == CODEC_ID_MP3) {
if (decoder_ctx->sub_id == 2)
codec_name = "mp2";
else if (decoder_ctx->sub_id == 1)
codec_name = "mp1";
}
#endif
} else if (decoder_ctx->codec_id == CODEC_ID_MPEG2TS) {
/* fake mpeg2 transport stream codec (currently not registered) */
codec_name = "mpeg2ts";
} else if (decoder_ctx->codec_name[0] != '\0') {
codec_name = decoder_ctx->codec_name;
} else {
/* output avi tags */
if (decoder_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
snprintf(buf1, sizeof(buf1), "%c%c%c%c",
decoder_ctx->codec_tag & 0xff,
(decoder_ctx->codec_tag >> 8) & 0xff,
(decoder_ctx->codec_tag >> 16) & 0xff,
(decoder_ctx->codec_tag >> 24) & 0xff);
} else {
snprintf(buf1, sizeof(buf1), "0x%04x", decoder_ctx->codec_tag);
}
codec_name = buf1;
}
return codec_name;
}
/* }}} */
/* {{{ proto int getVideoCodec()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getVideoCodec)
{
ff_movie_context *ffmovie_ctx;
char *codec_name;
GET_MOVIE_RESOURCE(ffmovie_ctx);
codec_name = (char*)_php_get_codec_name(ffmovie_ctx, AVMEDIA_TYPE_VIDEO);
if (codec_name) {
RETURN_STRINGL(codec_name, strlen(codec_name), 1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int getAudioCodec()
*/
FFMPEG_PHP_METHOD(ffmpeg_movie, getAudioCodec)
{
ff_movie_context *ffmovie_ctx;
char *codec_name;