forked from tony2001/ffmpeg-php
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ffmpeg_frame.c
660 lines (510 loc) · 17.6 KB
/
ffmpeg_frame.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
/*
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_ffmpeg.h"
#include "ffmpeg_frame.h"
#include "ffmpeg_tools.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/*
include gd header from local include dir. This is a copy of gd.h that is
distributed with php-5.2.5. It is distributed along with ffmpeg-php to
allow ffmpeg-php to be built without access to the php sources
*/
#if HAVE_LIBGD20
#include "gd.h"
#define FFMPEG_PHP_FETCH_IMAGE_RESOURCE(gd_img, ret) { \
ZEND_GET_RESOURCE_TYPE_ID(le_gd, "gd"); \
ZEND_FETCH_RESOURCE(gd_img, gdImagePtr, ret, -1, "Image", le_gd); \
}
#if PIX_FMT_RGBA32
#define FFMPEG_PHP_FFMPEG_RGB_PIX_FORMAT PIX_FMT_RGBA32
#else
#define FFMPEG_PHP_FFMPEG_RGB_PIX_FORMAT PIX_FMT_RGB32
#endif
#define gdImageBoundsSafeMacro(im, x, y) (!((((y) < (im)->cy1) || ((y) > (im)->cy2)) || (((x) < (im)->cx1) || ((x) > (im)->cx2))))
static int le_gd; // this is only valid after calling
// FFMPEG_PHP_FETCH_IMAGE_RESOURCE()
#endif // HAVE_LIBGD20
int le_ffmpeg_frame; // not static since it is used in ffmpeg_output_movie
static zend_class_entry *ffmpeg_frame_class_entry_ptr;
zend_class_entry ffmpeg_frame_class_entry;
/* {{{ ffmpeg_frame methods[]
Methods of the ffmpeg_frame class
*/
zend_function_entry ffmpeg_frame_class_methods[] = {
/* object can't be created from user space so no PHP constructor */
#if HAVE_LIBGD20
/* gd methods */
FFMPEG_PHP_MALIAS(ffmpeg_frame, togdimage, toGDImage, NULL, 0)
PHP_ME(ffmpeg_frame, __construct, NULL, 0)
#endif // HAVE_LIBGD20
/* methods */
FFMPEG_PHP_MALIAS(ffmpeg_frame, getwidth, getWidth, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_frame, getheight, getHeight, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_frame, resize, resize, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_frame, iskeyframe, isKeyFrame, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_frame, getpresentationtimestamp, getPresentationTimestamp, NULL, 0)
FFMPEG_PHP_MALIAS(ffmpeg_frame, getpts, getPresentationTimestamp, NULL, 0)
FFMPEG_PHP_END_METHODS
};
/* }}} */
/* {{{ _php_alloc_ff_frame()
*/
static ff_frame_context* _php_alloc_ff_frame()
{
ff_frame_context *ff_frame = NULL;
ff_frame = emalloc(sizeof(ff_frame_context));
ff_frame->av_frame = NULL;
ff_frame->width = 0;
ff_frame->height = 0;
ff_frame->pixel_format = 0;
return ff_frame;
}
/* }}} */
/* {{{ proto object _php_create_ffmpeg_frame()
creates an ffmpeg_frame object, adds a ffmpeg_frame resource to the
object, registers the resource and returns a direct pointer to the
resource.
*/
ff_frame_context* _php_create_ffmpeg_frame(INTERNAL_FUNCTION_PARAMETERS)
{
int ret;
ff_frame_context *ff_frame;
ff_frame = _php_alloc_ff_frame();
ret = ZEND_REGISTER_RESOURCE(NULL, ff_frame, le_ffmpeg_frame);
object_init_ex(return_value, ffmpeg_frame_class_entry_ptr);
add_property_resource(return_value, "ffmpeg_frame", ret);
return ff_frame;
}
/* }}} */
/* {{{ _php_free_av_frame()
*/
static void _php_free_av_frame(AVFrame *av_frame)
{
if (av_frame) {
if (av_frame->data[0]) {
av_free(av_frame->data[0]);
av_frame->data[0] = NULL;
}
av_free(av_frame);
}
}
/* }}} */
/* {{{ _php_free_ffmpeg_frame()
*/
static void _php_free_ffmpeg_frame(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ff_frame_context *ff_frame = (ff_frame_context*)rsrc->ptr;
_php_free_av_frame(ff_frame->av_frame);
efree(ff_frame);
}
/* }}} */
/* {{{ register_ffmpeg_frame_class()
*/
void register_ffmpeg_frame_class(int module_number)
{
TSRMLS_FETCH();
le_ffmpeg_frame = zend_register_list_destructors_ex(_php_free_ffmpeg_frame,
NULL, "ffmpeg_frame", module_number);
INIT_CLASS_ENTRY(ffmpeg_frame_class_entry, "ffmpeg_frame",
ffmpeg_frame_class_methods);
/* register ffmpeg frame class */
ffmpeg_frame_class_entry_ptr =
zend_register_internal_class(&ffmpeg_frame_class_entry TSRMLS_CC);
}
/* }}} */
/* {{{ _php_convert_frame()
*/
int _php_convert_frame(ff_frame_context *ff_frame, int dst_fmt) {
TSRMLS_FETCH();
AVFrame *dst_frame;
int result = 0;
if (!ff_frame->av_frame) {
return -1;
}
if (dst_fmt == ff_frame->pixel_format) {
return 0; // NOP
}
dst_frame = avcodec_alloc_frame();
avpicture_alloc((AVPicture*)dst_frame, dst_fmt, ff_frame->width,
ff_frame->height);
result = img_convert( (AVPicture *)dst_frame, dst_fmt,
(AVPicture *) ff_frame->av_frame, ff_frame->pixel_format, ff_frame->width, ff_frame->height);
if (result) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error converting frame");
_php_free_av_frame(dst_frame);
} else {
ff_frame->av_frame = dst_frame;
ff_frame->pixel_format = dst_fmt;
}
return result;
}
/* }}} */
#if HAVE_LIBGD20
/* {{{ _php_get_gd_image()
*/
static int _php_get_gd_image(int w, int h TSRMLS_DC)
{
zval *function_name, *width, *height;
zval **argv[2];
zend_function *func;
zval *retval;
char *function_cname = "imagecreatetruecolor";
int ret;
if (zend_hash_find(EG(function_table), function_cname, strlen(function_cname) + 1, (void **)&func) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error can't find %s function", function_cname);
return -1;
}
MAKE_STD_ZVAL(function_name);
MAKE_STD_ZVAL(width);
MAKE_STD_ZVAL(height);
ZVAL_STRING(function_name, function_cname, 0);
ZVAL_LONG(width, w);
ZVAL_LONG(height, h);
argv[0] = &width;
argv[1] = &height;
if (call_user_function_ex(EG(function_table), NULL, function_name,
&retval, 2, argv, 0, NULL TSRMLS_CC) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error calling %s function", function_cname);
FREE_ZVAL(function_name);
FREE_ZVAL(width);
FREE_ZVAL(height);
return -1;
}
FREE_ZVAL(function_name);
FREE_ZVAL(width);
FREE_ZVAL(height);
if (!retval || Z_TYPE_P(retval) != IS_RESOURCE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error creating GD Image");
return -1;
}
ret = Z_LVAL_P(retval);
zend_list_addref(ret);
zval_ptr_dtor(&retval);
return ret;
}
/* }}} */
/* {{{ _php_avframe_to_gd_image()
*/
static int _php_avframe_to_gd_image(AVFrame *frame, gdImage *dest, int width,
int height)
{
int x, y;
int *src = (int*)frame->data[0];
if (width > dest->sx || height > dest->sy) {
return -1;
}
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
/* copy pixel to gdimage buffer zeroing the alpha channel */
dest->tpixels[y][x] = src[x] & 0x00ffffff;
}
src += width;
}
return 0;
}
/* }}} */
/* {{{ _php_gd_image_to_avframe()
*/
static int _php_gd_image_to_avframe(gdImage *src, AVFrame *frame, int width,
int height)
{
int x, y;
int *dest = (int*)frame->data[0];
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
dest[x] = src->tpixels[y][x];
}
dest += width;
}
return 0;
}
/* }}} */
/* {{{ proto resource toGDImage()
*/
FFMPEG_PHP_METHOD(ffmpeg_frame, toGDImage)
{
ff_frame_context *ff_frame;
gdImage *gd_img;
int rsrc_id;
GET_FRAME_RESOURCE(getThis(), ff_frame);
_php_convert_frame(ff_frame, FFMPEG_PHP_FFMPEG_RGB_PIX_FORMAT);
rsrc_id = _php_get_gd_image(ff_frame->width, ff_frame->height TSRMLS_CC);
if (rsrc_id < 0) {
RETURN_FALSE;
}
RETVAL_RESOURCE(rsrc_id);
FFMPEG_PHP_FETCH_IMAGE_RESOURCE(gd_img, &return_value);
if (_php_avframe_to_gd_image(ff_frame->av_frame, gd_img, ff_frame->width, ff_frame->height)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to convert frame to gd image");
zend_list_delete(rsrc_id);
RETURN_FALSE;
}
/* return the resource */
}
/* }}} */
/* {{{ proto object ffmpeg_frame(mixed)
*/
FFMPEG_PHP_METHOD(ffmpeg_frame, __construct)
{
AVFrame *frame;
gdImage *gd_img;
ff_frame_context *ff_frame;
int width, height, ret;
zval *param;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", ¶m) != SUCCESS) {
return;
}
switch (Z_TYPE_P(param)) {
case IS_STRING:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Creating an ffmpeg_frame from a file is not implemented");
RETURN_FALSE;
//_php_read_frame_from_file(ff_frame, Z_STRVAL_PP(argv[0]));
break;
case IS_RESOURCE:
FFMPEG_PHP_FETCH_IMAGE_RESOURCE(gd_img, ¶m);
if (!gd_img->trueColor) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "First parameter must be a truecolor GD image");
RETURN_FALSE;
}
ff_frame = _php_alloc_ff_frame();
ret = ZEND_REGISTER_RESOURCE(NULL, ff_frame, le_ffmpeg_frame);
object_init_ex(getThis(), ffmpeg_frame_class_entry_ptr);
add_property_resource(getThis(), "ffmpeg_frame", ret);
width = gdImageSX(gd_img);
height = gdImageSY(gd_img);
/* create a an av_frame and allocate space for it */
frame = avcodec_alloc_frame();
avpicture_alloc((AVPicture*)frame, FFMPEG_PHP_FFMPEG_RGB_PIX_FORMAT, width, height);
/* copy the gd image to the av_frame */
_php_gd_image_to_avframe(gd_img, frame, width, height);
/* set the ffmepg_frame to point to this av_frame */
ff_frame->av_frame = frame;
/* set the ffpmeg_frame's properties */
ff_frame->width = width;
ff_frame->height = height;
ff_frame->pixel_format = FFMPEG_PHP_FFMPEG_RGB_PIX_FORMAT;
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid argument passed");
RETURN_FALSE;
}
}
/* }}} */
#endif /* HAVE_LIBGD20 */
/* {{{ proto int getPresentationTimestamp()
*/
FFMPEG_PHP_METHOD(ffmpeg_frame, getPresentationTimestamp)
{
ff_frame_context *ff_frame;
GET_FRAME_RESOURCE(getThis(), ff_frame);
RETURN_DOUBLE((double)ff_frame->pts / AV_TIME_BASE);
}
/* }}} */
/* {{{ proto int isKeyFrame()
*/
FFMPEG_PHP_METHOD(ffmpeg_frame, isKeyFrame)
{
ff_frame_context *ff_frame;
GET_FRAME_RESOURCE(getThis(), ff_frame);
RETURN_LONG(ff_frame->keyframe);
}
/* }}} */
/* {{{ proto int getWidth()
*/
FFMPEG_PHP_METHOD(ffmpeg_frame, getWidth)
{
ff_frame_context *ff_frame;
GET_FRAME_RESOURCE(getThis(), ff_frame);
RETURN_LONG(ff_frame->width);
}
/* }}} */
/* {{{ proto int getHeight()
*/
FFMPEG_PHP_METHOD(ffmpeg_frame, getHeight)
{
ff_frame_context *ff_frame;
GET_FRAME_RESOURCE(getThis(), ff_frame);
RETURN_LONG(ff_frame->height);
}
/* }}} */
/* {{{ _php_resample_frame()
*/
int _php_resample_frame(ff_frame_context *ff_frame,
int wanted_width, int wanted_height, int crop_top, int crop_bottom,
int crop_left, int crop_right)
{
AVFrame *resampled_frame;
ImgReSampleContext *img_resample_ctx = NULL;
if (!ff_frame->av_frame) {
return -1;
}
/*
* do nothing if width and height are the same as the frame and no
* cropping was specified
* */
if (wanted_width == ff_frame->width &&
wanted_height == ff_frame->height &&
(!crop_left && !crop_right && !crop_top && !crop_bottom)) {
return 0;
}
/* convert to AV_PIX_FMT_YUV420P required for resampling */
_php_convert_frame(ff_frame, AV_PIX_FMT_YUV420P);
img_resample_ctx = img_resample_full_init(
wanted_width, wanted_height,
ff_frame->width, ff_frame->height,
crop_top, crop_bottom, crop_left, crop_right,
0, 0, 0, 0);
if (!img_resample_ctx) {
return -1;
}
resampled_frame = avcodec_alloc_frame();
avpicture_alloc((AVPicture*)resampled_frame, AV_PIX_FMT_YUV420P,
wanted_width, wanted_height);
img_resample(img_resample_ctx, (AVPicture*)resampled_frame,
(AVPicture*)ff_frame->av_frame);
_php_free_av_frame(ff_frame->av_frame);
img_resample_close(img_resample_ctx);
ff_frame->av_frame = resampled_frame;
ff_frame->width = wanted_width;
ff_frame->height = wanted_height;
return 0;
}
/* }}} */
/* {{{ proto boolean resize(int width, int height [, int crop_top [, int crop_bottom [, int crop_left [, int crop_right ]]]])
*/
FFMPEG_PHP_METHOD(ffmpeg_frame, resize)
{
ff_frame_context *ff_frame = NULL;
long wanted_width, wanted_height;
long crop_top = 0, crop_bottom = 0, crop_left = 0, crop_right = 0;
int res;
GET_FRAME_RESOURCE(getThis(), ff_frame);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|llll", &wanted_width, &wanted_height, &crop_top, &crop_bottom, &crop_left, &crop_right) != SUCCESS) {
return;
}
if (crop_right % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Crop right must be an even number");
RETURN_FALSE;
}
if (crop_left % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Crop left must be an even number");
RETURN_FALSE;
}
if (crop_bottom % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Crop bottom must be an even number");
RETURN_FALSE;
}
if (crop_top % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Crop top be an even number");
RETURN_FALSE;
}
/* bounds check wanted height */
if (wanted_height < 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Frame height must be greater than zero");
RETURN_FALSE;
}
/* wanted height must be even number for lavc resample */
if (wanted_height % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Frame height must be an even number");
RETURN_FALSE;
}
/* bounds check wanted width */
if (wanted_width < 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Frame width must be greater than zero");
RETURN_FALSE;
}
/* wanted width must be even number for lavc resample */
if (wanted_width % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Frame width must be an even number");
RETURN_FALSE;
}
/* resize frame */
res = _php_resample_frame(ff_frame, wanted_width, wanted_height,
crop_top, crop_bottom, crop_left, crop_right);
if (res < 0) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto boolean crop([, int crop_top [, int crop_bottom [, int crop_left [, int crop_right ]]]])
*/
PHP_FUNCTION(crop)
{
ff_frame_context *ff_frame;
long crop_top = 0, crop_bottom = 0, crop_left = 0, crop_right = 0;
int res;
GET_FRAME_RESOURCE(getThis(), ff_frame);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|llll", &crop_top, &crop_bottom, &crop_left, &crop_bottom) != SUCCESS) {
return;
}
if (crop_right % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Crop right must be an even number");
RETURN_FALSE;
}
if (crop_left % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Crop left must be an even number");
RETURN_FALSE;
}
if (crop_bottom % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Crop bottom must be an even number");
RETURN_FALSE;
}
if (crop_top % 2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Crop top be an even number");
RETURN_FALSE;
}
/* resample with same dimensions */
res = _php_resample_frame(ff_frame, ff_frame->width, ff_frame->height,
crop_top, crop_bottom, crop_left, crop_right);
if (res < 0) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 expandtab
* vim<600: noet sw=4 ts=4
*/